MAX()具有Group by的功能非常慢

时间:2013-02-16 19:58:43

标签: mysql sql performance

我的查询真的很慢;执行需要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=''

1 个答案:

答案 0 :(得分:3)

(idgps_unit, dt)上的索引可以使查询更快。

您可以通过更改:

来扩展idgps_unit上的索引
KEY `fk_gps2` (`idgps_unit`),

KEY `fk_gps2` (`idgps_unit`, `dt`),

(根据this SO questionkeyindex的同义词。)