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 2
Copy 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.