为了提高超慢查询的速度(如果有关系,在SQL Server 2008上的两个表中,每个表只有约50,000行,每分钟要花几分钟),我将问题缩小到OR内部联接中,例如:
SELECT mt.ID, mt.ParentID, ot.MasterID
FROM dbo.MainTable AS mt
INNER JOIN dbo.OtherTable AS ot ON ot.ParentID = mt.ID
OR ot.ID = mt.ParentID
我将其更改为(希望是)一对等效的左联接,如下所示:
SELECT mt.ID, mt.ParentID,
CASE WHEN ot1.MasterID IS NOT NULL THEN
ot1.MasterID ELSE
ot2.MasterID END AS MasterID
FROM dbo.MainTable AS mt
LEFT JOIN dbo.OtherTable AS ot1 ON ot1.ParentID = mt.ID
LEFT JOIN dbo.OtherTable AS ot2 ON ot2.ID = mt.ParentID
WHERE ot1.MasterID IS NOT NULL OR ot2.MasterID IS NOT NULL
..并且查询现在运行大约一秒钟!
将联接置于OR联接条件通常不是一个好主意吗?还是我只是不幸地布置了桌子?
呼啦一阵风
大话西游666
相关分类