Home » RDBMS Server » Performance Tuning » query tunning (oracle10g)
query tunning [message #685968] |
Wed, 11 May 2022 06:53 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
Currently, i am currently the table and re-loading the data since the below query is running for long time.
can you suggest some ideas to tune the query and run faster.
whenever sqlerror exit -1
set ver off
set term off
set feed on
set head off
set pause off
set pages 0
set trimspool on
set echo on
set lines 500
set serveroutput on
spool &1
DEFINE CTRY='SP';
DEFINE OWNER='&&CTRY._DICT1'; -- NOTE THERE IS NO PREFIX FOR US
DEFINE CTRYCODE=223939;
DEFINE LANGCODE=4;
INSERT INTO &&OWNER..&&CTRY._DICT_LOG_TBL(LOCAL_TBL_NAME, COUNT_DESC, COUNT_RECS)
(SELECT '&&CTRY._COMP_TBL', 'REC COUNT BEFORE UPDATE', COUNT(*) FROM &&OWNER..&&CTRY._COMP_TBL)
/
/* DELETE ANY OLD COMPANIES WHERE THE ID NO LONGER EXISTS IN THE INDIA */
DELETE FROM &&OWNER..&&CTRY._COMP_TBL
WHERE COMP_ID NOT IN
(SELECT C.COMP_ID
FROM COMP@INDIA_DICTP C, COMP_SALE_LOCATION@INDIA_DICTP S
WHERE C.APPROVAL_CODE = 'APPRD'
AND S.COMP_ID=C.COMP_ID
AND S.SALE_LOCATION_ID=&&CTRYCODE.)
/
/* NOW DELETE ANY ROWS WHERE THE NAME OR THE LAST_WEEK HAVE CHANGED */
DELETE FROM &&OWNER..&&CTRY._COMP_TBL
WHERE COMP_ID IN
(SELECT INDIA.COMP_ID
FROM &&OWNER..&&CTRY._COMP_TBL &&CTRY.,
(SELECT C.COMP_ID, C.COMP_NAME, C.FIRST_WEEK, C.LAST_WEEK
FROM COMP@INDIA_DICTP C, COMP_SALE_LOCATION@INDIA_DICTP S
WHERE C.APPROVAL_CODE = 'APPRD'
AND S.COMP_ID=C.COMP_ID
AND S.SALE_LOCATION_ID=&&CTRYCODE.) INDIA
WHERE INDIA.COMP_ID=&&CTRY..COMP_ID
AND (&&CTRY..COMP_NAME!=INDIA.COMP_NAME OR &&CTRY..LAST_WEEK!=INDIA.LAST_WEEK))
/
INSERT INTO &&OWNER..&&CTRY._DICT_LOG_TBL(LOCAL_TBL_NAME, COUNT_DESC, COUNT_RECS)
(SELECT '&&CTRY._COMP_TBL', 'REC COUNT AFTER DELETE', COUNT(*) FROM &&OWNER..&&CTRY._COMP_TBL)
/
/* (RE-)INSERT ANY NEW OR CHANGED COMP_IDS TO THE &&CTRY. TABLE */
INSERT INTO &&OWNER..&&CTRY._COMP_TBL(COMP_ID, COMP_NAME, FIRST_WEEK, LAST_WEEK)
(SELECT C.COMP_ID, C.COMP_NAME, C.FIRST_WEEK, C.LAST_WEEK
FROM COMP@INDIA_DICTP C, COMP_SALE_LOCATION@INDIA_DICTP S
WHERE C.APPROVAL_CODE = 'APPRD'
AND S.COMP_ID=C.COMP_ID
AND S.SALE_LOCATION_ID=&&CTRYCODE.
AND NOT EXISTS (SELECT COMP_ID FROM &&OWNER..&&CTRY._COMP_TBL WHERE COMP_ID=C.COMP_ID))
/
INSERT INTO &&OWNER..&&CTRY._DICT_LOG_TBL(LOCAL_TBL_NAME, COUNT_DESC, COUNT_RECS)
(SELECT '&&CTRY._COMP_TBL', 'REC COUNT AFTER INSERT', COUNT(*) FROM &&OWNER..&&CTRY._COMP_TBL)
/
commit;
select '&&OWNER..&&CTRY._COMP_TBL sync completed on '||REPLACE(TO_CHAR(sysdate,'DD-MON-YYYY HH24:MI:SS'),' ',' at ') from dual
/
spool off
exit
|
|
|
query tunning [message #685969 is a reply to message #685968] |
Wed, 11 May 2022 06:53 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
Currently, i am currently the table and re-loading the data since the below query is running for long time.
can you suggest some ideas to tune the query and run faster.
whenever sqlerror exit -1
set ver off
set term off
set feed on
set head off
set pause off
set pages 0
set trimspool on
set echo on
set lines 500
set serveroutput on
spool &1
DEFINE CTRY='SP';
DEFINE OWNER='&&CTRY._DICT1'; -- NOTE THERE IS NO PREFIX FOR US
DEFINE CTRYCODE=223939;
DEFINE LANGCODE=4;
INSERT INTO &&OWNER..&&CTRY._DICT_LOG_TBL(LOCAL_TBL_NAME, COUNT_DESC, COUNT_RECS)
(SELECT '&&CTRY._COMP_TBL', 'REC COUNT BEFORE UPDATE', COUNT(*) FROM &&OWNER..&&CTRY._COMP_TBL)
/
/* DELETE ANY OLD COMPANIES WHERE THE ID NO LONGER EXISTS IN THE INDIA */
DELETE FROM &&OWNER..&&CTRY._COMP_TBL
WHERE COMP_ID NOT IN
(SELECT C.COMP_ID
FROM COMP@INDIA_DICTP C, COMP_SALE_LOCATION@INDIA_DICTP S
WHERE C.APPROVAL_CODE = 'APPRD'
AND S.COMP_ID=C.COMP_ID
AND S.SALE_LOCATION_ID=&&CTRYCODE.)
/
/* NOW DELETE ANY ROWS WHERE THE NAME OR THE LAST_WEEK HAVE CHANGED */
DELETE FROM &&OWNER..&&CTRY._COMP_TBL
WHERE COMP_ID IN
(SELECT INDIA.COMP_ID
FROM &&OWNER..&&CTRY._COMP_TBL &&CTRY.,
(SELECT C.COMP_ID, C.COMP_NAME, C.FIRST_WEEK, C.LAST_WEEK
FROM COMP@INDIA_DICTP C, COMP_SALE_LOCATION@INDIA_DICTP S
WHERE C.APPROVAL_CODE = 'APPRD'
AND S.COMP_ID=C.COMP_ID
AND S.SALE_LOCATION_ID=&&CTRYCODE.) INDIA
WHERE INDIA.COMP_ID=&&CTRY..COMP_ID
AND (&&CTRY..COMP_NAME!=INDIA.COMP_NAME OR &&CTRY..LAST_WEEK!=INDIA.LAST_WEEK))
/
INSERT INTO &&OWNER..&&CTRY._DICT_LOG_TBL(LOCAL_TBL_NAME, COUNT_DESC, COUNT_RECS)
(SELECT '&&CTRY._COMP_TBL', 'REC COUNT AFTER DELETE', COUNT(*) FROM &&OWNER..&&CTRY._COMP_TBL)
/
/* (RE-)INSERT ANY NEW OR CHANGED COMP_IDS TO THE &&CTRY. TABLE */
INSERT INTO &&OWNER..&&CTRY._COMP_TBL(COMP_ID, COMP_NAME, FIRST_WEEK, LAST_WEEK)
(SELECT C.COMP_ID, C.COMP_NAME, C.FIRST_WEEK, C.LAST_WEEK
FROM COMP@INDIA_DICTP C, COMP_SALE_LOCATION@INDIA_DICTP S
WHERE C.APPROVAL_CODE = 'APPRD'
AND S.COMP_ID=C.COMP_ID
AND S.SALE_LOCATION_ID=&&CTRYCODE.
AND NOT EXISTS (SELECT COMP_ID FROM &&OWNER..&&CTRY._COMP_TBL WHERE COMP_ID=C.COMP_ID))
/
INSERT INTO &&OWNER..&&CTRY._DICT_LOG_TBL(LOCAL_TBL_NAME, COUNT_DESC, COUNT_RECS)
(SELECT '&&CTRY._COMP_TBL', 'REC COUNT AFTER INSERT', COUNT(*) FROM &&OWNER..&&CTRY._COMP_TBL)
/
commit;
select '&&OWNER..&&CTRY._COMP_TBL sync completed on '||REPLACE(TO_CHAR(sysdate,'DD-MON-YYYY HH24:MI:SS'),' ',' at ') from dual
/
spool off
exit
|
|
|
|
|
|
Re: query tunning [message #685977 is a reply to message #685972] |
Thu, 12 May 2022 12:32 |
John Watson
Messages: 8963 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
arun888 wrote on Wed, 11 May 2022 14:55any efficient way to insert the records without long running. Almost certainly. But you have to begin by providing some information. How long does each statement take? How long would you like it to take? Which statement is the worst problem? Having identified the problem, you will have to find out the execution plan and the runtime statistics. If you don't know how to do that, people here can help. Only then can you look at how it can be improved.
|
|
|
|
Re: query tunning [message #685988 is a reply to message #685987] |
Fri, 20 May 2022 01:22 |
John Watson
Messages: 8963 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
If you use the SQL*Plus autotrace facility, all will be revealed. For example:orclz>
orclz> set autotrace on
orclz>
orclz> insert into emp(empno) select max(empno)+1 from emp;
1 row created.
Execution Plan
----------------------------------------------------------
Plan hash value: 1707959928
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | INSERT STATEMENT | | 1 | 4 | 2 (0)| 00:00:01 |
| 1 | LOAD TABLE CONVENTIONAL | EMP | | | | |
| 2 | SORT AGGREGATE | | 1 | 4 | | |
| 3 | INDEX FULL SCAN (MIN/MAX)| PK_EMP | 1 | 4 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
87 recursive calls
7 db block gets
167 consistent gets
6 physical reads
0 redo size
195 bytes sent via SQL*Net to client
402 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
12 sorts (memory)
0 sorts (disk)
1 rows processed
orclz>
(by the way, I wish you would not say "record" when you mean "row")
|
|
|
|
Re: query tunning [message #685990 is a reply to message #685989] |
Fri, 20 May 2022 15:06 |
John Watson
Messages: 8963 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
arun888 wrote on Fri, 20 May 2022 07:35only on the SQL*Plus is the facility is available. ? I don't know. It does help if you know what your chosen client tool can do. You must have a reason for choosing whatever tool it is that you use?
|
|
|
|
Re: query tunning [message #686005 is a reply to message #685998] |
Tue, 24 May 2022 06:26 |
John Watson
Messages: 8963 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Your query is returning 5 rows, but the optimizer is expecting 11992 rows. So possibly your object statistics are rubbish. Better gather table stats, including histograms, and see if it comes up with a better plan.
You could also run a few checks to see where it is going wrong:
How many rows are there in COMP_SALE_LOCATION_TEMP1 ? How many where "SALE_LOCATION_ID"=118444 ?
Same thing for COMP_TEMP1 and its filter.
How many when you join them on comp_id ?
And so on: break down the query, and check the figures at each stage.
|
|
|
Goto Forum:
Current Time: Thu Jan 23 11:31:16 CST 2025
|