Home Database Mysql Tutorial SQL Server 性能优化之T-SQL NOT IN 和 NOT Exists

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

Jun 07, 2016 pm 03:12 PM
NOT server sql optimization performance

这次介绍一下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]
Copy after login
<span class="lnum">   2:  </span><span class="kwrd">Create</span> <span class="kwrd">table</span> Test1 (name <span class="kwrd">varchar</span> (100) )
Copy after login
<span class="lnum">   3:  </span><span class="kwrd">Create</span> <span class="kwrd">table</span> Test2 (name <span class="kwrd">varchar</span> (100) )
Copy after login

使用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)
Copy after login
<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)
Copy after login
<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)
Copy after login

使用性能更好的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
Copy after login
<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> ;
Copy after login
<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
Copy after login
<span class="lnum">   4:  </span><span class="kwrd">when</span> matched <span class="kwrd">then</span> <span class="kwrd">delete</span> ;
Copy after login
<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)
Copy after login

注意,上面还是有一部分使用了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)
Copy after login

现在需要使用高效的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
Copy after login

 

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

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

 

 

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

 

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

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Performance comparison of different Java frameworks Performance comparison of different Java frameworks Jun 05, 2024 pm 07:14 PM

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

C++ program optimization: time complexity reduction techniques C++ program optimization: time complexity reduction techniques Jun 01, 2024 am 11:19 AM

Time complexity measures the execution time of an algorithm relative to the size of the input. Tips for reducing the time complexity of C++ programs include: choosing appropriate containers (such as vector, list) to optimize data storage and management. Utilize efficient algorithms such as quick sort to reduce computation time. Eliminate multiple operations to reduce double counting. Use conditional branches to avoid unnecessary calculations. Optimize linear search by using faster algorithms such as binary search.

How to optimize the performance of multi-threaded programs in C++? How to optimize the performance of multi-threaded programs in C++? Jun 05, 2024 pm 02:04 PM

Effective techniques for optimizing C++ multi-threaded performance include limiting the number of threads to avoid resource contention. Use lightweight mutex locks to reduce contention. Optimize the scope of the lock and minimize the waiting time. Use lock-free data structures to improve concurrency. Avoid busy waiting and notify threads of resource availability through events.

Performance comparison of Java frameworks Performance comparison of Java frameworks Jun 04, 2024 pm 03:56 PM

According to benchmarks, for small, high-performance applications, Quarkus (fast startup, low memory) or Micronaut (TechEmpower excellent) are ideal choices. SpringBoot is suitable for large, full-stack applications, but has slightly slower startup times and memory usage.

How good is the performance of random number generators in Golang? How good is the performance of random number generators in Golang? Jun 01, 2024 pm 09:15 PM

The best way to generate random numbers in Go depends on the level of security required by your application. Low security: Use the math/rand package to generate pseudo-random numbers, suitable for most applications. High security: Use the crypto/rand package to generate cryptographically secure random bytes, suitable for applications that require stronger randomness.

Performance comparison of C++ with other languages Performance comparison of C++ with other languages Jun 01, 2024 pm 10:04 PM

When developing high-performance applications, C++ outperforms other languages, especially in micro-benchmarks. In macro benchmarks, the convenience and optimization mechanisms of other languages ​​such as Java and C# may perform better. In practical cases, C++ performs well in image processing, numerical calculations and game development, and its direct control of memory management and hardware access brings obvious performance advantages.

What are some ways to resolve inefficiencies in PHP functions? What are some ways to resolve inefficiencies in PHP functions? May 02, 2024 pm 01:48 PM

Five ways to optimize PHP function efficiency: avoid unnecessary copying of variables. Use references to avoid variable copying. Avoid repeated function calls. Inline simple functions. Optimizing loops using arrays.

See all articles