表
CREATE TABLE `ch1` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`no` int(10) unsigned DEFAULT NULL,
`dtime` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)
查询语句 select sum(no) as number,dtime as date from ch1 group by month(dtime)
数据大约1000w条记录,需要40秒,感觉太慢了,有什么解决办法?
CREATE TABLE `ch1` (
`id` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
`no` int(10) unsigned DEFAULT NULL,
`dtime` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
)
查询语句 select sum(no) as number,dtime as date from ch1 group by month(dtime)
数据大约1000w条记录,需要40秒,感觉太慢了,有什么解决办法?
Extra: Using temporary; Using filesort
Extra: Using temporary; Using filesort
这条语句用不上索引,因为group by 在有聚合函数(sum(no))的时候不起作用。
索引在这里不好用,建议统计改为触发器,然后按要求触发单独存放在一张统计表,以后直接查统计表