DATE(mydate) = CURDATE()
vs
mydate = CURDATE()
I have a pretty performance sensitive situation with tens of millions of rows, and I'm wondering if storing only date in datetime
field and time in a separate columns, just to have a simple mydate = CURDATE()
comparison when looking up records by days, would be significantly faster than merging both information in datetime
field and using DATE(mydate) = CURDATE()
.
(yes, I cannot use "date" data type, because of the abstraction layer between me and the database)
Would there be a significant difference or is there a better way to get all records from the given day when using datetime
field?
Best Answer
You just asked
My Answer
When you use
The Query Optimizer will suggest a full table scan even if the column is indexed. Why ? The Query Optimizer is obligated to see the need to call the
DATE()
function againstmydate
in every row, thus eliminating the idea of using the index. Additionally, usingFORCE INDEX
will simply get you nowhere.If
mydate
isdatetime
and is indexed, you could then change the query toor
so you are comparing
datetime
todatetime
and isolating just today. In this instance, an index onmydate
will be used.You do not want to split the date and time into spearate columns as that will cause comparisons of datetime ranges across multiple dates and times very difficult to express in SQL.