我的查询真的很慢;执行需要17秒。 Godaddy VPS 上的 CPU 利用率达到 100%。有什么想法可以做什么吗?
此查询:
SELECT
`gps_unit_location`.`idgps_unit`,
MAX(`gps_unit_location`.`dt`) dtmax
FROM `gps_unit_location`
GROUP BY 1
解释一下
id='1', select type='SIMPLE', table='gps_unit_location', type='index', possible keys=NULL, key='fk_gps2', key len='5', ref=NULL, rows='368668', extra=''
最佳答案
(idgps_unit, dt)
上的索引可以使查询速度更快。
您可以通过更改以下内容来扩展 idgps_unit
上的索引:
KEY `fk_gps2` (`idgps_unit`),
至
KEY `fk_gps2` (`idgps_unit`, `dt`),
(根据 this SO question ,key
是 index
的同义词。)
关于mysql - 带有 Group by 的 MAX() 函数非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14914541/