2016-05-12 32 views
0
SELECT product_id FROM inventories 
INTERSECT 
SELECT product_id FROM order_items; 

SELECT product_id FROM inventories T1 
    inner join order_items t2 on T1.product_id = T2.product_id; 
+2

稍有不同的結果取決於我的系統上如果重複。你有沒有比較執行計劃? – jarlh

+0

在我的項目中我想執行查詢從數據庫中獲取數據。使用set操作符,子查詢或連接更快地查詢 – suhas

+0

學習使用可用的工具!對未來非常瞭解! – jarlh

回答

0

我做了一些測試,出於好奇

摘要:
1.採用了索引,設置查詢稍微高效查詢時的成本和執行時間作爲參數
2.具有指標設置選項效率比加入基於查詢時,查詢成本,讀取次數,採取的執行時間爲參數

演示腳本:

drop table #test2 

    create table #test1 
    (
    id int 
    ) 

    create table #test2 
    (
    id int 
    ) 

    insert into #test1 
    select * from numbers 


insert into #test2 
select * from numbers where id>10 


set statistics io on 


create index nxc on #test1(id) 
create index nxc2 on #test2(id) 


select id from #test1 
intersect 
select id from #test2 

select 
* from 
#test1 t1 
join 
#test2 t2 
on t1.id=t2.id 

執行兩個查詢了很多次:

使用索引

enter image description here

在數量方面查詢成本寫着:

表'#test2 ______________________________________________________________________________________________________ 000000000028'。掃描計數1,邏輯讀取2239,物理讀取0,預讀讀取0,lob邏輯讀取0,lob物理讀取0,lob預讀取讀數0. 表'#test1 ______________________________________________________________________________________________ 000000000027'。掃描計數1,邏輯讀取2239,物理讀取0,預讀0,lob邏輯讀取0,lob物理讀取0次,lob預讀0

問題2:使用聯接

Table'#test2 ______________________________________________________________________________________________________ 000000000028'。掃描計數5,邏輯讀取1609,物理讀取0,預讀讀取0,lob邏輯讀取0,lob物理讀取0,lob預讀取讀取0. 表'#test1 ______________________________________________________________________________________________________ 000000000027'。掃描計數5,邏輯讀取1610,物理讀取0,預讀讀取0,lob邏輯讀取0,lob物理讀取0,lob預讀讀取0.

更改連接查詢以使用maxdop 1併合並兩個查詢連接提示結果的讀取次數相同,但當並行執行兩個查詢時,使用set選項的查詢只佔總成本的3%。

隨着出指標:大量的讀取都比較相同
enter image description here

注:
產生這種測試和執行計劃是基於幾個因素

相關問題