Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 基于邮政编码的搜索_Ruby On Rails_Ruby_Thinking Sphinx - Fatal编程技术网

Ruby on rails 基于邮政编码的搜索

Ruby on rails 基于邮政编码的搜索,ruby-on-rails,ruby,thinking-sphinx,Ruby On Rails,Ruby,Thinking Sphinx,我有一个应用程序,店主可以输入10个邮政编码,他们可以在其中提供服务。目前,这些邮政编码存储在单个表列中。那么,基于此进行搜索的最佳有效方法是什么?我应该将所有邮政编码(所有美国邮政编码)存储在一个表中,并建立多对多关系,还是使用thinking sphinx基于当前字段进行文本搜索?数据库管理员的视角 既然您正在谈论使用Sphinx,我假定您将所有10个邮政编码存储在一行中,如下所示 shop_id zip_codes -- 167 22301, 22302, 22303, 223

我有一个应用程序,店主可以输入10个邮政编码,他们可以在其中提供服务。目前,这些邮政编码存储在单个表列中。那么,基于此进行搜索的最佳有效方法是什么?我应该将所有邮政编码(所有美国邮政编码)存储在一个表中,并建立多对多关系,还是使用thinking sphinx基于当前字段进行文本搜索?

数据库管理员的视角

既然您正在谈论使用Sphinx,我假定您将所有10个邮政编码存储在一行中,如下所示

shop_id  zip_codes
--
167      22301, 22302, 22303, 22304, 22305, 22306, 22307, 22308, 22309, 22310
出于搜索和其他几个原因,您最好这样存储它们

shop_id  zip_codes
--
167      22301
167      22302
167      22303
167      22304
167      22305
167      22306
167      22307
167      22308
167      22309
167      22310

-- Example in SQL.
create table serviced_areas (
  shop_id integer not null references shops (shop_id), -- Table "shops" not shown.
  zip_code char(5) not null,
  primary key (shop_id, zip_code)
);
在做了这一次更改后,您可以很好地说明停止的理由

但是,如果dbms支持正则表达式,则无需对数据库进行任何其他更改,就可以显著提高数据完整性。有了这种dbms支持,您可以保证邮政编码列只包含5个整数,不包含字母。(可能还有其他方法可以保证5个整数而不包含字母。)

邮政编码表将进一步提高数据的完整性。但是你可以很容易地争辩说,店主在输入有效的邮政编码时有一个既得利益,这不值得你付出更多的努力。邮政编码经常变化;不要期望“完整”的邮政编码表在很长时间内都是准确的。您需要有一个定义良好的程序来处理新的和过期的邮政编码

-- Example in SQL
create table zip_codes (
  zip_code char(5) primary key
);

create table serviced_areas (
  shop_id integer not null references shops (shop_id),
  zip_code char(5) not null references zip_codes (zip_code),
  primary key (shop_id, zip_code)
);

一个数据库专家的观点

既然您正在谈论使用Sphinx,我假定您将所有10个邮政编码存储在一行中,如下所示

shop_id  zip_codes
--
167      22301, 22302, 22303, 22304, 22305, 22306, 22307, 22308, 22309, 22310
出于搜索和其他几个原因,您最好这样存储它们

shop_id  zip_codes
--
167      22301
167      22302
167      22303
167      22304
167      22305
167      22306
167      22307
167      22308
167      22309
167      22310

-- Example in SQL.
create table serviced_areas (
  shop_id integer not null references shops (shop_id), -- Table "shops" not shown.
  zip_code char(5) not null,
  primary key (shop_id, zip_code)
);
在做了这一次更改后,您可以很好地说明停止的理由

但是,如果dbms支持正则表达式,则无需对数据库进行任何其他更改,就可以显著提高数据完整性。有了这种dbms支持,您可以保证邮政编码列只包含5个整数,不包含字母。(可能还有其他方法可以保证5个整数而不包含字母。)

邮政编码表将进一步提高数据的完整性。但是你可以很容易地争辩说,店主在输入有效的邮政编码时有一个既得利益,这不值得你付出更多的努力。邮政编码经常变化;不要期望“完整”的邮政编码表在很长时间内都是准确的。您需要有一个定义良好的程序来处理新的和过期的邮政编码

-- Example in SQL
create table zip_codes (
  zip_code char(5) primary key
);

create table serviced_areas (
  shop_id integer not null references shops (shop_id),
  zip_code char(5) not null references zip_codes (zip_code),
  primary key (shop_id, zip_code)
);

如果使用sphinx进行地理空间搜索,则需要数据库中的zipcodes加上纬度/经度(我想不需要,您可以使用文本文件或xml)


地理空间搜索的意思是“查找距离您所在位置20英里以内的商店”

如果您使用sphinx进行地理空间搜索,则需要数据库中的zipcodes加上纬度/经度(我想您可以使用文本文件或xml,但事实并非如此)


我所说的地理空间搜索是指类似于“在您所在位置20英里范围内查找店铺”之类的内容。为了灵活性和效率,我会选择#1

“将所有邮政编码存储在一个表中,并建立多对多 关系“


…假设您还需要存储其他邮政编码数据字段(城市、州、县、Lat/Long等)。在这种情况下,您的交叉点将是:shop_id到zipcode_id。但是,如果您不需要/没有扩展的邮政编码数据字段,那么在我看来,一个单独的表,将shop_id添加到acutal zipcodes(非id)就可以了。

为了灵活性和效率,我会选择#1

“将所有邮政编码存储在一个表中,并建立多对多 关系“


…假设您还需要存储其他邮政编码数据字段(城市、州、县、Lat/Long等)。在这种情况下,您的交叉点将是:shop_id到zipcode_id。但是,如果您不需要/没有扩展的邮政编码数据字段,那么在我看来,一个单独的表,将shop_id添加到acutal zipcodes(非id)就可以了。

您知道这个gem吗?(遗憾的是里面没有邮政编码)不知道。总有一天会有用的:)你知道这个宝石吗?(遗憾的是里面没有邮政编码)不知道。总有一天会有用:)