在不了解或更改数据模型的情况下,您无能为力
让我们一步一步来,在这个例子中找到一个小的优化:
先把子查询拉出来加入一个join,相信这个改动会在某些版本的SQL上给你更好的性能。它也使查询更清晰。
SELECT dep.*
FROM dep with (NOLOCK)
JOIN ma with (NOLOCK) ON dep.mID = ma.mID
LEFT JOIN ccm with (NOLOCK) ON ccm.cID = dep.cID
LEFT JOIN ctm with (NOLOCK) ON ctm.cID = dep.cID
LEFT JOIN cptgm with (NOLOCK) ON cptgm.cID = dep.cID
JOIN psptgm with (NOLOCK) ON cptgm.ptgID = psptgm.ptgID and psptgm.ib = 1 and psptgm.psID = 145214
WHERE ma.mtID = 3
AND dep.del = 0
AND dep.pub = 1
AND (ccm.cID = 1001 OR (ma.coID = 2 AND ma.cID = 1001))
AND ctm.tID = 2
我也更喜欢这样写(我觉得更清楚):
SELECT dep.*
FROM dep with (NOLOCK)
JOIN ma with (NOLOCK) ON dep.mID = ma.mID and ma.mtID = 3
LEFT JOIN ccm with (NOLOCK) ON ccm.cID = dep.cID
LEFT JOIN ctm with (NOLOCK) ON ctm.cID = dep.cID AND ctm.tID = 2
LEFT JOIN cptgm with (NOLOCK) ON cptgm.cID = dep.cID
JOIN psptgm with (NOLOCK) ON cptgm.ptgID = psptgm.ptgID and psptgm.ib = 1 and psptgm.psID = 145214
WHERE dep.del = 0
AND dep.pub = 1
AND (ccm.cID = 1001 OR (ma.coID = 2 AND ma.cID = 1001))
这使得仅连接修饰符与 where 子句更清晰。
现在很容易在主选择中看到 or 的公共部分并将其拉出(从而减少 or 子句进行的检查次数并提高性能):
WITH prequery AS
(
SELECT dep.*
FROM dep with (NOLOCK)
LEFT JOIN ctm with (NOLOCK) ON ctm.cID = dep.cID AND ctm.tID = 2
LEFT JOIN cptgm with (NOLOCK) ON cptgm.cID = dep.cID
JOIN psptgm with (NOLOCK) ON cptgm.ptgID = psptgm.ptgID and psptgm.ib = 1 and psptgm.psID = 145214
WHERE dep.del = 0 AND dep.pub = 1
)
SELECT dep.*
FROM prequery with (NOLOCK)
JOIN ma with (NOLOCK) ON dep.mID = ma.mID and ma.mtID = 3
LEFT JOIN ccm with (NOLOCK) ON ccm.cID = dep.cID
LEFT JOIN cptgm with (NOLOCK) ON cptgm.cID = dep.cID
WHERE ISNULL(ccm.cID,0) = 1001 OR (ma.coID = 2 AND ma.cID = 1001)