如果可能的话,我有一个可以进行优化的查询,因为它需要 15 秒才能运行。
它正在查询一个包含大约 1000000 条记录的大型数据库,并且通过按小时分组(从 DATE_FORMAT() 派生)来减慢速度。
我为所有表中的所有相关字段编制了索引,这显着提高了性能,但我不知道如何或是否甚至可以为小时组创建索引,因为它不是一个字段......
我确实意识到数据集非常大,但我想知道我是否有任何选择。
任何帮助,将不胜感激!谢谢!
SELECT `id`,
tbl1.num,
name,
DATE_FORMAT(`timestamp`,'%x-%v') AS wknum,
DATE_FORMAT(`timestamp`,'%Y-%m-%d') AS date,
DATE_FORMAT(`timestamp`,'%H') as hour,
IF(code<>0,codedescription,'') AS status,
SUM(TIME_TO_SEC(`timeblock`))/60 AS time,
SUM(`distance`) AS distance,
SUM(`distance`)/(SUM(TIME_TO_SEC(`timeblock`))/60) AS speed
FROM `tbl1`
LEFT JOIN `tbl2` ON tbl1.code = tbl2.code
LEFT JOIN `tbl3` ON tbl1.status = tbl3.status
LEFT JOIN `tbl4` ON tbl1.conditionnum = tbl4.conditionnum
LEFT JOIN `tbl5` ON tbl1.num = edm_mc_list.num
WHERE `timestamp`>'2013-07-28 00:00:00'
GROUP BY `num`,DATE_FORMAT(`timestamp`,'%H'),`mcstatus`