SQL Server 中Inner join 和where的效率差異
總結出來時說:對小數據量(<N萬)的來說效率幾乎無差異,更有說法說Inner join 和Where只是SQL標准不同,在查詢分析器中SQL Server查詢分析器是將Where直接轉換為Join后查詢的。
如是有了如下比較結果(均在查詢分析器中查詢和計時):
語句(1)
declare @operatorName nvarchar(50)
set @operatorName = '%'
select distinct item.* from item , customer_item , customer_operator ,operator
where item.itemcode = customer_item.itemCode
and customer_item.customerCode = customer_operator.customerCode
and customer_operator.operatorId = customer_operator.operatorId
and operator.operatorName like @operatorName
and item.deleted = 0 and customer_item.deleted = 0 and customer_operator.deleted = 0
查詢結果,74行,共時間0:00:04
語句(2)
declare @operatorName nvarchar(50)
set @operatorName = '%'
select distinct item.* from item inner join customer_item
on item.itemcode = customer_item.itemCode
inner join customer_operator on customer_item.customerCode = customer_operator.customerCode
inner join operator on customer_operator.operatorId = operator.operatorId
where operator.operatorName like @operatorName
and item.deleted = 0 and customer_item.deleted = 0 and customer_operator.deleted = 0
共74行,時間0:00:01
后檢查發現語句(1)中有一個重復自查詢條件 :customer_operator.operatorId = customer_operator.operatorId
將其葉加到語句2中,語句(3)
declare @operatorName nvarchar(50)
set @operatorName = '%'
select distinct item.* from item inner join customer_item
on item.itemcode = customer_item.itemCode
inner join customer_operator on customer_item.customerCode = customer_operator.customerCode
inner join operator on customer_operator.operatorId = operator.operatorId
where operator.operatorName like @operatorName
and item.deleted = 0 and customer_item.deleted = 0 and customer_operator.deleted = 0
and customer_operator.operatorId = customer_operator.operatorId
所用時間和結果都為74行,時間0:00:01。
將語句(1)中的去掉該條件后成為語句(4)
declare @operatorName nvarchar(50)
set @operatorName = '%'
select distinct item.* from item , customer_item , customer_operator ,operator
where item.itemcode = customer_item.itemCode
and customer_item.customerCode = customer_operator.customerCode
--and customer_operator.operatorId = customer_operator.operatorId
and operator.operatorName like @operatorName
and item.deleted = 0 and customer_item.deleted = 0 and customer_operator.deleted = 0
時間和結果為74行,時間0:00:01。
終於發現了些他們的差異。
結論:
盡量使用Join 而不是Where來列出關聯條件,特別是多個表聯合的時候。
原因是:
(1)在效率上,Where可能具有和Inner join一樣的效率。但基本可以肯定的(通過SQLServer幫助和其它資料,以及本測試)是Join的效率不比Where差。
(2)使用Join可以幫助檢查語句中的無效或者誤寫的關聯條件