正确的mysql索引可以减少时间

时间:2016-01-08 16:16:48

标签: mysql sql

我有这个SQL查询:

DROP TABLE IF EXISTS TEMP_FAB;
CREATE TEMPORARY table TEMP_FAB
(
  SELECT f.NAME, f.ID_FAB, f.COLOR          
  FROM 
    (SELECT * FROM `FAB` f ORDER BY f.TIME_MILIS DESC) f
  GROUP BY f.ID_FAB
);

/*very long run*/
SELECT h.ID as 'ID_HUMAN', h.DATE as 'NEXT_DATE',
    t.ID_MAN, 
    a.ID_FAB,   
      tf.NAME as 'FAB_NAME'
FROM `POOL` h 
INNER JOIN `TOTALS` t ON h.ID=t.ID_HUMAN
INNER JOIN (SELECT ID, ID_FAB FROM `MAN` ORDER BY `TIME_MILIS` DESC) a ON t.ID_MAN=a.ID
INNER JOIN `TEMP_FAB` tf ON a.ID_FAB=tf.ID_FAB
WHERE     
    h.DATE>=1420070400000 AND 
    h.DATE<=1452267460041
GROUP BY h.ID;

我创建了这些索引,但运行时间减少了20%:

CREATE INDEX DATE ON POOL (DATE);
CREATE INDEX ID_HUMAN ON TOTALS (ID_HUMAN);
CREATE INDEX TIME_MILIS ON MAN (TIME_MILIS);
CREATE INDEX ID ON MAN (ID);
CREATE INDEX ID_FAB ON MAN (ID_FAB);
CREATE INDEX ID_MAN ON TOTALS (ID_MAN);

我该怎样做才能减少时间?

编辑:我发现此查询“INNER JOIN TEMP_FAB tf on a.ID_FAB = tf.ID_FAB”运行时间很长。

0 个答案:

没有答案