Given the following two mysql queries:
SELECT * FROM table1 WHERE DATE_ADD(datetimecolumn,INTERVAL 24 HOUR)>NOW()
And
SELECT * FROM table1 WHERE DATE_SUB(NOW(),INTERVAL 24 HOUR)>datetimecolumn
Which query will be faster?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What I think you want to know:
The second ought to be appreciably faster, since
datetimecolumnmay be directly used in an index operation. Without an index, I’d say the difference ought to be negligible (time operation and function calls being too much faster than data retrieval from disk).What you really asked:
The two queries are not equivalent (they do not return the same records).
retrieves the records NEWER than 24 hours from now (so, all those inserted since yesterday).
The second
retrieves the records OLDER than 24 hours from now (two or more days in the past).
So in that case, which query is the faster, the answer would be it depends on how many records there are…