I'm using this query :
select field from table1 where time LIKE '%2016-03%' order by time asc limit 1
My problem is that the table has thousands of rows and so it takes too long to search for 1 row. Is it normal?
I'm using this query :
select field from table1 where time LIKE '%2016-03%' order by time asc limit 1
My problem is that the table has thousands of rows and so it takes too long to search for 1 row. Is it normal?
LIKE
queries are always going to be slower than looking for a specific value.
It'll help immensely to add an INDEX
on the field and change your query to LIKE '2016-03%'
(there won't ever be anything before the year in a timestamp so drop that first %
). It'll be able to take a couple shortcuts, at least.
Try adding an INDEX
to your time
column and as other have pointed out, remove the leading %
from the query.
If you use LIKE and starts with % MySQL must make a FULL TABLE SCAN to find the correct Dates, but if you start direct with the year they can use a index : ... KIKE '2016-03-%'