SQL INNER JOIN
with OR
: Performance Bottleneck
A recent performance optimization effort highlighted a significant issue: using the OR
operator within an INNER JOIN
condition. The following query exemplifies the problem:
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
This query proved slow. Rewriting it using LEFT JOIN
s dramatically improved performance:
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
The revised query executed in seconds, a substantial improvement. This raises concerns about the general use of OR
in JOIN
conditions.
Why OR
in JOINs Can Be Slow
The core issue is that OR
in JOIN
conditions prevents the SQL Server optimizer from utilizing efficient HASH
or MERGE
joins. These optimized join methods are typically crucial for fast query execution. The OR
condition prevents the server from recognizing the query's equivalence to two separate equijoins:
SELECT * FROM maintable m JOIN othertable o ON o.parentId = m.id UNION SELECT * FROM maintable m JOIN othertable o ON o.id = m.parentId
This forces SQL Server to choose a less efficient execution plan, resulting in slower performance.
Best Practices: Avoid OR
in JOIN
Conditions
While not strictly forbidden, using OR
in JOIN
conditions often hinders optimization and leads to performance degradation. For multiple join conditions, separate equijoins (as shown above with the LEFT JOIN
and UNION
or the LEFT JOIN
with CASE
statement) generally provide superior performance. This allows the query optimizer to leverage its most efficient algorithms.
The above is the detailed content of Is Using 'OR' in INNER JOIN Conditions Always Bad for SQL Performance?. For more information, please follow other related articles on the PHP Chinese website!