How to tune the below query?
SELECT /* + ALL_ROWS */
distinct
--(select name from team where team_id in (select team_id from alignment where alignment_id=a.alignment_id))as team_name,
(select name from team where team_id=a.team_id) as team_name,
e.employee_id, --REP EMPLOYEE_ID
c.external_id_2,
c.name || ','|| c.first_name as customer_name,
c.customer_sub_type, -- spcialty
event.ENTRY_DATE ,
CASE
WHEN event.EVENT_TYPE='C' and event.APPOINTMENT_TYPE='GA' THEN 'Via Appointment'
WHEN event.EVENT_TYPE='C' and event.APPOINTMENT_TYPE is null THEN 'Direct Call'
ELSE '-'
END EVENT_TYPE,
event.Event_id,
rating.rating_category, -- RATING CATEGORY
rating.call_frequency,
adr.line_1_address address1, -- address1
adr.line_2_address address2, --address2
adr.city, --city
adr.state, --state
adr.geography_code_1, -- brick name
event.start_date_time, --Start date
event.status_change_date,
nt.note_text call_objective,--next call OBJECTIVE
ed.text_1 as Call_Objective_results,
(select display_name from product where product_id in(select max(product_id) from event_detail where sequence=1 and event_id=event.event_id)) as product_1,
(select display_name from product where product_id in(select max(product_id) from event_detail where sequence=2 and event_id=event.event_id)) as product_2,
(select display_name from product where product_id in(select max(product_id) from event_detail where sequence=3 and event_id=event.event_id)) as product_3,
(select display_name from product where product_id in (
(select max(product_id) from event_item where product_id in (ei.product_id) ))) || ' , '|| ei.quantity as promotinal_items_type_quantity,
(select display_name from product where product_id in (
(select max(product_id) from sample_transaction where product_id not in (ei.product_id) and event_id=event.event_id)))|| ' , '||
(select physical_quantity from sample_transaction where product_id in (
(select max(product_id) from product where product_id not in (ei.product_id) and event_id=event.event_id)) ) as pob_product_quantity,
event.accompanied_by,
--(select ACCOMPANIED_BY from event ev where tenant_id=500020 and employee_id in (::employee_id) and ev.event_id=event.event_id) Accompanied_BY,
event.status,
event.end_date_time FROM employee e
inner join alignment a on e.employee_id=a.employee_id
inner join
(
select event_id,alignment_id,employee_id,affiliation_id,customer_id, interaction_channel, start_date_time,end_date_time,STATUS_CHANGE_DATE,
ENTRY_DATE ,EVENT_TYPE,APPOINTMENT_TYPE,
status,accompanied_by from event ev where
ev.event_type in (::event_type) and tenant_id=500020 and start_date_time>=:start_date AND end_date_time<=:end_date and
ev.employee_id in
(
select employee_id from alignment where manager_alignment_id in
(
select alignment_id from alignment where manager_alignment_id in
(
select alignment_id from alignment where employee_id in
(
::employee_id
)
)
) or manager_alignment_id in(
select alignment_id from alignment where employee_id in
(
::employee_id
)
)
or alignment_id in(
select alignment_id from alignment where employee_id in
(
::employee_id
)
)
) and ev.status in (::status) or (ev.tenant_id=500020 and 'VPOS'=(select distinct role from alignment where employee_id in ( ::employee_id ) AND role='VPOS' ) and ev.event_type in (::event_type) and start_date_time>=:start_date AND end_date_time<=:end_date)
)event on a.employee_id=event.employee_id and event.interaction_channel ='INPR'
inner join affiliation aff on event.affiliation_id=aff.affiliation_id
left outer join event_dynamic ed on event.event_id=ed.event_id
left outer join event_item ei on ei.event_id=event.event_id
inner join customer c on c.customer_id=event.customer_id and c.customer_type IN ('PRES','NPRS')
left outer join address adr on aff.address_id=adr.address_id
left outer join notes nt on event.event_id=nt.event_id and nt.customer_id=c.customer_id and note_type='CC'
left outer join
(
select dnr.dn_rating_attribute_id,r.team_id,
r.customer_id,
(select note_text from notes where
CUSTOMER_ID=r.customer_id
and note_type='CC' and event_id IS null) nextcall_objective,
r.user_account_id,
r.value_min,
case
when r.value_min = '1' then 'A'
when r.value_min= '2' then 'B'
when r.value_min = '3' then 'C'
end RATING_CATEGORY
from dn_rating_attribute dnr
inner join rating r on r.dn_rating_attribute_id=dnr.dn_rating_attribute_id
where dnr.external_id_1='IND_CATEGORY'
) rating on c.customer_id=rating.customer_id and rating.team_id in ( select team_id from alignment where employee_id=event.employee_id )
left outer join
(
select dnr.dn_rating_attribute_id,r.team_id,
r.customer_id,
r.user_account_id,
r.value_min,
case
when r.value_min = '1' then '1'
when r.value_min= '2' then '2'
when r.value_min = '3' then '3'
end Call_frequency
from dn_rating_attribute dnr
inner join rating r on r.dn_rating_attribute_id=dnr.dn_rating_attribute_id
where dnr.external_id_1='IND_CALLFREQUENCY'
) rating on c.customer_id=rating.customer_id and rating.team_id in ( select team_id from alignment where employee_id=event.employee_id )
It is taking long time to fetch the records. I am not sure where it is taking long time. Shall I use any hints for this.
Here is the Execution Plan
0 SELECT STATEMENT 36114 11M 503880 12G 503424
1 0 INDEX RANGE SCAN PEUSPMMI.TEAM_PF1 1 19 1 7321 1
2 0 TABLE ACCESS BY INDEX ROWID PEUSPMMI.PRODUCT 1 25 2 15463 2
3 2 INDEX UNIQUE SCAN PEUSPMMI.PRODUCT_PK 1 1 8171 1
4 3 SORT AGGREGATE 1 21
5 4 TABLE ACCESS BY INDEX ROWID PEUSPMMI.EVENT_DETAIL 1 21 4 29817 4
6 5 INDEX RANGE SCAN PEUSPMMI.EVENT_DETAIL_PF2 2 3 21964 3
7 0 TABLE ACCESS BY INDEX ROWID PEUSPMMI.PRODUCT 1 25 2 15463 2
8 7 INDEX UNIQUE SCAN PEUSPMMI.PRODUCT_PK 1 1 8171 1
9 8 SORT AGGREGATE 1 21
10 9 TABLE ACCESS BY INDEX ROWID PEUSPMMI.EVENT_DETAIL 1 21 4 29817 4
11 10 INDEX RANGE SCAN PEUSPMMI.EVENT_DETAIL_PF2 2 3 21964 3
12 0 TABLE ACCESS BY INDEX ROWID PEUSPMMI.PRODUCT 1 25 2 15463 2
13 12 INDEX UNIQUE SCAN PEUSPMMI.PRODUCT_PK 1 1 8171 1
14 13 SORT AGGREGATE 1 21
15 14 TABLE ACCESS BY INDEX ROWID PEUSPMMI.EVENT_DETAIL 1 21 4 29817 4
16 15 INDEX RANGE SCAN PEUSPMMI.EVENT_DETAIL_PF2 2 3 21964 3
17 0 TABLE ACCESS BY INDEX ROWID PEUSPMMI.PRODUCT 1 25 2 15463 2
18 17 INDEX UNIQUE SCAN PEUSPMMI.PRODUCT_PK 1 1 8171 1
19 18 SORT AGGREGATE 1 10
20 19 FIRST ROW 68 680 1 21571 1
21 20 INDEX RANGE SCAN (MIN/MAX) PEUSPMMI.EVENT_ITEM_FK2 68 680 1 21571 1
22 0 TABLE ACCESS BY INDEX ROWID PEUSPMMI.PRODUCT 1 25 2 15463 2
23 22 INDEX UNIQUE SCAN PEUSPMMI.PRODUCT_PK 1 1 8171 1
24 23 SORT AGGREGATE 1 19
25 24 TABLE ACCESS BY INDEX ROWID PEUSPMMI.SAMPLE_TRANSACTION 1 19 2 15974 2
26 25 INDEX RANGE SCAN PEUSPMMI.SAMPLE_TRANSACTION_FK4 1 1 8371 1
27 0 FILTER
28 27 TABLE ACCESS FULL PEUSPMMI.SAMPLE_TRANSACTION 37771 848K 205 18M 204
29 27 SORT AGGREGATE 1 9
30 29 FILTER
31 30 FIRST ROW 1 9 2 14493 2
32 31 INDEX FULL SCAN (MIN/MAX) PEUSPMMI.PRODUCT_PK 1 9 2 14493 2
33 0 HASH UNIQUE 36114 11M 503880 12G 503424 11M
34 33 NESTED LOOPS OUTER 36114 11M 501364 12G 500912
35 34 NESTED LOOPS OUTER 36114 10M 284619 11G 284228
36 35 HASH JOIN OUTER 36114 10M 67875 9G 67544 8576K
37 36 VIEW 36114 8146K 53266 8G 52971
38 37 FILTER
39 38 HASH JOIN RIGHT OUTER 36114 10M 53263 8G 52969
40 39 TABLE ACCESS FULL PEUSPMMI.EVENT_ITEM 7860 168K 17 2692444 17
41 39 NESTED LOOPS 36114 10M 53246 8G 52952
42 41 HASH JOIN 36114 9980K 53243 8G 52952
43 42 TABLE ACCESS FULL PEUSPMMI.ALIGNMENT 950 16K 10 816335 10
44 42 HASH JOIN OUTER 36114 9345K 53233 8G 52942 8504K
45 44 HASH JOIN OUTER 35521 8082K 45773 7G 45525 5920K
46 45 HASH JOIN 35445 5503K 41799 6G 41569 3992K
47 46 HASH JOIN 35484 3569K 30983 4G 30814 3368K
48 47 TABLE ACCESS FULL PEUSPMMI.EVENT 35530 2949K 27495 4G 27343
49 47 INDEX FAST FULL SCAN PEUSPMMI.AFFILIATION_PF12 1342K 23M 1412 195M 1405
50 46 TABLE ACCESS FULL PEUSPMMI.CUSTOMER 706K 37M 8346 1G 8292
51 45 TABLE ACCESS FULL PEUSPMMI.ADDRESS 303K 21M 2449 431M 2434
52 44 TABLE ACCESS FULL PEUSPMMI.EVENT_DYNAMIC 1255K 38M 4426 906M 4394
53 41 INDEX UNIQUE SCAN PEUSPMMI.EMPLOYEE_PK 1 9 0 1900 0
54 38 FILTER
55 54 TABLE ACCESS BY INDEX ROWID PEUSPMMI.ALIGNMENT 1 23 2 15640 2
56 55 INDEX UNIQUE SCAN PEUSPMMI.ALIGNMENT_UK1 1 1 8171 1
57 54 NESTED LOOPS
58 57 NESTED LOOPS 1 31 3 23674 3
59 58 INDEX RANGE SCAN PEUSPMMI.ALIGNMENT_PF2 1 15 2 14463 2
60 58 INDEX UNIQUE SCAN PEUSPMMI.ALIGNMENT_UK1 1 0 1900 0
61 57 TABLE ACCESS BY INDEX ROWID PEUSPMMI.ALIGNMENT 1 16 1 9211 1
62 54 TABLE ACCESS BY INDEX ROWID PEUSPMMI.ALIGNMENT 1 16 2 15533 2
63 62 INDEX UNIQUE SCAN PEUSPMMI.ALIGNMENT_UK1 1 1 8171 1
64 54 TABLE ACCESS BY INDEX ROWID PEUSPMMI.ALIGNMENT 1 16 2 15533 2
65 64 INDEX UNIQUE SCAN PEUSPMMI.ALIGNMENT_UK1 1 1 8171 1
66 38 TABLE ACCESS BY INDEX ROWID PEUSPMMI.ALIGNMENT 1 12 2 15693 2
67 66 INDEX UNIQUE SCAN PEUSPMMI.ALIGNMENT_UK1 1 1 8171 1
68 36 TABLE ACCESS FULL PEUSPMMI.NOTES 1138K 89M 9125 700M 9100
69 35 VIEW 1 6 6 47959 6
70 69 NESTED LOOPS
71 70 NESTED LOOPS 1 75 6 47959 6
72 71 MERGE JOIN CARTESIAN 1 52 3 25134 3
73 72 TABLE ACCESS BY INDEX ROWID PEUSPMMI.DN_RATING_ATTRIBUTE 1 34 2 15903 2
74 73 INDEX RANGE SCAN PEUSPMMI.DN_RATING_ATTRIBUTE_PF2 1 1 8171 1
75 72 BUFFER SORT 1 18 1 9231 1
76 75 TABLE ACCESS BY INDEX ROWID PEUSPMMI.ALIGNMENT 1 18 1 9231 1
77 76 INDEX UNIQUE SCAN PEUSPMMI.ALIGNMENT_UK1 1 0 1900 0
78 71 INDEX RANGE SCAN PEUSPMMI.RATING_UK1 1 2 15313 2
79 70 TABLE ACCESS BY INDEX ROWID PEUSPMMI.RATING 1 23 3 22824 3
80 34 VIEW 1 6 6 47959 6
81 80 NESTED LOOPS
82 81 NESTED LOOPS 1 75 6 47959 6
83 82 MERGE JOIN CARTESIAN 1 52 3 25134 3
84 83 TABLE ACCESS BY INDEX ROWID PEUSPMMI.DN_RATING_ATTRIBUTE 1 34 2 15903 2
85 84 INDEX RANGE SCAN PEUSPMMI.DN_RATING_ATTRIBUTE_PF2 1 1 8171 1
86 83 BUFFER SORT 1 18 1 9231 1
87 86 TABLE ACCESS BY INDEX ROWID PEUSPMMI.ALIGNMENT 1 18 1 9231 1
88 87 INDEX UNIQUE SCAN PEUSPMMI.ALIGNMENT_UK1 1 0 1900 0
89 82 INDEX RANGE SCAN PEUSPMMI.RATING_UK1 1 2 15313 2
90 81 TABLE ACCESS BY INDEX ROWID PEUSPMMI.RATING 1 23 3 22824 3
Please do the needful.
I am using Oracle 11g.
"I am not sure where it is taking long time."
There's no need to guess. Use Real-Time SQL Monitoring to find out exactly which operations are running the longest. (If you continue to use the package it may require additional licensing, but you should at least evaluate it.)
select dbms_sqltune.report_sql_monitor(sql_id => 'your_id', type => 'text') from dual;
There are a few other ways to do this such as tracing, GATHER_PLAN_STATISTICS
hint, etc. But SQL Monitoring is the easiest to use and provides the most information. Explain plans alone are usually not enough to tune large SQL statements with complex execution plans.
Edit your question with the results and the root cause may be obvious to spot.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.