Per this comment there's at least 8 different ways to select records by year and month:
Which is the fastest in the WHERE clause? Which is fastest in a GROUP BY? Can any of them take advantage of an index?
My particular query looks like this:
select count(*)
from quotes quote
where EXTRACT(YEAR_MONTH FROM qtime) = :date
Where qtime
is a TIMESTAMP
.
But I also need to get the month with the most quotes:
select date_format(qtime,'%Y-%m') d,count(*) c
from quotes
group by EXTRACT(YEAR_MONTH FROM qtime)
order by c desc, qid asc limit 1;
in which case I can't use the comparison operators.
I analysis all these queries and got the result.
between => Showing rows 0 - 29 (158 total, Query took 0.0030 sec) like => Showing rows 0 - 29 (167 total, Query took 0.0031 sec) DATE_FORMAT => Showing rows 0 - 29 (167 total, Query took 0.0237 sec) EXTRACT => Showing rows 0 - 29 (167 total, Query took 0.0031 sec) YEAR, MONTH => Showing rows 0 - 29 (167 total, Query took 0.0187 sec) substr => Showing rows 0 - 29 (167 total, Query took 0.0224 sec) > < => Showing rows 0 - 29 (158 total, Query took 0.0030 sec)
So i can say the use of between is best and faster from any other. Though use of greater than and less than is the same result but i think the between is pretty smart.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With