SQL Server not in statement causes the process to crash

Source: Internet
Author: User

Two organizational structure tables (Organise) and wage distribution History Table (WagePerMonthHis)
The two tables are associated through Organise. Item_id and WagePerMonthHis. OrgIdS.
In the Organise table (hereinafter referred to as the O table), there are about 6000 records and 11 fields. The WagePerMonthHis table (hereinafter referred to as the W table) has 1.25 million records and 25 fields.

The following statement in the original program
Is to query all records not in the W table at the organizational structure level of 2Copy codeThe Code is as follows: select OrgId as company code, OrgName as company name
From Organise
Where OrgLev = 2
And item_id not in
(Select OrgidS from WagesPerMonthHis
Where WagesYear = '20140901' and WagesMonth =
'01' Group by OrgidS, OrgNameS)
Order by Orgid

The statement execution takes 33 seconds, and the server configuration is relatively high: 16 core, 4 CPU, 24 GB memory, and there is no bottleneck between the memory and CPU during execution.
Where WagesYear = '20140901' and WagesMonth =
'01' Group by OrgidS, OrgNameS) This statement is slow to execute, but it is found that the execution speed is very fast, less than 2 seconds to come out, so the crux of the problem is coming out, is not in this full scan keyword to bring about performance degradation. the most direct cause is that the page does not respond, and a key function cannot be used.

I tried the not exist statement and found that the effect is the same, which does not improve a lot of performance as mentioned on the Internet.

The optimization statement is as follows:Copy codeThe Code is as follows: select a. OrgId as company code, a. OrgName as company name, a. item_id
From Organise
Left outer join (select distinct B. OrgIdS from WagesPerMonthHis B
Where WagesYear = '000000' and WagesMonth = '01') as B
On a. item_id = B. OrgidS
Where a. OrgLev = 2
And B. OrgIdS is Null
Order by company code

After switching to the left outer connection (in fact, the left connection can also be used), the execution speed of the entire statement is 400 ms, 33 S and 400 ms. I think many did not expect this.

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.