成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術文章
文章詳情頁

SQL Server 中Inner join 和where的效率差異

瀏覽:144日期:2023-11-01 17:28:41

今天,手頭上正在作的一個項目,在生成報表時,客戶感覺太慢,于是,各處檢查,看可否提示效率。界面上的都改進了,提升不大。如是在SQL 語句上下功夫。(我這人比較懶,對簡單的語句和查詢都沒有經過仔細優化的,一般只對姚使用left join,outer join,group by 以及carsor的語句會仔細寫并用數據庫理論考慮和檢查---因為這種語句一般測試時如果發現錯誤,檢查和調試很麻煩)

先在網上Google搜索“Join 與 Where 效率”以及察看SQL Server 幫助文檔,希望能獲得“捷徑”些的優化思路。

搜索的結果是,各大論壇,包括MSDN上很多人提出了這個問題,但回答是眾說紛紜。總體上總結出來時說:對小數據量(<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 ,operatorwhere item.itemcode = customer_item.itemCode and customer_item.customerCode =; customer_operator.customerCodeand customer_operator.operatorId =; customer_operator.operatorIdand operator.operatorName like @operatorNameand 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.customerCodeinner join operator on customer_operator.operatorId = operator.operatorIdwhere; operator.operatorName like @operatorNameand 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.customerCodeinner join operator on customer_operator.operatorId = operator.operatorIdwhere; operator.operatorName like @operatorNameand item.deleted = 0 and customer_item.deleted = 0 and customer_operator.deleted = 0and 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 ,operatorwhere item.itemcode = customer_item.itemCode and customer_item.customerCode =; customer_operator.customerCode--and customer_operator.operatorId =; customer_operator.operatorIdand operator.operatorName like @operatorNameand 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可以幫助檢查語句中的無效或者誤寫的關聯條件

標簽: Sql Server 數據庫
主站蜘蛛池模板: 亚洲欧美在线免费观看 | 日韩一区二区三区不卡视频 | 亚洲欧美高清视频 | 欧美精品亚洲精品日韩 | 欧美激情特级黄aa毛片 | 国产精品久久久久久搜索 | 美女被男人桶到嗷嗷叫爽网站 | 久久99视频免费 | 亚洲精品国产综合99久久一区 | 午夜三级国产精品理论三级 | 日韩欧美国产精品 | 久久黄色一级视频 | 人成午夜性刺激免费 | 久草天堂 | 国产精选一区 | 欧美亚洲日本韩国一级毛片 | 久久精品视频免费在线观看 | 国产一级一级毛片 | 免费伦费一区二区三区四区 | 国产精品久久久久久久久免费hd | 日本一区二区三区四区无限 | 欧美自拍另类 | 国产香蕉久久 | 午夜成年女人毛片免费观看 | 国产成人国产在线观看入口 | 一级一片一a一片 | 国产在线视频精品视频免费看 | 国产国产成人精品久久 | 美女张开腿让男人操 | 欧美成年人视频 | 美女操穴视频 | 精品亚洲成a人片在线观看 精品亚洲成a人在线播放 | 亚洲性在线 | 亚洲成av人片在线观看 | 日本一在线中文字幕天堂 | 日本红怡院在线 | 福利岛国深夜在线 | 亚洲怡红院在线 | 国产成人禁片免费观看视频 | 97久久精品午夜一区二区 | 久久永久免费视频 |