MySQL CURDATE() and NOW() vs. PHP date() – heavy server load
Let's assume the following:
- we have a MySQL table with some fields like "product", "dateX" and "dateY";
- the "product" is a "varchar" type;
- the "dateX" and "dateY" field type is "date" - YYYY-MM-DD;
If we are using the mysql NOW() and CURDATE() native function the query would look like this:
[sourcecode language="php"] SELECT product FROM table WHERE dateX <= NOW() and dateY >= CURDATE() [/sourcecode]
If we use PHP date("Y-m-d") and assume the variable
[sourcecode language="php"] $php_date = date("Y-m-d"); [/sourcecode]
the query would be sth. like:
[sourcecode language="php"] SELECT product FROM table WHERE dateX <= '$php_date' and dateY >= '$php_date' [/sourcecode]
I noticed a HUGE LACK in PERFORMANCE while using the native MySQL NOW() and CURDATE() function as opposed to the second scenario when using the string date generated by the php date function.
On a particular project, I work at, the page load was affected by a difference of more than 8 Seconds which is unacceptable.
As always I look forward to your comments...