Per this comment there are at least 8 different ways to select records by year and month:
- where date like '2005-01 -%'
- where DATE_FORMAT (date, '% Y-% m') = '2005-01'
- where EXTRACT (YEAR_MONTH FROM date) = '200501'
- where YEAR (date) = '2005' and MONTH (date) = '1'
- where substring (date, 1,7) = '2005-01'
- where the date is between '2005-01-01' and '2005-01-31'
- where date> = '2005-01-01' and date <= '2005-01-31'
- where date IN ('2005-01-01', '2005-01-02', '2005-01-03', '2005-01-04', '2005-01-05', '2005-01-06 ',' 2005-01-07 ',' 2005-01-08 ',' 2005-01-09 ',' 2005-01-10 ',' 2005-01-11 ',' 2005-01-12 ', '2005-01-13', '2005-01-14', '2005-01-15', '2005-01-16', '2005-01-17', '2005-01-18', '2005 -01-19 ',' 2005-01-20 ',' 2005-01-21 ',' 2005-01-22 ',' 2005-01-23 ',' 2005-01-24 ',' 2005-01 -25 ',' 2005-01-26 ',' 2005-01-27 ',' 2005-01-28 ',' 2005-01-29 ',' 2005-01-30 ',' 2005-01-31 ')
What is the fastest WHERE clause ? What is faster in GROUP BY ? Can any of them use the index?
My specific request looks like this:
select count(*) from quotes quote where EXTRACT(YEAR_MONTH FROM qtime) = :date
Where qtime is 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;
and in this case I cannot use comparison operators.
mysql
mpen
source share