表格(简体)
媒体表
mediaID description multimediaGroupID silolID fcsPathHigh fcsPathLow
----------------------------------------------------------------------------------
1 media1 11 6 blah blah
2 media2 12 6 blah blah
MultimediaGroup表
multimediaGroupID multimediaGroup isPollGroup
------------------------------------------------
11 You be the ref 1
12 Try of the week 1
得到了这个查询,我知道这不是最优的。我不喜欢它中的子查询。
SELECT top 30 *
FROM media
WHERE (remoteMedia = 1) AND multimediaGroupID <> 13 AND siloID <> 16
AND siloID = 1 AND (fcsPathHigh like '%.flv' AND fcsPathLow like '%.flv')
AND (multimediagroupid is null or multimediagroupid not in
(select multimediagroupid
from multimediagroups
where ispollgroup = 1))
ORDER BY dateUploaded DESC
任何人都可以建议一种方法来优化它,而不需要以下子查询部分:
and (multimediagroupid is null
or multimediagroupid not in
(select multimediagroupid
from multimediagroups
where ispollgroup = 1))
我也在考虑使用WITH(NOLOCK)来加速它,因为它有时会非常慢,并且有可能导致服务器崩溃。
答案 0 :(得分:3)
让你感到不安的部分可以转换为LEFT JOIN,如下所示:
SELECT TOP 30 m.*
FROM media m
LEFT JOIN multimediagroups g ON m.multimediagroupid = g.multimediagroupid
AND g.ispollgroup = 1
WHERE m.remoteMedia = 1
AND m.multimediaGroupID <> 13
AND m.siloID <> 16
AND m.siloID = 1
AND (m.fcsPathHigh like '%.flv' AND m.fcsPathLow like '%.flv')
AND g.multimediagroupid IS NULL
ORDER BY m.dateUploaded DESC
答案 1 :(得分:0)
我不相信子查询会导致问题,就像在子查询上执行Not一样。我确实有一些想法可能会有所帮助:
对于索引,我将添加以下内容: