There was an SO question Error matching dates in PDO which I gave an answer on and had some conversation around.
Essentially I said not use wildcards on dates:
SELECT * FROM table where datefiled LIKE '%2018-06%'
And instead use something like (yes a between would work too):
SELECT * FROM table where MONTH(datefield) = '06' and YEAR(datefield) = '2018
This lead to a comment on my answer that stated he was told (data with +90 million rows):
When you apply a function on a column it voids usage of the index for lookups, as it would have to apply the same function to ALL the values in the index and hence would be less effective even. Hence the need to use the trick with dates boundaries.
My understanding of the search algorithms SQL implements is not that great but I get when to use and not to use an index (reading vs writing). The above seems to imply that using the MONTH()
and YEAR()
function would be less effective then a string LIKE
. If the comment was about BETWEEN
I would get it but I cannot conceive of a use case where LIKE
would win out because of the function call.
If it can what is that use case?