根据this comment,至少有8种不同的方式按年和月选择记录:
哪个是 WHERE 子句中最快的?哪个 GROUP BY 最快?他们中的任何一个都可以利用索引吗?
我的特定查询如下:
select count(*)
from quotes quote
where EXTRACT(YEAR_MONTH FROM qtime) = :date
qtime
是TIMESTAMP
。
但我还需要获得最多报价的月份:
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;
在这种情况下,我无法使用比较运算符。
答案 0 :(得分:6)
我分析了所有这些查询并得到了结果。
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)
所以我可以说中间的使用是最好的,而且速度更快。虽然使用大于和小于是相同的结果,但我认为介于两者之间非常聪明。