SQL Server not in optimization

Source: Internet
Author: User

Using EXISTS (or not EXISTS) will generally improve the efficiency of the query, because the NOT in clause performs a full table traversal of the tables in the subquery.

During the execution of the in subquery, Oracle executes the subquery results into the temporary table before the main query;Instead, exists runs the main query, and then executes the subquery to find the first match.such as: Query a table does not have and B or C table connected data Select A.id,a.reads,a.addtimeFrom Awhere a.person_id not in (select user_id from B)or a.worksite_id not in (select ID from C)ORDER BY a.addtime Desc Program Execution Time: 40109.38 Ms Select A.id,a.reads,a.addtimeFrom Awhere not exists (select ID from B where b.user_id=a.person_id)or not exists (select ID from C where c.id=a.worksite_id)ORDER BY sendorder.addtime Desc Program Execution time: 8531.25 Ms

Ms sSQL Instance:

Selectsubscriber_id fromNewslettersystem_campaigncategorysubscriber CCS with(NOLOCK)where  not EXISTS(SELECT  distinctsubscriber_id fromNewslettersystem_opentracking OT with(NOLOCK)WHEREot.subscriber_id=ccs.subscriber_id andot.campaign_id= 52801) andccs.campaign_id= 52801  andccs.status_id= 1

SQL Server not in optimization

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.