且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL innoDB:查询执行时间长

更新时间:2021-10-05 23:30:53

首先,在(magnitude_id, reading_date)上添加索引:

ALTER TABLE
  ADD INDEX magnitude_id__reading_date__IX       -- just a name for the index
    (magnitude_id, reading_date) ;

然后尝试以下变体形式:

Then try this variation:

SELECT vm.* 
FROM value_magnitudes AS vm
  JOIN 
  ( SELECT MIN(id) AS id
    FROM value_magnitudes
    WHERE magnitude_id = 234
      AND reading_date >= '2013-04-01'         -- changed so index is used
    GROUP BY DATE(reading_date)
  ) AS vi
    ON vi.id = vm.id ;

GROUP BY DATE(reading_date)仍然需要将函数应用于所有选定的行(通过索引),除非您遵循@jurgen的建议并将该列拆分为datetime列,否则无法对其进行改进.

The GROUP BY DATE(reading_date) will still need to apply the function to all the selected (thorugh the index) rows and that cannot be improved, unless you follow @jurgen's advice and split the column into date and time columns.