使用MySQL的CURDATE()或PHP的date()更快? [英] Faster to use MySQL's CURDATE() or PHP's date()?

查看:242
本文介绍了使用MySQL的CURDATE()或PHP的date()更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用mysql查询是否更快:

Is it faster to use the mysql query:

SELECT CURDATE() as today

或PHP语句:

$curdate = date('Y-m-d');

使用date() VS MySQL的NOW()CURTIME()是否适用相同的答案?

Does the same answer apply to using date() VS MySQL's NOW() and CURTIME()?

推荐答案

如果您只是在进行查询以将日期添加到PHP脚本中,请使用PHP函数.在这两种情况下,PHP和MySQL都会调用系统时钟,将时间值转换为格式化的字符串,然后返回. MySQL版本将增加查询解析/编译的开销,以及PHP-> MySQL-> PHP的往返路程.而且,如果您正在通过TCP套接字与MySQL进行通信,则将增加建立/发送/接收TCP连接和数据包的开销.

If you're simply doing the query to get a date into a PHP script, then use the PHP function. In both cases, PHP and MySQL will call the system clock, mangle that time value into the formatted string, and return it. The MySQL version will have the added overhead of query parsing/compiling, and the roundtrip from PHP->MySQL->PHP. And if you're talking to MySQL via TCP socket, you've got added overhead of building/sending/receiving a TCP connection and packets.

另一方面,如果在查询中使用该数据值,最好将其保留在MySQL中.例如,随机地,您可能会在午夜之前启动脚本,以便PHP为其时间戳生成"May 24/2011 23:59:59.999",但是直到"May 25/2011"才在MySQL中执行查询00:00:00.001",因此日期不匹配.

On the other hand, if you're using that data value within a query, you'd be better off keeping it within MySQL. For instance, by random chance, you may start a script immediately before midnight, so that PHP generates "May 24/2011 23:59:59.999" for its timestamp, but the query doesn't execute in MySQL until "May 25/2011 00:00:00.001", so now the dates don't match.

这篇关于使用MySQL的CURDATE()或PHP的date()更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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