简体   繁体   中英

JOIN with OR - Query Planner Choosing Nested Loops

I am wondering why does MS SQL Server Query Planner create nested loops rather than choosing a Union for a JOIN with OR Conditions.

Note: From searching on SO, does not seem to be MSSQL specific

Eg

SELECT * FROM TableA a
JOIN TableB b
ON a.One = b.One 
OR a.Two = b.Two

Is taking 6 minutes in my case (Both One and Two are indexed on both tables)

But

SELECT * FROM TableA a
JOIN TableB b
ON a.One = b.One

UNION -- Not ALL, as need to remove duplicates

SELECT * FROM TableA a
JOIN TableB b
ON a.Two = b.Two

takes 2 seconds.

I know the reason why the first is taking so long (because of the nested loop, where as the 2 unions use the indexes), but I am wondering why does the query planner not choose UNION as the execution plan?

Is there some sort of caveat I need to be aware of when using UNION for which it does not use it?

Why is this logic not implemented into the query planner?

Is it just to keep the code of query planner simpler (As its probably already pretty complex), something that they haven't got round to optimizing or because there is some other caveat that I am not aware of?

Is this a selectivity issue? SQL Server likes indexes to be highly selective. One predicate will always be considered more selective than two predicates OR'd and the difference in your case may be the difference between using the indexes or not.

See Bart Duncan's SQL Weblog Query Tuning Fundamentals: Density, Predicates, Selectivity, and Cardinality for a lengthy writeup on selectivity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM