Merge into SQL optimization

Source: Internet
Author: User

Today, the Netizen said his merge into SQL ran for 15 minutes did not run out of data, asked if I can optimize, I let him put SQL and SQL execution plan sent over

Merge into F_sal_p_camp_samp_cust_data A
Using (select cust_id,
CUST_MNGR_ID,
Orgcode from (
SELECT t.cust_id,
T.CUST_MNGR_ID,
T.orgcode,
Row_number () over (PARTITION by t.cust_id ORDER by T.belg_rela_type asc,t.open_percent DESC) as RN
From F_sal_p_camp_samp_cust_data A
INNER JOIN (
SELECT T.cust_id,t.cust_mngr_id,t.belg_rela_type,t.open_percent,om.orgseq,om.orgcode
From O_f_pty_p_cust_belg_info T
INNER JOIN Om_employee EMP
On t.cust_mngr_id = EMP. Empcode
And Emp.empname not a like '% virtual% '
INNER JOIN Ac_operator OPR
On emp.operatorid = Opr.operatorid
and Opr.status in (' Running ', ' openuse ')
INNER JOIN om_organization OM
On emp.orgadminid = Om.orgid
) T on a.cust_id=t.cust_id
where a.activity_id = ' 20150114-1003 ' and t.orgseq like '. 10001. ' | | ' %‘
and a.used_org = ' BS001 '
and A.execute_man is null
) Where rn = 1
) T1
On (a.cust_id = t1.cust_id and a.activity_id = ' 20150114-1003 ')
When matched then
Update
Set A.execute_man = T1. CUST_MNGR_ID,
A.task_stat = ' 3 ',
a.used_org = T1.orgcode
WHERE a.activity_id = ' 20150114-1003 '

5-filter ("RN" =1)

6-filter (Row_number () over (PARTITION by "T". " cust_id "ORDER by
"T". " Belg_rela_type ", Internal_function (" T "." Open_percent ") DESC) <=1)
12-filter ("A". ") used_org "= ' BS001 ' and" A "." Execute_man "is NULL)
13-access ("A". ") activity_id "= ' 20150114-1003 ')
15-access ("A". ") cust_id "=" T "." cust_id ")
16-filter ("EMP"). EmpName "Not like '% virtual% ' and" EMP "." EmpName "is not NULL)
17-access ("T". " cust_mngr_id "=" EMP "." Empcode ")
18-filter ("OPR". STATUS "= ' openuse ' OR" OPR "." STATUS "= ' running ')
19-access ("EMP"). Operatorid "=" OPR "." Operatorid ")
20-access ("EMP"). Orgadminid "=" OM "." ORGID ")
21-filter ("OM". " Orgseq "like '. 10001.% ')
22-access ("A". ") activity_id "= ' 20150114-1003 ')
23-filter ("A". ") cust_id "=" cust_id ")

Looked at this execution plan, found all NL connections, I asked him to query the corresponding number of rows returned by SQL

F_sal_p_camp_samp_cust_data return 20w

Select COUNT (*) from O_f_pty_p_cust_belg_info T; 369983
Select COUNT (*) from Om_employee EMP where emp.empname '% virtual%; 6353
Select COUNT (*) from Ac_operator OPR where Opr.status in (' Running ', ' openuse '); 3234
Select COUNT (*) from om_organization OM; 1079

found that the number of returned rows, go to NL connection must have a problem, should go hash join, and the implementation of the plan to see the statistics is not right

Because the netizen is development does not have DBA authority, therefore cannot re-statistic information, so can only use hint, added hint forced to go hash, run time is 19 seconds, Netizen said can, I help him to see

Merge/*+ Use_hash (A,T1) leading (a) */into F_sal_p_camp_samp_cust_data A
Using (select cust_id,
CUST_MNGR_ID,
Orgcode from (
SELECT/*+ Use_hash (a,t) leading (A) */t.cust_id,
T.CUST_MNGR_ID,
T.orgcode,
Row_number () over (PARTITION by t.cust_id ORDER by T.belg_rela_type asc,t.open_percent DESC) as RN
From F_sal_p_camp_samp_cust_data A
INNER JOIN (
SELECT T.cust_id,t.cust_mngr_id,t.belg_rela_type,t.open_percent,om.orgseq,om.orgcode
From O_f_pty_p_cust_belg_info T
INNER JOIN Om_employee EMP
On t.cust_mngr_id = EMP. Empcode
And Emp.empname not a like '% virtual% '
INNER JOIN Ac_operator OPR
On emp.operatorid = Opr.operatorid
and Opr.status in (' Running ', ' openuse ')
INNER JOIN om_organization OM
On emp.orgadminid = Om.orgid
) T on a.cust_id=t.cust_id
where a.activity_id = ' 20150114-1003 ' and t.orgseq like '. 10001. ' | | ' %‘
and a.used_org = ' BS001 '
and A.execute_man is null
) Where rn = 1
) T1
On (a.cust_id = t1.cust_id and a.activity_id = ' 20150114-1003 ')
When matched then
Update
Set A.execute_man = T1. CUST_MNGR_ID,
A.task_stat = ' 3 ',
a.used_org = T1.orgcode
WHERE a.activity_id = ' 20150114-1003 '

This SQL is nested in the middle of a lot of layers, this is the middle layer, this SQL ran 2:30 seconds

SELECT T.cust_id,t.cust_mngr_id,t.belg_rela_type,t.open_percent,om.orgseq,om.orgcode
From O_f_pty_p_cust_belg_info T
INNER JOIN Om_employee EMP
On t.cust_mngr_id = EMP. Empcode
And Emp.empname not a like '% virtual% '
INNER JOIN Ac_operator OPR
On emp.operatorid = Opr.operatorid
and Opr.status in (' Running ', ' openuse ')
INNER JOIN om_organization OM
On emp.orgadminid = Om.orgid

Select COUNT (*) from O_f_pty_p_cust_belg_info T; 369983
Select COUNT (*) from Om_employee EMP where emp.empname '% virtual%; 6353
Select COUNT (*) from Ac_operator OPR where Opr.status in (' Running ', ' openuse '); 3234
Select COUNT (*) from om_organization OM; 1079

Look at this execution plan, look at the number of rows returned by SQL, found very good, go to the hash of the connection is right, then why go slow, because all the table go is full table scan, no walk index, but can walk index is small table, no impact,

The real impact is the T table, t Table 30 W, and there is no filter criteria, only to rewrite the SQL

With X as (select/*+ materialize Parallel (t,6) */t.cust_id,t.cust_mngr_id,t.belg_rela_type,t.open_percent from O_f_pty _p_cust_belg_info T)
SELECT X.cust_id,x.cust_mngr_id,x.belg_rela_type,x.open_percent,om.orgseq,om.orgcode
From X
INNER JOIN Om_employee EMP
On x.cust_mngr_id = EMP. Empcode
And Emp.empname not a like '% virtual% '
INNER JOIN Ac_operator OPR
On emp.operatorid = Opr.operatorid
and Opr.status in (' Running ', ' openuse ')
INNER JOIN om_organization OM
On emp.orgadminid = Om.orgid

1 minutes after the SQL rewrite, it's a lot quicker.

The entire SQL run, in about 10 seconds, change to here.

Merge into SQL optimization

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.