I ve got a database of ~1 000万参赛者,其中每个条目都有一个日记为。
I ve用一种非独特的BTREE对该栏进行索引。
I m 提出一个问题,即每个不同年份的条目数目:
SELECT DISTINCT(YEAR(awesome_date)) as year, COUNT(id) as count
FROM all_entries
WHERE awesome_date IS NOT NULL
GROUP BY YEAR(awesome_date)
ORDER BY year DESC;
问题大约需要90秒的时间,而EXPLAIN的产出表明:
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
----------------------------------------------------------------------------------------------------------------------------------------
1 | SIMPLE | all_entries | ALL | awesome_date | | | | 9759848 | Using where; Using temporary; Using filesort
如果I FORCE KEY(awesome_date)
将浏览量降至8000万,key_len = 4
,但仍有 使用地方;使用临时;使用文档
。
I also run queries selecting DISTINCT(MONTH(awesome_date))
and DISTINCT(DAY(awesome_date))
with the relevant WHERE
conditions restricting them to a particular year or month.
除了将年、月和日信息单独一栏存储外,是否还能加速处理和(或)避免临时表格和档案?