2

I have a left table and a right table and both tables have 1 million records -

CREATE TABLE t_left (
id INT NOT NULL PRIMARY KEY,
value INT NOT NULL)

CREATE TABLE t_right (
id INT NOT NULL PRIMARY KEY,
value INT NOT NULL
)

sample data for t_left table is -

id  value
10000   1
20000   1
30000   1
40000   2
50000   2
60000   2

sample data for t_right table is -

id  value
2   2
3   2
4   2
5   2
6   2

Now I want to find all the rows of t_Left whose column "value" does not exists in column "Value" of table t_right

so my output should be

id  value
10000   1
20000   1
30000   1

Now my question is why sql server is generating different plan when column "Value" of t_left table is NOT NULL and very different plan when column "Value" is null. So as per above table definition my plan for query -

select a.id,a.value 
from dbo.t_left as a
where a.value not in (select b.value from t_right as b)

this is the plan when value column is not null

this is the plan when value column is not null

Now when change the column value from not null to null then I get total different plan. Can someone explain me this plan. Why the table t_right is scanned twice and why row count spool operator is showing in the plan. Which makes the query expensive. I need someone to help me out in understanding the plan in both the cases - when column is not null and when column is null.

alter table t_left
alter column value int null

enter image description here

I know I should be using where notexists when column is null. But why where not in is behaving differently in both the conditions

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
sam
  • 1,242
  • 3
  • 12
  • 31

0 Answers0