Maison > Article > base de données > SQL Server 性能优化之T-SQL NOT IN 和 NOT Exists
这次介绍一下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 好很多。不管你信不信,反正我信了!!!
在此谢谢读完这篇博客,有什么写的不对的地方请指正
有帮助就推荐下,有感想就下下来,不满意就留言,有问题就更正。