I have a datetime field, but I often query over the date part, like this
select * from table where date(datetime_field)=curdate()
This query can't use index on datetime_field!
What would be the best solution with the minimum amount of changes in code or db?
PS. upgrading mysql could be an option if there is a solution I'm not aware of yet, but a more short term solution for 5.6 would be appreciated
Best Answer
The problem isn't the index. The problem is that your query as written does not allow an index to be used.
Modify your query so that the date range is resolved to a pair of constants and the problem is solved:
This will use the index on the column.
As a rule, never use a column as an argument to a function in
WHERE
because this forces a table scan.