用于搜索邮政编码的文件或数据库? [英] File or Database for searching zip code?

查看:55
本文介绍了用于搜索邮政编码的文件或数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在线购物车,结账时用户输入他的邮政编码.

I have a online shopping cart, at checkout user enters his zipcode.

有两种付款方式,货到付款和网上银行.快递服务只运送到某些地区(由邮政编码识别).货到付款和网上银行允许的邮政编码列表不同.(列表长度 = COD 约 2500,后者约 10,000)

There are 2 payment methods, cash-on-delivery and net-banking. The courier service ships to only certain areas(identified by zipcode). And the allowed list of zipcodes for COD and Net-Banking differ. (length of list = about 2500 for COD, and about 10,000 for latter)

我应该将这些列表存储在数据库中还是平面文件中?

Should I store these lists in database or a flat file?

对于数据库,我将使用SELECT进行查询,对于文件,我可以读取数组中的整个(或部分)列表,然后对其进行二进制搜索.

For database, I will be querying using SELECT, and for file, I can read the entire(or partial) list in array, and then do Binary search on it.

考虑到以下几点,哪个会更快 -

Which one would be faster, considering following points -

  1. 现在只有一种快递服务,但将来会有更多服务,并且会有不同的列表.所以我需要在多个列表中进行搜索.
  2. 大部分是读,写会少很多.此外,该列表应该可以在以后自定义.

我会选择数据库,但我不知道它会不会让事情变慢,而且我不想花时间设计数据库,当一个文件可能更好时.

I would have selected Database, but I don't know if it would make things slower, and I don't want to spend time designing database, when a file might be better.

假设有 2 家快递公司 ABC 和 DEF.
对于文件,我将有 4 个文件(比如)ABC_COD.txt、ABC_net.txt、DEF_COD.txt、DEF_net.txt.因此,如果客户要求 COD,我会搜索 ABC_COD,如果不在那里,我会搜索 DEF_COD,依此类推.好吧,这看起来成本很高,但它也很容易扩展.

Say there are 2 courier companies ABC and DEF.
For file I will have 4 files (say) ABC_COD.txt, ABC_net.txt, DEF_COD.txt, DEF_net.txt. So if a customer goes for COD, I search ABC_COD, if not in there, I search DEF_COD and so on. So ok this seems to be costly, but it is also easily extensible.

现在考虑数据库,我将有一个表 Allowed_zipcodes,有五列:zipcode(int/varchar(6))、ABC_COD(boolean)、ABC_net(boolean)、DEF_COD(boolean)、DEF_net(布尔值).如果 x 公司为 y 代码提供 cod,则对应列为 true,否则为 false.虽然这看起来很适合查找,但添加公司涉及架构更改.

Now consider database, I will have a table Allowed_zipcodes, with five columns : zipcode(int/varchar(6)), ABC_COD(boolean), ABC_net(boolean), DEF_COD(boolean), DEF_net(boolean). If the x company offers cod for y code, the corresponding column has true, otherwise false. While this seems good for lookup, adding a company involves a change in schema.

请同时考虑未来的变化和设计.

Please consider future changes and design as well.

推荐答案

出于某种原因,我认为您应该看看 magenta 框架,它不是已经在某些包中了吗?

For some reason I think you should look at the magenta framework, isn't it already in some of the packages?

但是如果你想自己做:只是给你一个数据库模型的起点:

But if you want to do it yourself: Just to give you a starting point on the database model:

carrier
        id(int) | name (varchar)

zipcodes
        start(int) | end(int) | carrier(fk::carrier.id)

例如:

carrier
        1 | UPS
        2 | fedex

zipcodes
        1000 | 1199 | 2
        1000 | 1099 | 1

查询您的邮政编码和可用的运营商:

Querying your zipcode and available carriers:

SELECT carrier.name 
FROM zipcodes 
LEFT JOIN carrier ON zip codes.carrier = carrier.id
WHERE
   zipcodes.end >= :code
AND
   zipcodes.start <= :code

这篇关于用于搜索邮政编码的文件或数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆