如何在数据库中管理国家

ale*_*ion 5 php mysql database-design countries database-performance

我知道我的问题有点模糊,但是我觉得管理国家/地区的地址实际上很普遍,因此我想从我的实际设置中获取一些建议。

I have a database with a "country" column, previously it was a medium int type, acting as Foreign Key to another table with the actual information about the countries (id, name and ISO3166-1 alpha2 code mainly).

After some testing and benchmarking I ended up having all the countries information in a php file array instead, including/requiring it when needed, and it was like one or two orders of magnitude faster than querying the database. (they are 278 countries).

So apparently it's a better approach but I feel like there's something wrong because people normally tend to do this kind of stuff reading from a table instead from a file, but I can't figure out what would it be, is it easier to maintain or something like that?

另外,我还考虑将2个字母的ISO代码作为键而不是数字id,这将更易于阅读,并且无论如何它们都是唯一的。我在400.000行表中没有看到明显的性能损失,如果我的数据库增长,这最终会成为错误吗?

Nev*_*uyt 2

一般来说,您希望将变化的事物保持在一起。因此,如果您的主要数据位于 SQL 数据库中,则将国家/地区数据保留在数据库中有助于避免像有人更改 PHP 查找数组而没有意识到 SQL 数据库中存在大量数据那样的疯狂行为。

它还有助于避免重复 - 如果您为系统构建第二个应用程序(例如管理系统),您最终不会得到国家/地区查找 PHP 文件的 2 个副本。同样,重复会产生错误,开发人员会更改数据库和其中一个查找文件,但不会更改另一个。

所有这些都是相当防御性的 - 但应用程序往往会以意想不到的方式发展,避免错误通常是一个好主意。

根据我的经验,在两个表之间使用联接对具有您提到的数字的经过良好调整的系统几乎没有可测量的性能影响 - 在将查找转移到 PHP 之前您是否优化了 SQL?