我的查询非常慢;执行需要17秒。它在Godaddy VPS上达到100%的CPU。有什么办法吗?
此查询:
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 - 带分组依据的MAX()函数非常慢,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14914541/