亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

今天,手頭上正在作的一個(gè)項(xiàng)目,在生成報(bào)表時(shí),客戶感覺太慢,于是,各處檢查,看可否提示效率。界面上的都改進(jìn)了,提升不大。如是在SQL 語句上下功夫。(我這人比較懶,對(duì)簡(jiǎn)單的語句和查詢都沒有經(jīng)過仔細(xì)優(yōu)化的,一般只對(duì)姚使用left join,outer join,group by 以及carsor的語句會(huì)仔細(xì)寫并用數(shù)據(jù)庫理論考慮和檢查---因?yàn)檫@種語句一般測(cè)試時(shí)如果發(fā)現(xiàn)錯(cuò)誤,檢查和調(diào)試很麻煩)

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

搜索的結(jié)果是,各大論壇,包括MSDN上很多人提出了這個(gè)問題,但回答是眾說紛紜。總體上總結(jié)出來時(shí)說:對(duì)小數(shù)據(jù)量(<N萬)的來說效率幾乎無差異,更有說法說Inner join 和Where只是SQL標(biāo)準(zhǔn)不同,在查詢分析器中SQL Server查詢分析器是將Where直接轉(zhuǎn)換為Join后查詢的。

還是自己來做試驗(yàn)吧。

如是有了如下比較結(jié)果(均在查詢分析器中查詢和計(jì)時(shí)):

語句(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查詢結(jié)果,74行,共時(shí)間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行,時(shí)間0:00:01

后檢查發(fā)現(xiàn)語句(1)中有一個(gè)重復(fù)自查詢條件 :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

所用時(shí)間和結(jié)果都為74行,時(shí)間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

時(shí)間和結(jié)果為74行,時(shí)間0:00:01。

終于發(fā)現(xiàn)了些他們的差異。

結(jié)論: 盡量使用Join 而不是Where來列出關(guān)聯(lián)條件,特別是多個(gè)表聯(lián)合的時(shí)候。原因是: (1)在效率上,Where可能具有和Inner join一樣的效率。但基本可以肯定的(通過SQLServer幫助和其它資料,以及本測(cè)試)是Join的效率不比Where差。 (2)使用Join可以幫助檢查語句中的無效或者誤寫的關(guān)聯(lián)條件

標(biāo)簽: Sql Server 數(shù)據(jù)庫
主站蜘蛛池模板: 日日综合网 | 三级大片在线观看 | 成人午夜大片免费看爽爽爽 | 成人爱爱爱欧美日本视频 | 亚洲一区二区三区首页 | 欧美性生活视频 | 在线成年视频免费观看 | 久久久精品久久久久久 | 日韩高清专区 | 欧美成人精品第一区首页 | 丁香婷婷色综合亚洲小说 | 看日本真人一一级特黄毛片 | 农村三级孕妇视频在线 | 欧美在线性视频 | 一级黄色录像大片 | 欧美精品一区二区三区观 | 热er99久久6国产精品免费 | 国产成人久久777777 | 小明www永久免费播放平台 | 国产精品偷伦视频免费观看的 | 国产精品久久在线 | 亚洲高清免费视频 | 中国女人野外做爰视频在线看 | 青草国产精品久久久久久 | 91国内视频 | 国产三香港三韩国三级不卡 | 日本特级爽毛片叫声 | 国产成人精品三区 | 色综合久久六月婷婷中文字幕 | 久久久久久久免费 | 免费成人黄色片 | 网站啪啪| 成人自拍网 | 国产精品日韩 | 在线中文字幕日韩 | 精品国产麻豆免费网站 | 国产精品视频99 | 黑人巨大系列在线播放 | 青草视频在线观看视频 | 最新国产网址 | 亚洲福利一区二区三区 |