任何人都可以帮我优化此查询吗?我是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'))
答案 0 :(得分:0)
为了优化MIN(g.starttime)
,我认为您需要(serialid, starttime)
中gtable
的综合索引:
CREATE INDEX serial_starttime on gtable (serialid, starttime);
created
中的ptable
索引将优化WHERE
子句。