Hi Tom,
Could you please tell me why it take so long to do the insert data into a Temporary Table inside Store Procedure compare to the straight Insert from SQLPLUS.
User the same statement INSERT as:
Insert into TMP_GTT
Select t1.a, t2.b, t3.c, t4.d, ....t12.l
from t1, t2, t3, t4,...t12
where ....
....
When execute the store procedure (has only the insert statement), it took 30 minutes. But if I execute the Insert statement from SQLPLUS, it took 1 minute & 30 sec.
Thanks.
Followup August 29, 2008 - 10pm US/Eastern:
tkprof it and post the results of each.
I'll bet - almost 100% sure - in plsql you are running a different query....
I use the same Insert statement to insert data into a Temporary table. Since we're off Monday & I've to wait DBA extract the trace file.
Here is the tkprof when calling the store procedureTKPROF: Release 9.2.0.6.0 - Production on Tue Sep 2 16:48:13 2008
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Trace file: xxxxx_ora_xxxxx.trc
Sort options: default
********************************************************************************
count = number of times OCI procedure was executed
cpu = cpu time in seconds executing
elapsed = elapsed time in seconds executing
disk = number of physical reads of buffers from disk
query = number of buffers gotten for consistent read
current = number of buffers gotten in current mode (usually for update)
rows = number of rows processed by the fetch or execute call
********************************************************************************
INSERT INTO TEMP_RPI_TBL
SELECT E.CHANNEL_ID, E.ON_DATE,
NEXT_DAY(E.ON_DATE-7,:B10 ), E.START_TIME, E.START_TIME + E.DURATION,
E.DURATION, ETD.CC_SOURCE_CODE, DECODE(CN.NETWORK_TYPE,500000681,
DECODE(PROG.PROGRAM_KIND_CODE,4270001,ES.CATEGORY,PS.CATEGORY),
DECODE(PROG.PROGRAM_KIND_CODE,4270001,EB.CATEGORY,PB.CATEGORY)),
DECODE(CN.NETWORK_TYPE,500000681,DECODE(PROG.PROGRAM_KIND_CODE,4270001,
ES.COUNTRY_OF_ORIGIN,PS.COUNTRY_OF_ORIGIN), DECODE(PROG.PROGRAM_KIND_CODE,
4270001,EB.COUNTRY_OF_ORIGIN,PB.COUNTRY_OF_ORIGIN)), DECODE(CN.NETWORK_TYPE,
500000682,DECODE(PROG.PROGRAM_KIND_CODE,4270001,EB.BROADCAST_ORIGIN_POINT,
PB.BROADCAST_ORIGIN_POINT), NULL), DECODE(CN.NETWORK_TYPE,500000681,
DECODE(PROG.PROGRAM_KIND_CODE,4270001,ES.EXHIBITION_CODE,PS.EXHIBITION_CODE)
, DECODE(PROG.PROGRAM_KIND_CODE,4270001,EB.COMPOSITION_CODE ,
PB.COMPOSITION_CODE)), DECODE(PROG.PROGRAM_KIND_CODE,4270001,
CEC.CONTENT_SOURCE,CPC.CONTENT_SOURCE), DECODE(CN.NETWORK_TYPE,500000681,
DECODE(PROG.PROGRAM_KIND_CODE,4270001,ES.PRODUCER_1,PS.PRODUCER_1),
DECODE(PROG.PROGRAM_KIND_CODE,4270001,EB.PRODUCER_1 ,PB.PRODUCER_1)),
DECODE(CN.NETWORK_TYPE,500000681,DECODE(PROG.PROGRAM_KIND_CODE,4270001,
ES.PRODUCER_2,PS.PRODUCER_2), DECODE(PROG.PROGRAM_KIND_CODE,4270001,
EB.PRODUCER_2 ,PB.PRODUCER_2)), DECODE(PROG.PROGRAM_KIND_CODE,4270001,
CEC.PRODUCTION_LOCATION_1,CPC.PRODUCTION_LOCATION_1),
DECODE(PROG.PROGRAM_KIND_CODE,4270001,CEC.PRODUCTION_LOCATION_1_PERCENT,
CPC.PRODUCTION_LOCATION_1_PERCENT), DECODE(PROG.PROGRAM_KIND_CODE,4270001,
CEC.PRODUCTION_LOCATION_2,CPC.PRODUCTION_LOCATION_2),
DECODE(PROG.PROGRAM_KIND_CODE,4270001,CEC.PRODUCTION_LOCATION_2_PERCENT,
CPC.PRODUCTION_LOCATION_2_PERCENT), DECODE(PROG.PROGRAM_KIND_CODE,4270001,
CEC.PRODUCTION_LOCATION_3,CPC.PRODUCTION_LOCATION_3),
DECODE(PROG.PROGRAM_KIND_CODE,4270001,CEC.PRODUCTION_LOCATION_3_PERCENT,
CPC.PRODUCTION_LOCATION_3_PERCENT), SYSDATE FROM EVENT E, EVENT_RUN ER,
EVENT_COMPOSITION EC, PROGRAM PROG, EPISODE EPIS, CHANNEL_NETWORK CN,
CHANNEL CH, PROGRAM_BROADCAST PB, PROGRAM_SPECIALITY PS, EPISODE_BROADCAST
EB, EPISODE_SPECIALITY ES, C_PROGRAM_CLASSIFICATION CPC,
C_EPISODE_CLASSIFICATION CEC, EVENT_TECHNICAL_DATA ETD WHERE EC.ON_DATE
BETWEEN :B9 AND :B8 AND BITAND(:B7 , POWER( 2, (TO_NUMBER(TO_CHAR(
E.ON_DATE, 'D') ) -1 ) )) <> 0 AND E.CHANNEL_ID = CH.CHANNEL_ID AND
E.CHANNEL_ID = CN.CHANNEL_ID AND ER.ON_DATE = E.ON_DATE AND ER.EVENT_ID =
E.EVENT_ID AND ER.DETAIL_ID = E.DETAIL_ID AND E.START_TIME >= :B6 AND :B5 >=
DECODE(:B5 ,0,0,(E.START_TIME + E.DURATION)) AND E.DAY_TYPE_ID =
DECODE(:B4 ,0,E.DAY_TYPE_ID,:B4 ) AND EC.ON_DATE = ER.ON_DATE AND
EC.DETAIL_ID = ER.DETAIL_ID AND EC.EVENT_COMPOSITION_ID =
ER.EVENT_COMPOSITION_ID AND EC.PROGRAM_ID = EPIS.PROGRAM_ID (+) AND
EC.EPISODE_ID = EPIS.EPISODE_ID (+) AND PROG.PROGRAM_ID = EC.PROGRAM_ID AND
PROG.COMPANY_POLICY = :B3 AND ETD.ON_DATE = E.ON_DATE AND
ETD.EVENT_TECHNICAL_DATA_ID = E.EVENT_TECHNICAL_DATA_ID AND EC.PROGRAM_ID =
PB.PROGRAM_ID (+) AND EC.PROGRAM_ID = EB.PROGRAM_ID (+) AND EC.EPISODE_ID =
EB.EPISODE_ID (+) AND EC.PROGRAM_ID = PS.PROGRAM_ID (+) AND EC.PROGRAM_ID =
ES.PROGRAM_ID (+) AND EC.EPISODE_ID = ES.EPISODE_ID (+) AND EC.PROGRAM_ID =
CPC.PROGRAM_ID (+) AND EC.PROGRAM_ID = CEC.PROGRAM_ID (+) AND EC.EPISODE_ID
= CEC.EPISODE_ID (+) AND
CBC_PKG_BROADCAST_RPT.CBC_FCT_ORIGINAL_HOURS(ER.TELECAST_NUMBER,
ER.DETAIL_ID) >= :B2 AND DECODE(PROG.PROGRAM_KIND_CODE,4270001,
EB.BROADCAST_ORIGIN_POINT,PB.BROADCAST_ORIGIN_POINT) = DECODE(:B1 ,0,
500000436, DECODE(PROG.PROGRAM_KIND_CODE,4270001,EB.BROADCAST_ORIGIN_POINT,
PB.BROADCAST_ORIGIN_POINT)) ORDER BY E.CHANNEL_ID, E.ON_DATE
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.01 0.01 0 0 0 0
Execute 1 1579.42 2148.30 76953 24929744 82103 53981
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 2 1579.43 2148.32 76953 24929744 82103 53981
Misses in library cache during parse: 1
Optimizer goal: CHOOSE
Parsing user id: 1743 (recursive depth: 1)
Rows Row Source Operation
------- ---------------------------------------------------
53981 SORT ORDER BY (cr=27661707 r=77017 w=0 time=2168482799 us)
53981 CONCATENATION (cr=27661707 r=77017 w=0 time=2167234416 us)
53981 FILTER (cr=27661707 r=77017 w=0 time=2167198474 us)
53981 NESTED LOOPS (cr=27661707 r=77017 w=0 time=2167153836 us)
341926 NESTED LOOPS OUTER (cr=26977853 r=77017 w=0 time=2162352480 us)
341926 NESTED LOOPS OUTER (cr=26267234 r=77006 w=0 time=2151391350 us)
341926 NESTED LOOPS OUTER (cr=25583840 r=77003 w=0 time=2147546126 us)
341926 NESTED LOOPS OUTER (cr=24900430 r=76997 w=0 time=2143739958 us)
341926 NESTED LOOPS OUTER (cr=24279981 r=76922 w=0 time=2138659725 us)
341926 NESTED LOOPS OUTER (cr=23660978 r=76817 w=0 time=2133202134 us)
341926 NESTED LOOPS OUTER (cr=23126242 r=76809 w=0 time=2130044803 us)
341926 NESTED LOOPS (cr=23122804 r=76807 w=0 time=2128158776 us)
341926 NESTED LOOPS (cr=22086391 r=76706 w=0 time=2121390834 us)
1677211 NESTED LOOPS (cr=15575367 r=75858 w=0 time=2004925844 us)
1677211 NESTED LOOPS (cr=13898154 r=75858 w=0 time=1992246113 us)
5543578 NESTED LOOPS (cr=7189310 r=68405 w=0 time=1869523653 us)
76 TABLE ACCESS FULL CHANNEL (cr=15 r=0 w=0 time=3612 us)
5543578 PARTITION RANGE ITERATOR PARTITION: KEY KEY (cr=7189295 r=68405 w=0 time=1866723705 us)
5543578 TABLE ACCESS BY LOCAL INDEX ROWID EVENT PARTITION: KEY KEY (cr=7189295 r=68405 w=0 time=1863823515 us)
5593708 INDEX RANGE SCAN EVENT_ONDATE_CHANNEL_IDX PARTITION: KEY KEY (cr=3765948 r=16599 w=0 time=1460964886 us)(object id 54519)
1677211 PARTITION RANGE ITERATOR PARTITION: KEY KEY (cr=6708844 r=7453 w=0 time=99338566 us)
1677211 TABLE ACCESS BY LOCAL INDEX ROWID EVENT_TECHNICAL_DATA PARTITION: KEY KEY (cr=6708844 r=7453 w=0 time=92302405 us)
1677211 INDEX UNIQUE SCAN EVENT_TECHNCL_DATA_ID_DATE_PK PARTITION: KEY KEY (cr=5031633 r=2600 w=0 time=44525085 us)(object id 55068)
1677211 TABLE ACCESS BY INDEX ROWID CHANNEL_NETWORK (cr=1677213 r=0 w=0 time=8508732 us)
1677211 INDEX UNIQUE SCAN CHANNEL_NETWORK_PK_IDX (cr=2 r=0 w=0 time=2802378 us)(object id 44857)
341926 TABLE ACCESS BY INDEX ROWID EVENT_RUN (cr=6511024 r=848 w=0 time=111382420 us)
341926 INDEX RANGE SCAN EVENT_RUN_PK_IDX (cr=3419906 r=560 w=0 time=23676300 us)(object id 51151)
341926 TABLE ACCESS BY INDEX ROWID EVENT_COMPOSITION (cr=1036413 r=101 w=0 time=5444400 us)
341926 INDEX UNIQUE SCAN EVENT_COMP_ON_DATE_IDX (cr=683854 r=101 w=0 time=3621422 us)(object id 41970)
3436 TABLE ACCESS BY INDEX ROWID C_EPISODE_CLASSIFICATION (cr=3438 r=2 w=0 time=776854 us)
3436 INDEX UNIQUE SCAN C_EPISODE_CLASSIFICATION_PK (cr=2 r=0 w=0 time=505506 us)(object id 44889)
192808 TABLE ACCESS BY INDEX ROWID C_PROGRAM_CLASSIFICATION (cr=534736 r=8 w=0 time=2183909 us)
192808 INDEX UNIQUE SCAN C_PROGRAM_CLASSIFICATION_PK (cr=341928 r=1 w=0 time=1143260 us)(object id 86827)
276987 TABLE ACCESS BY INDEX ROWID EPISODE_SPECIALITY (cr=619003 r=105 w=0 time=4305215 us)
276987 INDEX UNIQUE SCAN EPISODE_SPECIALITY_PK_IDX (cr=341928 r=22 w=0 time=2011892 us)(object id 44953)
278521 TABLE ACCESS BY INDEX ROWID EPISODE_BROADCAST (cr=620449 r=75 w=0 time=3947050 us)
278521 INDEX UNIQUE SCAN EPISODE_BROADCAST_PK_IDX (cr=341928 r=28 w=0 time=2030345 us)(object id 44941)
341482 TABLE ACCESS BY INDEX ROWID PROGRAM_SPECIALITY (cr=683410 r=6 w=0 time=2781355 us)
341482 INDEX UNIQUE SCAN PROGRAM_SPECIALITY_PK_IDX (cr=341928 r=2 w=0 time=1308375 us)(object id 45013)
341466 TABLE ACCESS BY INDEX ROWID PROGRAM_BROADCAST (cr=683394 r=3 w=0 time=2814329 us)
341466 INDEX UNIQUE SCAN PROGRAM_BROADCAST_PK_IDX (cr=341928 r=2 w=0 time=1257436 us)(object id 44999)
330872 TABLE ACCESS BY INDEX ROWID EPISODE (cr=710619 r=11 w=0 time=9919952 us)
341926 INDEX UNIQUE SCAN EPISODE_UK_IDX (cr=341928 r=11 w=0 time=1680328 us)(object id 51606)
53981 TABLE ACCESS BY INDEX ROWID PROGRAM (cr=683854 r=0 w=0 time=4013994 us)
341926 INDEX UNIQUE SCAN PROGRAM_PK_IDX (cr=341928 r=0 w=0 time=1396292 us)(object id 42891)
0 FILTER (cr=0 r=0 w=0 time=1 us)
0 FILTER
0 NESTED LOOPS OUTER
0 NESTED LOOPS OUTER
0 NESTED LOOPS OUTER
0 NESTED LOOPS
0 NESTED LOOPS OUTER
0 NESTED LOOPS OUTER
0 NESTED LOOPS
0 NESTED LOOPS
0 NESTED LOOPS
0 NESTED LOOPS
0 NESTED LOOPS OUTER
0 NESTED LOOPS OUTER
0 NESTED LOOPS
0 TABLE ACCESS FULL PROGRAM
0 TABLE ACCESS BY INDEX ROWID EVENT_COMPOSITION
0 INDEX RANGE SCAN EVENT_COMP_PROGRAM_FK_IDX (object id 41969)
0 TABLE ACCESS BY INDEX ROWID EPISODE_SPECIALITY
0 INDEX UNIQUE SCAN EPISODE_SPECIALITY_PK_IDX (object id 44953)
0 TABLE ACCESS BY INDEX ROWID EPISODE_BROADCAST
0 INDEX UNIQUE SCAN EPISODE_BROADCAST_PK_IDX (object id 44941)
0 TABLE ACCESS BY INDEX ROWID EVENT_RUN
0 INDEX RANGE SCAN EVENT_RUN_PK_IDX (object id 51151)
0 TABLE ACCESS BY GLOBAL INDEX ROWID EVENT PARTITION: ROW LOCATION ROW LOCATION
0 INDEX UNIQUE SCAN EVENT_PK (object id 41917)
0 TABLE ACCESS BY INDEX ROWID CHANNEL
0 INDEX UNIQUE SCAN CHANNEL_PK_IDX (object id 41245)
0 PARTITION RANGE ITERATOR PARTITION: KEY KEY
0 TABLE ACCESS BY LOCAL INDEX ROWID EVENT_TECHNICAL_DATA PARTITION: KEY KEY
0 INDEX UNIQUE SCAN EVENT_TECHNCL_DATA_ID_DATE_PK PARTITION: KEY KEY (object id 55068)
0 TABLE ACCESS BY INDEX ROWID C_EPISODE_CLASSIFICATION
0 INDEX UNIQUE SCAN C_EPISODE_CLASSIFICATION_PK (object id 44889)
0 TABLE ACCESS BY INDEX ROWID EPISODE
0 INDEX UNIQUE SCAN EPISODE_UK_IDX (object id 51606)
0 TABLE ACCESS BY INDEX ROWID CHANNEL_NETWORK
0 INDEX UNIQUE SCAN CHANNEL_NETWORK_PK_IDX (object id 44857)
0 TABLE ACCESS BY INDEX ROWID C_PROGRAM_CLASSIFICATION
0 INDEX UNIQUE SCAN C_PROGRAM_CLASSIFICATION_PK (object id 86827)
0 TABLE ACCESS BY INDEX ROWID PROGRAM_SPECIALITY
0 INDEX UNIQUE SCAN PROGRAM_SPECIALITY_PK_IDX (object id 45013)
0 TABLE ACCESS BY INDEX ROWID PROGRAM_BROADCAST
0 INDEX UNIQUE SCAN PROGRAM_BROADCAST_PK_IDX (object id 44999)
and here is the tkprof when using a straigh SQL insertionTKPROF: Release 9.2.0.6.0 - Production on Tue Sep 2 17:58:13 2008
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Trace file: xxxxx_ora_xxxxx.trc
Sort options: default
********************************************************************************
count = number of times OCI procedure was executed
cpu = cpu time in seconds executing
elapsed = elapsed time in seconds executing
disk = number of physical reads of buffers from disk
query = number of buffers gotten for consistent read
current = number of buffers gotten in current mode (usually for update)
rows = number of rows processed by the fetch or execute call
********************************************************************************
Insert into temp_rpi_tbl
Select e.channel_id, e.on_date, next_day(e.on_date-7,'Monday'), e.start_time,
e.start_time + e.duration, e.duration, etd.cc_source_code,
decode(cn.network_type,500000681,decode(prog.program_kind_code,4270001,es.category,ps.category),
decode(prog.program_kind_code,4270001,eb.category,pb.category)),
decode(cn.network_type,500000681,decode(prog.program_kind_code,4270001,es.country_of_origin,ps.country_of_origin),
decode(prog.program_kind_code,4270001,eb.country_of_origin,pb.country_of_origin)), decode(cn.network_type,500000682,decode(prog.program_kind_code,4270001,eb.broadcast_origin_point,pb.broadcast_origin_point),null),
decode(cn.network_type,500000681,decode(prog.program_kind_code,4270001,es.exhibition_code,ps.exhibition_code),
decode(prog.program_kind_code,4270001,eb.composition_code ,pb.composition_code)),
decode(prog.program_kind_code,4270001,cec.content_source,cpc.content_source),
decode(cn.network_type,500000681,decode(prog.program_kind_code,4270001,es.producer_1,ps.producer_1),
decode(prog.program_kind_code,4270001,eb.producer_1 ,pb.producer_1)),
decode(cn.network_type,500000681,decode(prog.program_kind_code,4270001,es.producer_2,ps.producer_2),
decode(prog.program_kind_code,4270001,eb.producer_2 ,pb.producer_2)),
decode(prog.program_kind_code,4270001,cec.production_location_1,cpc.production_location_1),
decode(prog.program_kind_code,4270001,cec.production_location_1_percent,cpc.production_location_1_percent),
decode(prog.program_kind_code,4270001,cec.production_location_2,cpc.production_location_2),
decode(prog.program_kind_code,4270001,cec.production_location_2_percent,cpc.production_location_2_percent),
decode(prog.program_kind_code,4270001,cec.production_location_3,cpc.production_location_3),
decode(prog.program_kind_code,4270001,cec.production_location_3_percent,cpc.production_location_3_percent),
sysdate
From event e, event_run er, event_composition ec, program prog, episode epis,
channel_network cn, channel ch, program_broadcast pb, program_speciality ps,
episode_broadcast eb, episode_speciality es, c_program_classification cpc,
c_episode_classification cec, event_technical_data etd
Where ec.on_date between '07-01-01' and '07-06-30'
and bitand(127, power( 2, (to_number(to_char( e.on_date, 'D') ) -1 ) )) <> 0
and e.channel_id = ch.channel_id
and e.channel_id = cn.channel_id
and er.on_date = e.on_date
and er.event_id = e.event_id
and er.detail_id = e.detail_id
and e.start_time >= 0
and 5183999 >= decode(5183999,0,0,(e.start_time + e.duration))
and e.day_type_id = decode(0,0,e.day_type_id,0)
and ec.on_date = er.on_date
and ec.detail_id = er.detail_id
and ec.event_composition_id = er.event_composition_id
and ec.program_id = epis.program_id (+)
and ec.episode_id = epis.episode_id (+)
and prog.program_id = ec.program_id
and prog.company_policy = '110'
and etd.on_date = e.on_date
and etd.event_technical_data_id = e.event_technical_data_id
and ec.program_id = pb.program_id (+)
and ec.program_id = eb.program_id (+)
and ec.episode_id = eb.episode_id (+)
and ec.program_id = ps.program_id (+)
and ec.program_id = es.program_id (+)
and ec.episode_id = es.episode_id (+)
and ec.program_id = cpc.program_id (+)
and ec.program_id = cec.program_id (+)
and ec.episode_id = cec.episode_id (+)
and cbc_pkg_broadcast_rpt.cbc_fct_original_hours(er.telecast_number,er.detail_id) >= 0
and decode(prog.program_kind_code,4270001,eb.broadcast_origin_point,pb.broadcast_origin_point) =
decode(0,0,500000436,
decode(prog.program_kind_code,4270001,eb.broadcast_origin_point,pb.broadcast_origin_point))
order by e.channel_id, e.on_date
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.18 0.17 0 22 0 0
Execute 1 27.10 26.58 0 1802450 105199 53981
Fetch 0 0.00 0.00 0 0 0 0
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 2 27.28 26.76 0 1802472 105199 53981
Misses in library cache during parse: 1
Optimizer goal: CHOOSE
Parsing user id: 1743
both of them using the same parameter values as:P_STARTDATE DATE = '2007-01-01';
P_ENDDATE DATE = '2007-06-30';
P_STARTTIME INTEGER = 0;
P_ENDTIME INTEGER = 5183999;
P_OTHER INTEGER = 0;
P_DOW INTEGER = 127;
P_WEEKDAY INTEGER = 1082002;
P_SCHEDULE INTEGER = 0;
P_PROG_STIME INTEGER = 2052000;
P_PROG_ETIME INTEGER = 2484000;
P_HOUR INTEGER = 0;
LANGUAGE_ID_ INTEGER = 1;
COMPANY_TAG_ VARCHAR2 = '110';
P_WEEKDAY VARCHAR2 = 'Monday';
Thanks Tom.
P.S.: both tkprof has been trim out of the unrelate to my question (all SQL of other users), otherwise it'll be a long message.