MySQL查询效率(使用MIN)

时间:2015-05-28 23:16:42

标签: mysql

任何人都可以帮我优化此查询吗?我是Stackoverflow的新手,非常感谢。

-

SELECT COUNT(DISTINCT(g2.serialid)) as countResult
from g2table g2 INNER JOIN
     ((SELECT p.serialid AS serialid, p.created as created, MIN(g.starttime) AS firstuse
       FROM gtable g INNER JOIN
            ptable p
            ON g.serialid = p.serialid
       WHERE  (p.created < '2015-05-22')
       GROUP BY p.serialid
      ) ) AS firstuseset
     ON g2.serialid = firstuseset.serialid
 WHERE (firstuseset.firstuse < '2015-05-22') AND
       ((g2.starttime > '2015-05-22') AND (g2.starttime < '2015-05-23'))

1 个答案:

答案 0 :(得分:0)

为了优化MIN(g.starttime),我认为您需要(serialid, starttime)gtable的综合索引:

CREATE INDEX serial_starttime on gtable (serialid, starttime);

created中的ptable索引将优化WHERE子句。