Beruflich Dokumente
Kultur Dokumente
loop join
by Muthukkumaran kaliyamoorthy Published on: October 5, 2011
Comments: 25 Comments
Tags: Merge join Vs Hash join Vs Nested loop join, Physical join type
Categories:Performance, SQL party
?
create table tableA (id int identity ,name
varchar(50))
declare @i int
set @i=0
while (@i<100)
begin
insert into tableA (name)
select name from master.dbo.spt_values
set @i=@i+1
end
--select COUNT(*) from dbo.tableA --250600
go
create table tableB (id int identity ,name
varchar(50))
declare @i int
set @i=0
while (@i<100)
begin
insert into tableB (name)
select name from master.dbo.spt_values
set @i=@i+1
end
-- select COUNT(*) from dbo.tableB --250600
select * from dbo.tableA A join tableB B
on (a.id=b.id)
If either of the table has indexed then it goes hash join. I havent
shown this picture here. You can drop either of the table indexes
and test it.
Test2: Without Index
Lets create a medium table
?
create table tableC (id int identity,name
varchar(50))
insert into tableC (name)
select name from master.dbo.spt_values
-- select COUNT(*) from dbo.tableC --2506
create table tableD (id int identity,name
varchar(50))
?
create table tableE (id int identity,name
varchar(50))
insert into tableE (name)
select top 10 name from master.dbo.spt_values
-- select COUNT(*) from dbo.tableE --10
create table tableF (id int identity,name
varchar(50))
insert into tableF (name)
select top 10 name from master.dbo.spt_values
?
create
tableE
create
tableF
If either of the table has indexed then it goes Nested loop join. I
havent shown this picture here. You can drop either of the table
indexes and test it.
You can also join tables vice versa like big table Vs Medium table
Vs small table
?
select * from dbo.tableA A join tableC C
on (a.id=C.id)
select * from dbo.tableA A join tableE E
on (a.id=E.id)
select * from dbo.tableC C join tableE E
on (C.id=E.id)
In this case if all the table has indexed then it goes Nested loop
join. If they dont then hash join. If either of the table has
indexed then it goes Nested loop join. I havent shown this
picture here.
Still you can force optimizer to use any one of the internal joins,
but its not good practice. The query optimizer is smart it will
dynamically choose the best one.
Here just I used the merge hint so the optimizer goes to merge
join instead of a hash join (Test1 without an index)
?
select * from dbo.tableA A join tableB B
on (A.id=B.id)option (merge join)
select * from dbo.tableA A inner merge join
tableB B
on (A.id=B.id)
If both the tables have indexes then the query optimizer will
choose Merge (For big tables) /Nested loop (For small
tables) internally.
With index
Table size
(Both)
Big (Both)
HASH
Medium (Both) HASH
NESTED
Small (Both)
LOOP
Big Vs
Small(medium) HASH
Without
Index(Both)
HASH
HASH
NESTED
LOOP
HASH
Either of
table has
index
HASH
HASH
HASH
HASH