简体   繁体   English

MySQL中的数学函数比PHP快吗?

[英]Are mathematical functions in MySQL faster than PHP?

I've been working with MySQL for a while, but I've never really used the supported mathematical functions , such as FLOOR() , SQRT() , CRC32() , etc. 我一直在使用MySQL一段时间,但我从未真正使用过支持的数学函数 ,例如FLOOR()SQRT()CRC32()等。

Is it faster / better to use these functions in queries rather than just doing the same on the result set with PHP? 在查询中使用这些函数更快/更好,而不是在PHP的结果集上做同样的事情吗?

EDIT : I don't think this question is a duplicate of this , as my question is about mathematical functions, listed on the page I linked, not CONCAT() or NOW() or any other function as in that question. 编辑 :我不认为这个问题与重复,因为我的问题是关于数学函数,列在我链接的页面上, 而不是 CONCAT()NOW()或该问题中的任何其他函数。 Please consider this before flagging. 在标记之前请考虑这一点。

There's no general answer to that. 对此没有一般的答案。 You certainly shouldn't go out of your way to do math in SQL instead of PHP; 你当然不应该用SQL而不是PHP来做数学; it really doesn't make that much of a difference, if there is any. 如果有的话,它确实没有那么大的差别。 However, if you're doing an SQL query anyway, and you have the choice of doing the operation in PHP before you send it to MySQL or in the query itself... it still won't make much of a difference. 但是,如果您正在进行SQL查询,并且您可以选择在将其发送到MySQL或查询本身之前在PHP中执行操作......它仍然不会产生太大的影响。 Oftentimes there will be a logical difference, in terms of when and how often exactly the operation is performed and where it needs to be performed and where the code to do this is best kept for good maintainability and reuse. 通常情况下,在执行操作的时间和频率以及需要执行的位置以及执行此操作的代码最佳位置以实现良好的可维护性和重用方面存在逻辑差异。 That should be your first consideration, not performance. 这应该是你的首要考虑因素,而不是表现。

Overall, you have to do some really complex math for any of it to make any difference whatsoever. 总的来说,你必须做一些非常复杂的数学运算才能产生任何不同。 Some simple math operations are trivial in virtually any language and environment. 几乎任何语言和环境中的一些简单的数学运算都是微不足道的。 If in doubt, benchmark your specific case. 如有疑问,请根据具体情况进行基准测试

It is more efficient to do this in PHP. 在PHP中执行此操作更有效。

Faster depends on the machines involved, if you're talking about faster for one user. 如果您为一个用户更快地谈论,则更快取决于所涉及的机器。 If you're talking about faster for a million users hitting a website, then it's more efficient to do these calculations in PHP. 如果你正在谈论一百万用户访问网站的速度更快,那么在PHP中进行这些计算会更有效率。

The load of a webserver running PHP is very easily distributed over a large number of machines. 运行PHP的Web服务器的负载非常容易分布在大量机器上。 These machines can run in parallel, handling requests from visitors and fetching necessary information from the database. 这些机器可以并行运行,处理访问者的请求并从数据库中获取必要的信息。 The database, however, is not easy to run in parallel. 但是,数据库并不容易并行运行。 Issues such as replication or sharding are complex and can require specialty software and properly organized data to function well. 复制或分片等问题很复杂,可能需要专业软件和正确组织的数据才能正常运行。 These are expensive solutions compared to adding another PHP installation to a server array. 与将另一个PHP安装添加到服务器阵列相比,这些是昂贵的解决方案。

Because of this, the value of a CPU cycle on the database machine is far more valuable than one on the webserver. 因此,数据库计算机上CPU周期的值远远超过Web服务器上的值。 So you should perform these math functions on the webserver where CPU cycles are cheaper and significantly more easy to parallelize. 因此,您应该在Web服务器上执行这些数学函数,其中CPU周期更便宜并且更容易并行化。

MySQL is faster in scope of SQL query. MySQL的SQL查询范围更快。 PHP is faster in PHP code. PHP在PHP代码中更快。 If you make SQL query to find out SQRT() it should be definitely slower (unless PHP is broken) because MySQL parser and networking overhead. 如果你进行SQL查询以找出SQRT()它应该肯定更慢(除非PHP被破坏)因为MySQL解析器和网络开销。

Like deceze said there's probably not going to be much difference in speed between using math functions in SQL and PHP. 就像deceze所说,在SQL和PHP中使用数学函数之间的速度可能没有多大差别。 If you're really worried, you should always benchmark both of your use cases. 如果您真的很担心,您应该始终对两个用例进行基准测试。

However, one example that comes to my mind, when is probably better to use SQL math functions than doing PHP math functions: when you don't need to perform any additional operations on the results from the DB. 但是,我想到的一个例子是,使用SQL数学函数可能比使用PHP数学函数更好:当您不需要对DB的结果执行任何其他操作时。 If you do your operations in MySQL, you avoid having to loop through results in PHP. 如果您在MySQL中执行操作,则可以避免在PHP中循环遍历结果。

There is an additional consideration to think of and that's scaling. 还有一个需要考虑的因素,那就是扩展。 You usually have one MySQL server (if you have more than one, then you probably already know all of this). 你通常有一个MySQL服务器(如果你有一个以上,那么你可能已经知道了所有这些)。 But you can have many web servers connecting to the same MySQL server (eg when having load balancer). 但是,您可以将许多Web服务器连接到同一MySQL服务器(例如,在具有负载平衡器时)。

In that case, it's going to be better to move the computation to the PHP to take the load of MySQL. 在这种情况下,将计算移动到PHP以获取MySQL的负载会更好。 It's "easier" to add more web servers than to increase the performance of MySQL. 添加更多Web服务器比提高MySQL的性能“更容易”。 In theory you can add infinite amount of web servers, but the amount of memory and the processor speed / num. 从理论上讲,您可以添加无限量的Web服务器,但内存量和处理器速度/数量。 of cores in a MySQL server is finite. MySQL服务器中的核心是有限的。 You can scale MySQL in some other ways, like using MySQL cluster or doing master-slave replication and reading from slaves, but that will always be more complicated / harder to do. 您可以通过其他方式扩展MySQL,例如使用MySQL集群或进行主从复制和从从设备读取,但这将总是更复杂/更难。

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

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