Let's say I have two tables:
Table A
ProdID | PartNumber | Data...
1 | ABC-a | "Data A"
2 | (null) | "Data B"
3 | ABC-c | "Data C"
...
and
Table B
ProdID | PartNumber | DataB
(null) | ABC-a | "Data D"
2 | (null) | "Data E"
3 | (null) | "Data F"
(null) | ABC-z | "Data G"
...
Not ideal, but anyway. I want
ProdID | PartNumber | Data | DataB...
1 | ABC-a | "Data A" | "Data D"
2 | (null) | "Data B" | "Data E"
3 | ABC-c | "Data C" | "Data F"
(null) | ABC-z | (null) | "Data G"
So I use
SELECT *
FROM Table1 T1
RIGHT JOIN Table2 T2 ON
T1.ProdID = T2.ProdID OR T1.PartNumber = T2.PartNumber
Which does exactly what I want, but is seems to take about 100 times as long as either side of the or individually. As part of a more complex query it takes 2 minutes for the OR
compared to <1 second for just the int
and 1 second for just the nvarchar(50)
. Table "A" has ~13k rows, table "b" has ~35k and the whole query returns ~40k.
Query Plans
I think this "Table Spool" may be the problem.
SQL Server 2008 R2 Express. Thoughts?