I have a MySQL innodb table with a few columns.
one of them is named "dateCreated" which is a DATETIME
column and it is indexed.
My query:
SELECT
*
FROM
`table1`
WHERE
DATE(`dateCreated`) BETWEEN '2014-8-7' AND '2013-8-7'
MySQL for some reason refuses to use the index on the dateCreated
column (even with USE INDEX
or FORCE INDEX
.
However, if I change the query to this:
SELECT
*
FROM
`table1`
WHERE
`dateCreated` BETWEEN '2014-8-7' AND '2013-8-7'
note the DATE(...)
removal
MySQL uses the index just fine. I could manage without using the DATE() function, but this is just weird to me.
I understand that maybe MySQL indexes the full date and time and when searching only a part of it, it gets confused or something. But there must be a way to use a partial date (lets say MONTH(...)
or DATE(...)
) and still benefit from the indexed column and avoid the full table scan.
Any thoughts..?
Thanks.