SQL Server not in statement causes the process to crash _mssql

Source: Internet
Author: User
Two Tables Organizational structure table (organise) and payroll history Sheet (wagepermonthhis)
Two tables are associated by organise.item_id and Wagepermonthhis.orgids
Organise table (hereinafter referred to as O table) about 6,000 records in 11 fields, Wagepermonthhis (hereinafter referred to as W table) has 1.25 million records and 25 fields

The following statement in the original program
is to query all records that have an organizational hierarchy of 2 that is not in the W table
Copy Code code 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= ' and wagesmonth= '
' Group by Orgids,orgnames '
ORDER BY OrgID

Statement execution is 33 seconds long, the server configuration is relatively high: 16 core 4cpu,24g memory, and the memory and CPU in the implementation of the bottleneck does not occur, began to think is (select Orgids from Wagespermonthhis
Where wagesyear= ' and wagesmonth= '
' Group by Orgids,orgnames ' This statement is slow to execute, but it is found that execution is fast, less than 2 seconds out, so the crux comes out, is not in the full scan keyword performance degradation. The most immediate result is a loss of response to the page, A key feature is not available.

Tried the not exist statement, found that the effect is the same, not as the internet can be said to improve a lot of performance.

Then the following is the optimization of the statement
Copy Code code as follows:

Select A.orgid as company code, a.orgname as company name, a.item_id
From organise a
Left OUTER join (SELECT DISTINCT b.orgids from Wagespermonthhis b
Where wagesyear= ' and wagesmonth= ') as B
On a.item_id = B.orgids
where A.orglev = 2
and B.orgids is Null
Order BY Company Code

Use the left outer join (in fact, the left connection can also), the entire statement execution speed of 400ms, 33 seconds and 400ms I think many people did not think of.
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.