Update statement query tuning [message #436117] |
Mon, 21 December 2009 11:30 |
shiva_jm
Messages: 9 Registered: November 2009
|
Junior Member |
|
|
I have an update statement which updates more than a million records and have to CAST the <<WHERE CLAUSE>> to match the records (since one table has a DATE field, other table has a TIMESTAMP field). Any help on how to make the update run faster ?
execute immediate ' UPDATE ODS_STAGE.CMC_BLSB_SB_DETAIL a ' ||
' SET (a.LAST_MOD_DTTM,a.LAST_MOD_USER_ID)=' ||
' ( SELECT b.LAST_MOD_DTTM,b.LAST_MOD_USER_ID from ODS_STAGE.STOO_CMC_BLSB_SB_DETAIL b WHERE ' ||
' a.BLEI_CK = b.BLEI_CK ' ||
' and TRUNC(a.BLBL_DUE_DT) = TRUNC(b.BLBL_DUE_DT) ' ||
' and a.CSPI_ID = b.CSPI_ID ' ||
' and a.PDPD_ID = b.PDPD_ID ' ||
' and a.PDBL_ID = b.PDBL_ID ' ||
' and TRUNC(a.BLSB_COV_DUE_DT) = TRUNC(b.BLSB_COV_DUE_DT) ' ||
' and TRUNC(a.BLSB_COV_FROM_DT) = TRUNC(b.BLSB_COV_FROM_DT) ' ||
' and a.BLSB_PREM_TYPE = b.BLSB_PREM_TYPE ' ||
' and a.SBSB_CK = b.SBSB_CK ' ||
' and a.MEME_CK = b.MEME_CK ' ||
' and TRUNC(a.BLSB_CREATE_DTM) = TRUNC(b.BLSB_CREATE_DTM) ' ||
' ) ';
Thank you
|
|
|
|
Re: Update statement query tuning [message #436120 is a reply to message #436117] |
Mon, 21 December 2009 11:34 |
Its_me_ved
Messages: 979 Registered: October 2009 Location: India
|
Senior Member |
|
|
Why you using dyanamic sql when you can do this in a single update stament?
Moreover, INDEX is not used when you use a function over the column.
Please read the sticky link as provided by Michel Sir..
Regards,
Ved
Edit: Please read the sticky link as provided by Michel Sir..
[Updated on: Mon, 21 December 2009 11:36] Report message to a moderator
|
|
|
|
Re: Update statement query tuning [message #436125 is a reply to message #436117] |
Mon, 21 December 2009 11:52 |
shiva_jm
Messages: 9 Registered: November 2009
|
Junior Member |
|
|
Why you using dyanamic sql when you can do this in a single update stament?
Its auto-generated code (generated for more than 1000 tables)....had to run some other statements running before and after for logging time.
Example :
set serveroutput on;
DECLARE
starting_time TIMESTAMP;
ending_time TIMESTAMP;
log_date DATE;
sequence_number INTEGER;
table_name VARCHAR2(40);
table_desc VARCHAR2(40);
status VARCHAR2(1);
BEGIN
DBMS_OUTPUT.put_line('Started creating table CMC_BLSB_SB_DETAIL');
starting_time := SYSTIMESTAMP;
ending_time := SYSTIMESTAMP;
log_date := SYSDATE;
DBMS_OUTPUT.put_line('The starting time is: ' || starting_time);
table_name := 'CMC_BLSB_SB_DETAIL';
table_desc := 'UPDATE LASTMOD TABLES';
status := 'P';
execute immediate 'SELECT REP_LOG_SEQ.NEXTVAL FROM DUAL' into sequence_number;
commit;
execute immediate ' INSERT INTO ODS_STAGE.REP_LOG ' ||
' (SEQ_NO,TABLE_NAME,SCRIPT_DESC,START_DTM,END_DTM,LOG_DT,STATUS) VALUES ' ||
' (:1, :2, :3, :4, :5, :6, :7)'
using sequence_number, table_name, table_desc, starting_time, ending_time, log_date, status;
commit;
execute immediate ' UPDATE ODS_STAGE.CMC_BLSB_SB_DETAIL a ' ||
' SET (a.LAST_MOD_DTTM,a.LAST_MOD_USER_ID)=' ||
' ( SELECT b.LAST_MOD_DTTM,b.LAST_MOD_USER_ID from ODS_STAGE.STOO_CMC_BLSB_SB_DETAIL b WHERE ' ||
' a.BLEI_CK = b.BLEI_CK ' ||
' and TRUNC(a.BLBL_DUE_DT) = TRUNC(b.BLBL_DUE_DT) ' ||
' and a.CSPI_ID = b.CSPI_ID ' ||
' and a.PDPD_ID = b.PDPD_ID ' ||
' and a.PDBL_ID = b.PDBL_ID ' ||
' and TRUNC(a.BLSB_COV_DUE_DT) = TRUNC(b.BLSB_COV_DUE_DT) ' ||
' and TRUNC(a.BLSB_COV_FROM_DT) = TRUNC(b.BLSB_COV_FROM_DT) ' ||
' and a.BLSB_PREM_TYPE = b.BLSB_PREM_TYPE ' ||
' and a.SBSB_CK = b.SBSB_CK ' ||
' and a.MEME_CK = b.MEME_CK ' ||
' and TRUNC(a.BLSB_CREATE_DTM) = TRUNC(b.BLSB_CREATE_DTM) ' ||
' ) ';
commit;
status := 'C';
ending_time := SYSTIMESTAMP;
execute immediate ' UPDATE ODS_STAGE.REP_LOG SET STATUS = :1, END_DTM = :2 WHERE SEQ_NO = :3 '
USING status, ending_time, sequence_number;
commit;
DBMS_OUTPUT.put_line('The ending time is: ' || ending_time);
DBMS_OUTPUT.put_line('Completed creating table CMC_BLSB_SB_DETAIL');
END;
/
|
|
|
|
|
Re: Update statement query tuning [message #436203 is a reply to message #436129] |
Tue, 22 December 2009 01:49 |
rleishman
Messages: 3728 Registered: October 2005 Location: Melbourne, Australia
|
Senior Member |
|
|
Dynamic SQL isn't your problem. Sure it's hard for us to read, but it's only being run once to update a million rows - the possible extra parse time for a single run is the least of your problems.
Your real problem is NESTED SQL. See this article for a detailed explanation and an appropriate fix.
Ross Leishman
|
|
|