SQL Server 性能优化之T-SQL NOT IN 和 NOT Exists

WBOY
풀어 주다: 2016-06-07 15:12:50
원래의
1550명이 탐색했습니다.

这次介绍一下T-SQL中Not IN 和Not Exists的 优化 。 Not IN 和 Not Exists 命令 : 有些情况下,需要select/update/delete 操作孤立数据。孤立数据:不存在主表中而存在其关联表中。 操作这样的数据,一般第一反应是利用Not in 或 Not Exists命令。使用Not IN

这次介绍一下T-SQL中“Not IN” 和“Not Exists”的优化

 

Not IN Not Exists 命令 :

有些情况下,需要select/update/delete 操作孤立数据。孤立数据:不存在主表中而存在其关联表中。

操作这样的数据,一般第一反应是利用“Not in” 或 “Not Exists”命令。使用Not IN会严重影响性能,因为这个命令会逐一检查每个记录,就会造成资源紧张,尤其是当对大数据进行更新和删除操作时,可能导致资源被这些操作锁住。

.

选择NOT IN 还是 NOT Exists

现在SQL Server 中有两个命令可以使用大数据的插入、更新、删除操作,不仅性能方面比NOT IN 和 NOT Exists有很大的提高,而且语法简单,写出来的语句看上去也很清爽。 现在就请它们闪亮登场,Merge 和 Except。

例子:

首先创建两个表

<span class="lnum">   1:  </span><span class="kwrd">use</span> [MyTest]
로그인 후 복사
<span class="lnum">   2:  </span><span class="kwrd">Create</span> <span class="kwrd">table</span> Test1 (name <span class="kwrd">varchar</span> (100) )
로그인 후 복사
<span class="lnum">   3:  </span><span class="kwrd">Create</span> <span class="kwrd">table</span> Test2 (name <span class="kwrd">varchar</span> (100) )
로그인 후 복사

使用Not IN命令Select/update/delete操作:

<span class="lnum">   1:  </span><span class="kwrd">SELECT</span> name <span class="kwrd">FROM</span> Test1 <span class="kwrd">where</span> name <span class="kwrd">not</span> <span class="kwrd">in</span> (<span class="kwrd">select</span> name <span class="kwrd">from</span> Test2)
로그인 후 복사
<span class="lnum">   2:  </span><span class="kwrd">UPDATE</span> Test1 <span class="kwrd">SET</span> name =N<span class="str">'Company_Name'</span> <span class="kwrd">where</span> name <span class="kwrd">not</span> <span class="kwrd">in</span> (<span class="kwrd">select</span> name <span class="kwrd">from</span> Test2)
로그인 후 복사
<span class="lnum">   3:  </span><span class="kwrd">DELETE</span> Test1 <span class="kwrd">FROM</span> Test1 <span class="kwrd">where</span> name <span class="kwrd">not</span> <span class="kwrd">in</span> (<span class="kwrd">select</span> name <span class="kwrd">from</span> Test2)
로그인 후 복사

使用性能更好的Merge and Except

<span class="lnum">   1:  </span>merge Test1 T <span class="kwrd">using</span> (<span class="kwrd">select</span> name <span class="kwrd">from</span> Test1 <span class="kwrd">except</span> <span class="kwrd">select</span> name <span class="kwrd">from</span> Test2 )S <span class="kwrd">on</span> t.name=s.name
로그인 후 복사
<span class="lnum">   2:  </span><span class="kwrd">when</span> matched <span class="kwrd">then</span> <span class="kwrd">update</span> <span class="kwrd">SET</span> name=N<span class="str">'New_Name'</span> ;
로그인 후 복사
<span class="lnum">   3:  </span>merge Test1 T <span class="kwrd">using</span> (<span class="kwrd">select</span> name <span class="kwrd">from</span> Test1 <span class="kwrd">except</span> <span class="kwrd">select</span> name <span class="kwrd">from</span> Test2 )S <span class="kwrd">on</span> t.name=s.name
로그인 후 복사
<span class="lnum">   4:  </span><span class="kwrd">when</span> matched <span class="kwrd">then</span> <span class="kwrd">delete</span> ;
로그인 후 복사
<span class="lnum">   5:  </span><span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> Test1 S <span class="kwrd">where</span> <span class="kwrd">not</span> <span class="kwrd">exists</span> (<span class="kwrd">select</span> 1 <span class="kwrd">from</span> Test1 <span class="kwrd">inner</span> <span class="kwrd">join</span> Test2 <span class="kwrd">on</span> Test1.name=Test2.name <span class="kwrd">and</span> Test1.name=s.name)
로그인 후 복사

注意,上面还是有一部分使用了Not Exists:

<span class="lnum">   1:  </span><span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> Test1 S <span class="kwrd">where</span> <span class="kwrd">not</span> <span class="kwrd">exists</span> (<span class="kwrd">select</span> 1 <span class="kwrd">from</span> Test1 <span class="kwrd">inner</span> <span class="kwrd">join</span> Test2 <span class="kwrd">on</span> Test1.name=Test2.name <span class="kwrd">and</span> Test1.name=s.name)
로그인 후 복사

现在需要使用高效的Except:

<span class="lnum">   1:  </span><span class="kwrd">select</span> name <span class="kwrd">from</span> Test1 <span class="kwrd">except</span> <span class="kwrd">select</span> name <span class="kwrd">from</span> Test2
로그인 후 복사

 

在这里只是给出了例子,没有拿出实际的对比数据。但是Merge 和Except 两个命令在大数据的处理方面的性能,要比

Not IN 和Not EXISTS 好很多。不管你信不信,反正我信了!!!

 

 

在此谢谢读完这篇博客,有什么写的不对的地方请指正

 

有帮助就推荐下,有感想就下下来,不满意就留言,有问题就更正。

 

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿