我有如下表结构:
id item_id created
1 5 2012-09-05 09:37:59
2 5 2012-09-05 10:25:09
3 5 2012-09-05 11:05:09
4 1 2012-09-05 10:25:09
5 3 2012-09-05 03:05:01
我想通过 WHERE 子句通过当前日期知道哪个 item_id 是最受关注的,如下所示:
SELECT item_id, COUNT( id ) AS TOTAL
FROM stats_item
WHERE DAY( created ) = '05'
AND MONTH( created ) = '07'
AND YEAR( created ) = '2013'
GROUP BY item_id
ORDER BY TOTAL DESC
LIMIT 0 , 30
MySql 中的结果查询
Showing rows 0 - 29 ( 30 total, Query took 4.1747 sec)
耗时长达 4.1747 秒
波纹管是表中的索引
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment
stats_item 0 PRIMARY 1 id A 2575580 NULL NULL BTREE
stats_item 1 created 1 created A 515116 NULL NULL YES BTREE
为什么使用 WHERE 子句查询需要这么长时间并使用 YEAR、MONTH 和 DAY 进行过滤?
=================================== 用解释编辑:
Field Type Null Key Default Extra
id int(11) unsigned NO PRI NULL auto_increment
item_id int(11) unsigned YES NULL
created timestamp YES MUL NULL