左鏈接與子查詢效率比較

經測試 左鏈接的效率要高很多,子查詢要建臨時表 

測試轉自https://bbs.csdn.net/topics/391030135?page=1

create table A (id char(10),fname varchar(20))
go
create table B (id char(10),fname varchar(20))
go
declare @i int
set @i=0
while @i<=100000
begin
    insert into A (id,fname) values (convert(char(10),@i),'A表數據'+convert(varchar(20),@i))
    insert into B (id,fname) values (convert(char(10),@i),'B表數據'+convert(varchar(20),@i))
    set @i=@i+1
end
go
select getdate()
select * ,(select fname from B where id=A.id)
from A where CONVERT(int,id) between 50000 and 50100
select getdate()
select a.*,b.fname
from a left join b on a.id=b.id
where CONVERT(int,a.id) between 50000 and 50100
select getdate()
select a.*,b.fname
from a , b 
where a.id=b.id and CONVERT(int,a.id) between 50000 and 50100
select getdate()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章