我正在尝试优化此查询。现在需要28秒。
我的查询过去常常缺少AS 。添加后,查询时间下降了20%
SELECT
g.id,
g.adresid,
g.senaryoid,
g.olayid,
g.gonderilecegitarih
FROM
(
SELECT
adresid
FROM
expose2.800_emsenaryolar_emgidenbulten
WHERE
olayid = '3320'
) AS s
RIGHT JOIN expose2.800_emsenaryolar_emgidenbulten AS g ON s.adresid = g.adresid
WHERE
s.adresid IS NULL
AND g.olayid = '2784'
AND g.durum = '1'
AND g.gonderilecegitarih < DATE_SUB(
'2015-05-13 15:40:15',
INTERVAL 1 DAY
)
答案 0 :(得分:0)
如果在子查询中使用s.adresid IS NULL
条件,它将加快加快行数...
SELECT
g.id,
g.adresid,
g.senaryoid,
g.olayid,
g.gonderilecegitarih
FROM (
SELECT adresid FROM expose2.800_emsenaryolar_emgidenbulten WHERE olayid = '3320' and s.adresid IS NULL
) AS s
RIGHT JOIN expose2.800_emsenaryolar_emgidenbulten AS g ON s.adresid = g.adresid
AND g.olayid = '2784'
AND g.durum = '1'
AND g.gonderilecegitarih < DATE_SUB(
'2015-05-13 15:40:15',
INTERVAL 1 DAY
)
仍使用自联接优化此查询。
答案 1 :(得分:0)
要获得更快的速度,请将此综合索引添加到g
:
INDEX(olayid, durum, gonderilecegitarih)
请提供SHOW CREATE TABLE 800_emsenaryolar_emgidenbulten
;我想验证您还有adresid
上的索引。