简体   繁体   English

PHP与MySQL之间的数据处理速度

[英]Speed of data manipulation in PHP vs MySQL

Apologies in advance if this is a silly question but I'm wondering which might be faster/better in the following simplified scenario... 如果这是一个愚蠢的问题,请提前道歉,但我想知道在以下简化方案中哪种方法更快/更好。

I've got registered users (in a users table) and I've got countries (in a countries table) roughly as follows: 我已经有注册用户(在用户表中),并且有国家(在国家表中)大致如下:

USERS TABLE: user_id (PK, INT) | 用户表:user_id(PK,INT)| country_id (FK, TINYINT) | country_id(FK,TINYINT)| other user-related fields... 其他与用户相关的字段...

COUNTRIES TABLE: country_id (PK, TINYINT) | 国家/地区表:country_id(PK,TINYINT)| country_name (VARCHAR) | country_name(VARCHAR)| other country-related fields... 其他国家相关领域...

Now, every time I need to display a user's country, I need to do a MySQL join. 现在,每次需要显示用户所在的国家/地区时,都需要进行MySQL连接。 However, I often need to do lots of other joins with regard to the users and the big picture seems quite "join-heavy". 但是,对于用户,我经常需要进行许多其他联接,并且总体情况似乎很“繁重”。

I'm wondering what the pros & cons might be of taking the countries out of the database and sticking them into a class as an array, from which I could easily retrieve them with public method calls using country_id? 我想知道将国家/地区从数据库中取出并粘贴到数组中的类中的利弊是什么,我可以使用country_id通过公共方法调用轻松地从中检索它们吗? Would there be a speed advantage/disadvantage? 速度上会有优势/劣势吗?

Thanks a lot. 非常感谢。

EDIT: Thanks for the all the views, very useful. 编辑:感谢所有的意见,非常有用。 I'll pick the first answer as the accepted solution although all contributions are valued. 尽管所有贡献都得到了重视,但我将选择第一个答案作为可接受的解决方案。

Do you have a serious problem performance problem now? 您现在有严重的问题性能问题吗? I recently went through a performance improvement on a php/mysql website I developed for my company. 我最近在为公司开发的php / mysql网站上进行了性能改进。 Certain areas were too slow, and it turned out a lot of fault was with the queries themselves. 某些区域太慢,结果发现查询本身存在很多错误。 I used timers to figure out which queries were slow, and I reorganized them (added indexes, etc). 我使用计时器来找出哪些查询速度很慢,然后重新组织了它们(添加了索引等)。 In a few cases, it was faster to make two separate queries and join them in php (I had some pretty complicated joins). 在某些情况下,进行两个单独的查询并将其加入php速度更快(我有一些非常复杂的连接)。

Do not try to optimize until you know you have a problem. 在知道自己有问题之前,请不要尝试进行优化。 Figure out if you have a problem first by measuring it, and then if you need to rearrange your queries you will be able to know if you made an improvement. 首先通过测量来确定您是否有问题,然后如果您需要重新安排查询,您将能够知道自己是否有所改进。

It would ease stress on your MySQL server to have less JOIN statements, but not significantly so (there aren't that many countries in the world). 使用较少的JOIN语句可以减轻MySQL服务器的压力,但这样做的效果不是很大(世界上没有很多国家)。 However, you'll make up that time in the fact that you'll have to implement the JOIN yourself in PHP. 但是,您将不得不浪费时间,因为您必须自己在PHP中实现JOIN。 And since you're writing it yourself, you will probably write it less efficiently than the SQL statement, which means that it will take more time. 而且由于您自己编写它,所以编写它的效率可能不如SQL语句,这意味着将花费更多时间。 I would recommend keeping it in the SQL server, since the advantages of moving it out are so few (and if the PHP instance and the MySQL instance are on the same box, there are not real advantages). 我建议将其保留在SQL Server中,因为将其移出的优点很少(如果PHP实例和MySQL实例位于同一盒子上,则没有真正的优点)。

What you suggest should be faster. 您建议的速度应该更快。 Granted, the join probably doesn't cost much, but looking it up in a dictionary should be just about free as far as compute power goes. 当然,联接可能不会花费很多,但是就计算能力而言,在字典中查找它应该几乎是免费的。

This is really just a trade off of memory for speed. 这实际上只是内存与速度之间的折衷。 The only downsides I could see would of course be the increased memory usage to store the country info and the fact that you would have to invalidate that cache if you ever update the countries table (which is probably not very often). 我唯一能看到的缺点当然是存储国家/地区信息的内存使用量增加,并且如果您要更新国家/地区表,就必须使该缓存无效(这可能不是很常见)。

I don't think you'd gain anything from removing the join, as you'd have to iterate over all your result rows and manually lookup the country name, which I doubt would be quicker than MySQL can do. 我认为删除连接不会让您有所收获,因为您必须遍历所有结果行并手动查找国家/地区名称,我怀疑这会比MySQL更快。

I also would not consider such an approach for the following reason: If you want to change the name of a country (say you've got a typo), you can do so just by updating a row in the database. 由于以下原因,我也不会考虑这种方法:如果您想更改国家/地区的名称(例如您有错字),则只需更新数据库中的一行即可。 But if the names of the countries are in your PHP code, you'd have to redeploy the code in order to make a change. 但是,如果国家名称在您的PHP代码中,则必须重新部署代码才能进行更改。 I don't know PHP, but that might not be as straightforard than a DB change in a production system. 我不了解PHP,但是这可能不比生产系统中的数据库更改那么简单。

So for maintainability reasons, IMHO let the DB do the work. 因此,出于可维护性的原因,恕我直言,让数据库来完成这项工作。

The general rule in a database world is to NORMALIZED first (results in more tables) and figure performance issues later. 数据库领域的一般规则是首先进行规范化(生成更多表),然后再处理性能问题。

You will want to DENORMALIZED only for simplicity of code, not for performance. 您只想为了代码的简化而不是为了性能而对DENORMALIZED。 Use indexes and stored procedures. 使用索引和存储过程。 DBMS are designed to optimize on joins. DBMS旨在优化联接。

The reason not "normalize as you go" is that you would have to modify the code you already have written most every time you modify the database design. 之所以不能“随便进行标准化”,是因为每次修改数据库设计时,您都必须修改已经编写的最多的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM