Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster to use MySQL's CURDATE() or PHP's date()?

Tags:

php

mysql

Is it faster to use the mysql query:

SELECT CURDATE() as today

or the PHP statement:

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

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

like image 918
Ali Avatar asked Nov 28 '22 10:11

Ali


1 Answers

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.

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.

like image 86
Marc B Avatar answered Dec 01 '22 01:12

Marc B