Tuning SQL query
Please help me in tuning this query. This view has around 2 million of records
SELECT * FROM employee_v
WHERE status_id IN (SELECT MAX(v2.status_id)status_id
FROM employee_v v2
WHERE UPPER(v2.name) LIKE UPPER(:v_name)||'%'
GROUP BY v2.emp_id, v2.project_id)
ORDER BY status_type_id;
Here:
employees may work on one or more projects
employee working on a given project may have one or more status_ids associated.
for example:
emp_id project_id status_id
10 100 1
10 100 2
20 100 2
20 200 3
30 100 4
30 200 5
30 200 6
40 200 6
If you have any other suggestions , please let me know too. For example, I was thinking that the subquery should use actual tables instead of the view because view is based on 10 tables and the subquery can instead use two tables . Does it make sense?
Thanks a lot for the help!
Edited by: user5406804 on Apr 20, 2010 5:42 AM
If you have any other suggestions , please let me know too. Wild shot in the dark, but maybe using EXISTS instead of IN speeds up your query:
untested for obvious reasons
select *
from employee_v v
where upper(v.name) like upper(:v_name)||'%'
where not exists ( select null
from employee_v v2
and v2.status_id > v.status_id
and v2.emp_id = v.emp_id -- assuming you have some emp_id PK column
-- if necessary, you need to change the name of this column ofcourse
order by status_type_id;Do compare the resultsets, not sure this will give you the same/desired result, but it might give you an idea.
For example, I was thinking that the subquery should use actual tables instead of the view because > view is based on 10 tables and the subquery can instead
use two tables . Does it make sense?Sounds like worth a try to me, yes. Less data to plough through = faster response.
As well as using a function based index on UPPER(name) might be worth a try.
Similar Messages
-
Tuning SQL Query calling multiple views
Dear All,
I have a complex scenario in which I have created views in 3 levels for sales and Service separately so total no of views are 6.
Level 2 calls view in Level 1 and Level 3 calls level 2 in sales as well as services.
Finally Level 4 has the union of sales and Service Level 3 view.
There are 5 tables used in total and all have been indexed for better performance.
The cube processing for the above scenario has processing time of 6 mins.
The goal is to bring it down to 2-3 mins.
The amount of data currently we are looking at is not more than 2-3 GB.
Can anybody help me fine tune this scenario?
Thanks, Franco.Run the queries the cube is using in SSMS and turn on the Include Actual Execution Plan option.
https://msdn.microsoft.com/en-us/library/ms189562.aspx?f=255&MSPPError=-2147217396
Straight up it may suggest missing indexes which you can test to see if they improve the query performance. You can't take these suggestions as gospel - sometimes SQL Server gets it wrong - so you need to test them.
After that look in the query plan for any table scan operators. Unless you are actually retrieving every row in a table you shouldn't have any of these and they can probably be removed by placing a properly considered index on the table.
After that look for any RID Lookups or Key Lookups. These can generally be resolved fairly easily by adding the columns being looked up as "include" columns in the indexes.
Here are some links that might get you started.
Reading Execution Plans: http://www.mssqltips.com/sqlservertip/1873/how-to-read-sql-server-graphical-query-execution-plans/
Resolving Table Scans http://blog.sqlauthority.com/2007/03/30/sql-server-index-seek-vs-index-scan-table-scan/
Resolving Lookups: http://blog.sqlauthority.com/2009/11/09/sql-server-removing-key-lookup-seek-predicate-predicate-an-interesting-observation-related-to-datatypes/
Those are the obvious things - and generally the easiest to achieve. Performance tuning is a big area. It may be that the database queries are already performing perfectly well and the time being spent is processing the cube itself so you need
to establish where the time is going. There is no point trying to tune the database queries if they are already working well.
LucasF -
Tuning SQL query with SDO and Contains?
I'trying to optimize a query
with a sdo_filter and an intermedia_contains
on a 3.000.000 records table,
the query look like this
SELECT COUNT(*) FROM professionnel WHERE mdsys.sdo_filter(professionnel.coor_cart,mdsys.sdo_geometry(2003, null, null,mdsys.sdo_elem_info_array(1,1003,4),mdsys.sdo_ordinate_array(809990,2087279,778784,2087279,794387,2102882)),'querytype=window') = 'TRUE' AND professionnel.code_rubr ='12 3 30' AND CONTAINS(professionnel.Ctx,'PLOMBERIE within Nom and ( RUE within Adresse1 )',1)>0
and it takes 15s on a bi 750 pentium III with
1.5Go of memory running under 8.1.6 linux.
What can i do to improve this query time?
nullHi Vincent,
We have patches for Oracle 8.1.6 Spatial
on NT and Solaris.
These patches include bug fixes and
performance enhancements.
We are in the process of making these patches
avaialble in a permanent place, but until then, I will temporarily put the patches on:
ftp://oracle-ftp.oracle.com/
Log in as anonymous and use your email for
password.
The patches are in /tmp/outgoing in:
NT816-000706.zip - NT patch
libordsdo.tar - Solaris patch
I recommend doing some analysis on
individual pieces of the query.
i.e. time the following:
1)
SELECT COUNT(*)
FROM professionnel
WHERE mdsys.sdo_filter(
professionnel.coor_cart,
mdsys.sdo_geometry(
2003, null, null,
mdsys.sdo_elem_info_array(1,1003,4),
mdsys.sdo_ordinate_array(
809990,2087279,
778784,2087279,
794387,2102882)),
'querytype=window') = 'TRUE';
2)
SELECT COUNT(*)
FROM professionnel
WHERE CONTAINS(professionnel.Ctx,
'PLOMBERIE within Nom and ( RUE within Adresse1)',1) >0;
You might want to try reorganizing the entire
query as follows (no promises).
If you contact me directly, I can try to
help to further tune the SQL.
Hope this helps. Thanks.
Dan
select count(*)
FROM
(SELECT /*+ no_merge */ rowid
FROM professionnel
WHERE mdsys.sdo_filter(
professionnel.coor_cart,
mdsys.sdo_geometry(
2003, null, null,
mdsys.sdo_elem_info_array(1,1003,4),
mdsys.sdo_ordinate_array(809990,2087279,
778784,2087279,
794387,2102882)),
'querytype=window') = 'TRUE'
) a,
(SELECT /*+ no_merge */ rowid
FROM professionnel
WHERE CONTAINS(professionnel.Ctx,
'PLOMBERIE within Nom and
( RUE within Adresse1)',1) >0
) b
where a.rowid = b.rowid
and professionnel.code_rubr ='12 3 30';
**NOTE** Try this with no index on code_rubr
null -
Hi,
Can anyone assist me in tuning the below query
Thanks in Advance
A.Gopal
SELECT nvl(sum(p.GRP_ACTUALQUANTITY),0)
FROM GOODSRECEIPTS r, GOODSRECEIPTPOSITIONS p
,reportparameters plt,reportparameters spl,reportparameters mat
WHERE r.GRH_NO = p.GRP_GRH_NO
AND TRUNC(r.GRH_ARRIVAL) BETWEEN NVL(TO_DATE('01-jan-09'), TRUNC(r.GRH_ARRIVAL)) AND NVL(TO_DATE('31-jan-09'), TRUNC(r.GRH_ARRIVAL))
AND (p.GRP_PLT_ID = 'VDN' OR 1 =0)
AND (r.GRH_SPL_ID = '15035122' OR 1 =0)
AND (p.GRP_MAT_ID = '1001360' OR 1 =0)
AND ('01.01.2009' = 'TOTAL' or trunc(r.GRH_ARRIVAL,'mm') = to_date('01.01.2009','dd.mm.yyyy'))
and plt.rep_field = 'PLT_ID' AND plt.rep_id = 'AN_EXC_RAT' AND plt.rep_user = 'ANANTGOP'
and spl.rep_field = 'SPL_ID' AND spl.rep_id = 'AN_EXC_RAT' AND spl.rep_user ='ANANTGOP'
and mat.rep_field = 'MAT_ID' AND mat.rep_id = 'AN_EXC_RAT' AND mat.rep_user = 'ANANTGOP'
and r.GRH_SPL_ID = decode(spl.rep_value,'All',r.GRH_SPL_ID,spl.rep_value)
and p.GRp_plt_ID = decode(plt.rep_value,'All',p.GRp_plt_ID,plt.rep_value)
and p.GRp_mat_ID = decode(mat.rep_value,'All',p.GRp_mat_ID,mat.rep_value)
AND NOT EXISTS (SELECT 1 FROM GOODSRECEIPTUSAGESTATUS u
WHERE p.GRP_GRH_NO = u.GRU_GRP_GRH_NO
AND p.GRP_NO =u.GRU_GRP_NO
AND u.GRU_UST_ID = 'CANCEL')This query is used in my Oracle report. The original query is like beow
SELECT nvl(sum(p.GRP_ACTUALQUANTITY),0)
FROM GOODSRECEIPTS r, GOODSRECEIPTPOSITIONS p
,reportparameters plt,reportparameters spl,reportparameters mat
WHERE r.GRH_NO = p.GRP_GRH_NO
AND TRUNC(r.GRH_ARRIVAL) >= NVL(:l_date_from, TRUNC(r.GRH_ARRIVAL)) AND
TRUNC(r.GRH_ARRIVAL) <=NVL(:l_date_to, TRUNC(r.GRH_ARRIVAL))
AND (p.GRP_PLT_ID = :p_plt_id OR :ip_plt_group =0)
AND (r.GRH_SPL_ID = :p_spl_id OR :ip_spl_group =0)
AND (p.GRP_MAT_ID = :p_mat_id OR :ip_mat_group =0)
AND (:l_flag = 'TOTAL' or trunc(r.GRH_ARRIVAL,'mm') = to_date(:p_month_abc_sp,'dd.mm.yyyy'))
and plt.rep_field = 'PLT_ID' AND plt.rep_id = :ip_rep_id AND plt.rep_user = :ip_rep_user
and spl.rep_field = 'SPL_ID' AND spl.rep_id = :ip_rep_id AND spl.rep_user = :ip_rep_user
and mat.rep_field = 'MAT_ID' AND mat.rep_id = :ip_rep_id AND mat.rep_user = :ip_rep_user
and r.GRH_SPL_ID = decode(spl.rep_value,'All',r.GRH_SPL_ID,spl.rep_value)
and p.GRp_plt_ID = decode(plt.rep_value,'All',p.GRp_plt_ID,plt.rep_value)
and p.GRp_mat_ID = decode(mat.rep_value,'All',p.GRp_mat_ID,mat.rep_value)
AND NOT EXISTS (SELECT 1
FROM GOODSRECEIPTUSAGESTATUS u
WHERE p.GRP_GRH_NO = u.GRU_GRP_GRH_NO
AND p.GRP_NO =u.GRU_GRP_NO
AND u.GRU_UST_ID = 'CANCEL');
:ip_rep_id = 'AN_EXC_RAT'
:ip_rep_user = 'ANANTGOP
Value of :l_flag = '01.01.2009'
(:p_month_abc_sp = '01.01.2009'
Thanks
A.Gopal -
Tuning (SQL Query has radically different Buffer GETS in each instance)
I have been noticing that on Node2 of my 10.2.0.3 RAC cluster CPU has been running 90% for the past couple of days and on Node1 we are at the normal 20-30%. I was comparing AWR for both instances and noticed that the same exact SQL was showing different GETS on each instance.
Both Instances are configured exactly the same. SGA 3GB, 2 dual core 1.5Ghz CPU on Solaris 10.
AWR For Node 1
SQL ordered by Gets
Resources reported for PL/SQL code includes the resources used by all SQL statements called by the code.
Total Buffer Gets: 8,199,224
Captured SQL account for 94.2% of Total
Buffer Gets Executions Gets per Exec %Total CPU Time (s) Elapsed Time (s) SQL Id SQL Module SQL Text
4,834,470 16,465 293.62 58.96 442.34 459.42 cs3w0nz7uanhc BEGIN HRW_EC_QUERY.CHECK_STUDE...
4,561,082 16,469 276.95 55.63 411.10 430.16 6vbpbvfburc4x SELECT COUNT(SECURE_PROGRAMS.P... AWR For Node 2
Buffer Gets Executions Gets per Exec %Total CPU Time (s) Elapsed Time (s) SQL Id SQL Module SQL Text
219,779,881 9,306 23,617.01 99.26 11791.62 32202.99 cs3w0nz7uanhc BEGIN HRW_EC_QUERY.CHECK_STUDE...
219,320,634 9,302 23,577.79 99.05 11760.22 32137.23 6vbpbvfburc4x SELECT COUNT(SECURE_PROGRAMS.P... Gets on Node 2 are about 45 times what they are on node 1. We are using bind variables in the SQL. The statements are completely identical. Anyone seen this before?
Thanks,
BrianAll parameters are the same between instances. Here is the result of the query.
PLAN_TABLE_OUTPUT
SQL_ID 6vbpbvfburc4x
SELECT COUNT(SECURE_PROGRAMS.PROG_ID) FROM SECURE_PROGRAMS,HRW_EC_ACTIVE_ITEM, (SELECT ACTIVE_ITEM_ID
FROM TABLE(CAST(:B1 AS HRW_EC_ACTIVE_ITEM_ID_TABLE_T)) ) T WHERE HRW_EC_ACTIVE_ITEM.ACTIVE_ITEM_ID =
T.ACTIVE_ITEM_ID AND HRW_EC_ACTIVE_ITEM.SPROG_ID = SECURE_PROGRAMS.PROG_ID AND
SECURE_PROGRAMS.STUDENT_RESOURCES_URL = :B2
Plan hash value: 25280318
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | | | 730 (100)| |
| 1 | SORT AGGREGATE | | 1 | 57 | | |
| 2 | HASH JOIN | | 116 | 6612 | 730 (1)| 00:00:09 |
| 3 | COLLECTION ITERATOR PICKLER FETCH| | | | | |
| 4 | NESTED LOOPS | | 48596 | 2610K| 705 (1)| 00:00:09 |
| 5 | INDEX RANGE SCAN | SECURE_PROGRAMS_IDX_04 | 4 | 76 | 2 (0)| 00:00:01 |
| 6 | INDEX RANGE SCAN | HRW_EC_ACTIVE_ITEM_IDX_03 | 10984 | 386K| 176 (1)| 00:00:03 |
Query Block Name / Object Alias (identified by operation id):
1 - SEL$62A3881B
5 - SEL$62A3881B / SECURE_PROGRAMS@SEL$1
6 - SEL$62A3881B / HRW_EC_ACTIVE_ITEM@SEL$1
SQL_ID 6vbpbvfburc4x
SELECT COUNT(SECURE_PROGRAMS.PROG_ID) FROM SECURE_PROGRAMS,HRW_EC_ACTIVE_ITEM, (SELECT ACTIVE_ITEM_ID
FROM TABLE(CAST(:B1 AS HRW_EC_ACTIVE_ITEM_ID_TABLE_T)) ) T WHERE HRW_EC_ACTIVE_ITEM.ACTIVE_ITEM_ID =
T.ACTIVE_ITEM_ID AND HRW_EC_ACTIVE_ITEM.SPROG_ID = SECURE_PROGRAMS.PROG_ID AND
SECURE_PROGRAMS.STUDENT_RESOURCES_URL = :B2
Plan hash value: 219484150
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | | | 4845 (100)| |
| 1 | SORT AGGREGATE | | 1 | 57 | | |
| 2 | HASH JOIN | | 2066 | 115K| 4845 (2)| 00:00:59 |
| 3 | INDEX RANGE SCAN | SECURE_PROGRAMS_IDX_04 | 79 | 1501 | 2 (0)| 00:00:01 |
| 4 | HASH JOIN | | 3284 | 121K| 4843 (2)| 00:00:59 |
| 5 | COLLECTION ITERATOR PICKLER FETCH| | | | | |
| 6 | INDEX FAST FULL SCAN | HRW_EC_ACTIVE_ITEM_IDX_03 | 1373K| 47M| 4803 (2)| 00:00:58 |
Query Block Name / Object Alias (identified by operation id):
1 - SEL$62A3881B
3 - SEL$62A3881B / SECURE_PROGRAMS@SEL$1
6 - SEL$62A3881B / HRW_EC_ACTIVE_ITEM@SEL$1
Note
- SQL profile "SYS_SQLPROF_014414387fd00001" used for this statement
SQL_ID 6vbpbvfburc4x
SELECT COUNT(SECURE_PROGRAMS.PROG_ID) FROM SECURE_PROGRAMS,HRW_EC_ACTIVE_ITEM, (SELECT ACTIVE_ITEM_ID
FROM TABLE(CAST(:B1 AS HRW_EC_ACTIVE_ITEM_ID_TABLE_T)) ) T WHERE HRW_EC_ACTIVE_ITEM.ACTIVE_ITEM_ID =
T.ACTIVE_ITEM_ID AND HRW_EC_ACTIVE_ITEM.SPROG_ID = SECURE_PROGRAMS.PROG_ID AND
SECURE_PROGRAMS.STUDENT_RESOURCES_URL = :B2
Plan hash value: 740005210
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | | | 4845 (100)| |
| 1 | SORT AGGREGATE | | 1 | 57 | | |
| 2 | HASH JOIN | | 7 | 399 | 4845 (2)| 00:00:59 |
| 3 | COLLECTION ITERATOR PICKLER FETCH| | | | | |
| 4 | HASH JOIN | | 2992 | 160K| 4821 (2)| 00:00:58 |
| 5 | INDEX RANGE SCAN | SECURE_PROGRAMS_IDX_04 | 33 | 627 | 2 (0)| 00:00:01 |
| 6 | INDEX FAST FULL SCAN | HRW_EC_ACTIVE_ITEM_IDX_03 | 1373K| 47M| 4803 (2)| 00:00:58 |
Query Block Name / Object Alias (identified by operation id):
1 - SEL$62A3881B
5 - SEL$62A3881B / SECURE_PROGRAMS@SEL$1
6 - SEL$62A3881B / HRW_EC_ACTIVE_ITEM@SEL$1
Note
- SQL profile "SYS_SQLPROF_014414387fd00001" used for this statement
SQL_ID 6vbpbvfburc4x
SELECT COUNT(SECURE_PROGRAMS.PROG_ID) FROM SECURE_PROGRAMS,HRW_EC_ACTIVE_ITEM, (SELECT ACTIVE_ITEM_ID
FROM TABLE(CAST(:B1 AS HRW_EC_ACTIVE_ITEM_ID_TABLE_T)) ) T WHERE HRW_EC_ACTIVE_ITEM.ACTIVE_ITEM_ID =
T.ACTIVE_ITEM_ID AND HRW_EC_ACTIVE_ITEM.SPROG_ID = SECURE_PROGRAMS.PROG_ID AND
SECURE_PROGRAMS.STUDENT_RESOURCES_URL = :B2
Plan hash value: 1418188916
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | | | 2136 (100)| |
| 1 | SORT AGGREGATE | | 1 | 57 | | |
| 2 | HASH JOIN | | 3 | 171 | 2136 (1)| 00:00:26 |
| 3 | NESTED LOOPS | | 1103 | 60665 | 2111 (1)| 00:00:26 |
| 4 | INDEX RANGE SCAN | SECURE_PROGRAMS_IDX_04 | 12 | 228 | 2 (0)| 00:00:01 |
| 5 | INDEX RANGE SCAN | HRW_EC_ACTIVE_ITEM_IDX_03 | 10984 | 386K| 176 (1)| 00:00:03 |
| 6 | COLLECTION ITERATOR PICKLER FETCH| | | | | |
Query Block Name / Object Alias (identified by operation id):
1 - SEL$62A3881B
4 - SEL$62A3881B / SECURE_PROGRAMS@SEL$1
5 - SEL$62A3881B / HRW_EC_ACTIVE_ITEM@SEL$1 Thanks,
Brian -
We are converting from a 9i database to an 11g database. The query defined works perfectly under 9i (about 1 second) but is taking 60-70 seconds to return in 11g. The wierd thing is that it has an outer query and a subquery. If I remove the where clause from the outer query it executes in less than a second. Here is the query...
SELECT period, biweek_start, biweek_end, pay_period_complete, worker_count
FROM (SELECT to_char(en.start_date, 'yyyy/mm/dd') || ' to ' ||
to_char(en.start_date + 13, 'yyyy/mm/dd') period,
en.start_date biweek_start,
en.start_date + 13 biweek_end,
decode(sign(sysdate - en.start_date - 13),
-1,
'In Progress',
'Completed') pay_period_complete,
ta_mssauth_pkg.actual_endorser(en.start_date, '33811') worker_count
FROM (select worker_id,
default_endorser resolved_endorser,
effective_date,
expiration_date
from endorse_delegate_history_v
where default_endorser = '33811'
union
select worker_id,
temporary_delegate resolved_endorser,
effective_date,
expiration_date
from endorse_delegate_history_v
where temporary_delegate = '33811'
and temporary_delegate is not null ) de,
endorse_activity_v en
WHERE en.worker_id = de.worker_id
AND en.endorse_status = 'N'
AND en.start_date <= de.expiration_date
AND en.start_date + 13 >= de.effective_date
GROUP BY en.start_date)
WHERE worker_count > 0;
The function that is used as part of the inner query to generate worker_counts is pretty complex. But it runs subsecond if you run it manually. And it works great when you run the query without the "where worker_count > 0" portion.
So the where clause on the outer query seems to be causing the problem - it is based off a function that should have calculated a value FIRST then the outer query should run based on the result of the inner query. Below are the explain plans for the two different versions:
BAD QUERY - 60 - 70 Seconds
Description Object Owner Object Name Cost Cardinality Bytes
SELECT STATEMENT, GOAL = ALL_ROWS 617 1 41
HASH GROUP BY 617 1 41
NESTED LOOPS
NESTED LOOPS 616 1 41
VIEW CA17062 58 186 4464
SORT UNIQUE 58 186 5392
UNION-ALL
TABLE ACCESS BY INDEX ROWID TAS_AUTH ENDORSE_DELEGATE_HISTORY_TBL 42 139 4170
INDEX RANGE SCAN TAS_AUTH ENDORSE_DELEGATE_HIST_DEF_IDX 3 139
TABLE ACCESS BY INDEX ROWID TAS_AUTH ENDORSE_DELEGATE_HISTORY_TBL 14 47 1222
INDEX RANGE SCAN TAS_AUTH ENDORSE_DELEGATE_HIST_TEMP_IDX 3 47
INDEX RANGE SCAN TAS_AUTH TA_SCLENDORSE_PK 2 1
TABLE ACCESS BY INDEX ROWID TAS_AUTH ENDORSE_ACTIVITY_TBL 3 1 17
GOOD QUERY < 1 second
Description Object Owner Object Name Cost Cardinality Bytes
SELECT STATEMENT, GOAL = ALL_ROWS 617 25 1025
HASH GROUP BY 617 25 1025
NESTED LOOPS
NESTED LOOPS 616 25 1025
VIEW CA17062 58 186 4464
SORT UNIQUE 58 186 5392
UNION-ALL
TABLE ACCESS BY INDEX ROWID TAS_AUTH ENDORSE_DELEGATE_HISTORY_TBL 42 139 4170
INDEX RANGE SCAN TAS_AUTH ENDORSE_DELEGATE_HIST_DEF_IDX 3 139
TABLE ACCESS BY INDEX ROWID TAS_AUTH ENDORSE_DELEGATE_HISTORY_TBL 14 47 1222
INDEX RANGE SCAN TAS_AUTH ENDORSE_DELEGATE_HIST_TEMP_IDX 3 47
INDEX RANGE SCAN TAS_AUTH TA_SCLENDORSE_PK 2 1
TABLE ACCESS BY INDEX ROWID TAS_AUTH ENDORSE_ACTIVITY_TBL 3 1 17
So those expleain plans look pretty much identical to me - can anyone tell me why having the where clause on the outer query is causing so much trouble and how I can fix it? I've tried using NO_MERGE and PUSH_SUBQ hints - but nothing that I have done has made the bad query any better.
Thanks in advance!
CoryI have traced the queries and attached the trace files here too...
Here is the bad query...
SQL ID: 0g07hqvjma217 Plan Hash: 0
alter session set sql_trace = true
call count cpu elapsed disk query current rows
Parse 0 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 0 0.00 0.00 0 0 0 0
total 1 0.00 0.00 0 0 0 0
Misses in library cache during parse: 0
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 331 (CA17062)
SQL ID: 9m7787camwh4m Plan Hash: 0
begin :id := sys.dbms_transaction.local_transaction_id; end;
call count cpu elapsed disk query current rows
Parse 3 0.00 0.00 0 0 0 0
Execute 3 0.00 0.00 0 0 0 3
Fetch 0 0.00 0.00 0 0 0 0
total 6 0.00 0.00 0 0 0 3
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 331 (CA17062)
SELECT period, biweek_start, biweek_end, pay_period_complete, worker_count
FROM (SELECT to_char(en.start_date, 'yyyy/mm/dd') || ' to ' ||
to_char(en.start_date + 13, 'yyyy/mm/dd') period,
en.start_date biweek_start,
en.start_date + 13 biweek_end,
decode(sign(sysdate - en.start_date - 13),
-1,
'In Progress',
'Completed') pay_period_complete,
ta_mssauth_pkg.actual_endorser(en.start_date, '33811') worker_count
FROM (select worker_id,
default_endorser resolved_endorser,
effective_date,
expiration_date
from endorse_delegate_history_v
where default_endorser = '33811'
union
select worker_id,
temporary_delegate resolved_endorser,
effective_date,
expiration_date
from endorse_delegate_history_v
where temporary_delegate = '33811'
and temporary_delegate is not null ) de,
endorse_activity_v en
WHERE en.worker_id = de.worker_id
AND en.endorse_status = 'N'
AND en.start_date <= de.expiration_date
AND en.start_date + 13 >= de.effective_date
GROUP BY en.start_date)
WHERE worker_count > 0
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 1 6.11 14.77 38 455 0 6
total 3 6.11 14.77 38 455 0 6
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 331 (CA17062)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
6 6 6 HASH GROUP BY (cr=9330657 pr=826 pw=0 time=86392712 us cost=698 size=41 card=1)
21 21 21 NESTED LOOPS (cr=9330657 pr=826 pw=0 time=18442971 us)
49 49 49 NESTED LOOPS (cr=9330609 pr=826 pw=0 time=26482026 us cost=697 size=41 card=1)
213 213 213 VIEW (cr=32 pr=32 pw=0 time=851827 us cost=58 size=5112 card=213)
213 213 213 SORT UNIQUE (cr=32 pr=32 pw=0 time=850864 us cost=58 size=5598 card=213)
213 213 213 UNION-ALL (cr=32 pr=32 pw=0 time=4390885 us)
15 15 15 TABLE ACCESS BY INDEX ROWID ENDORSE_DELEGATE_HISTORY_TBL (cr=10 pr=10 pw=0 time=718114 us c)
15 15 15 INDEX RANGE SCAN ENDORSE_DELEGATE_HIST_DEF_IDX (cr=3 pr=3 pw=0 time=119582 us cost=3 size=)
198 198 198 TABLE ACCESS BY INDEX ROWID ENDORSE_DELEGATE_HISTORY_TBL (cr=22 pr=22 pw=0 time=7328583 us )
198 198 198 INDEX RANGE SCAN ENDORSE_DELEGATE_HIST_TEMP_IDX (cr=3 pr=3 pw=0 time=107110 us cost=3 size)
49 49 49 INDEX RANGE SCAN TA_SCLENDORSE_PK (cr=9330577 pr=794 pw=0 time=157121745 us cost=2 size=0 card)
21 21 21 TABLE ACCESS BY INDEX ROWID ENDORSE_ACTIVITY_TBL (cr=48 pr=0 pw=0 time=493 us cost=3 size=17 ca)
Rows Execution Plan
0 SELECT STATEMENT MODE: ALL_ROWS
6 HASH (GROUP BY)
21 NESTED LOOPS
49 NESTED LOOPS
213 VIEW
213 SORT (UNIQUE)
213 UNION-ALL
15 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'ENDORSE_DELEGATE_HISTORY_TBL' (TABLE)
15 INDEX MODE: ANALYZED (RANGE SCAN) OF
'ENDORSE_DELEGATE_HIST_DEF_IDX' (INDEX)
198 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'ENDORSE_DELEGATE_HISTORY_TBL' (TABLE)
198 INDEX MODE: ANALYZED (RANGE SCAN) OF
'ENDORSE_DELEGATE_HIST_TEMP_IDX' (INDEX)
49 INDEX MODE: ANALYZED (RANGE SCAN) OF 'TA_SCLENDORSE_PK'
(INDEX (UNIQUE))
21 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'ENDORSE_ACTIVITY_TBL' (TABLE)
SQL ID: gwpkaabt9naxy Plan Hash: 2668568437
SELECT DISTINCT DE.WORKER_ID
FROM
ENDORSE_DELEGATE_HISTORY_V DE , ENDORSE_ACTIVITY_V EN ,
WORKER_ACTIVITY_AUTH_V EA WHERE DE.WORKER_ID = EN.WORKER_ID AND
EN.WORKER_ID = EA.WORKER_ID AND EN.START_DATE = :B2 AND EN.ENDORSE_STATUS =
'N' AND EA.ACTIVITY_DATE >= EN.START_DATE AND EA.ACTIVITY_DATE <=
EN.START_DATE + 13 AND EN.START_DATE <= DE.EXPIRATION_DATE AND
EN.START_DATE + 13 >= DE.EFFECTIVE_DATE AND (DE.DEFAULT_ENDORSER = :B1 OR
DE.TEMPORARY_DELEGATE = :B1 ) ORDER BY DE.WORKER_ID
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 24015 0.97 1.50 0 0 0 0
Fetch 24015 56.38 132.62 593 9318744 0 285
total 48031 57.35 134.12 593 9318744 0 285
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 3054 (TAS_AUTH) (recursive depth: 1)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
0 0 0 SORT UNIQUE (cr=407 pr=43 pw=0 time=1057475 us cost=805 size=83 card=1)
0 0 0 NESTED LOOPS (cr=407 pr=43 pw=0 time=1057453 us cost=804 size=17347 card=209)
0 0 0 HASH JOIN RIGHT ANTI (cr=407 pr=43 pw=0 time=1057442 us cost=804 size=16511 card=209)
19 19 19 INDEX RANGE SCAN INDACC_02X (cr=1 pr=0 pw=0 time=78 us cost=1 size=114 card=19)(object id 5403)
0 0 0 NESTED LOOPS (cr=406 pr=43 pw=0 time=1055235 us)
0 0 0 NESTED LOOPS (cr=406 pr=43 pw=0 time=1055231 us cost=803 size=19856 card=272)
0 0 0 NESTED LOOPS (cr=406 pr=43 pw=0 time=1055226 us cost=431 size=350 card=7)
186 186 186 TABLE ACCESS BY INDEX ROWID ENDORSE_DELEGATE_HISTORY_TBL (cr=32 pr=0 pw=0 time=4375 us cost)
213 213 213 BITMAP CONVERSION TO ROWIDS (cr=6 pr=0 pw=0 time=759 us)
1 1 1 BITMAP OR (cr=6 pr=0 pw=0 time=533 us)
1 1 1 BITMAP CONVERSION FROM ROWIDS (cr=3 pr=0 pw=0 time=178 us)
15 15 15 SORT ORDER BY (cr=3 pr=0 pw=0 time=152 us)
15 15 15 INDEX RANGE SCAN ENDORSE_DELEGATE_HIST_DEF_IDX (cr=3 pr=0 pw=0 time=62 us cost=3 size=)
1 1 1 BITMAP CONVERSION FROM ROWIDS (cr=3 pr=0 pw=0 time=328 us)
198 198 198 SORT ORDER BY (cr=3 pr=0 pw=0 time=319 us)
198 198 198 INDEX RANGE SCAN ENDORSE_DELEGATE_HIST_TEMP_IDX (cr=3 pr=0 pw=0 time=214 us cost=3 siz)
0 0 0 TABLE ACCESS BY INDEX ROWID ENDORSE_ACTIVITY_TBL (cr=374 pr=43 pw=0 time=1052137 us cost=2 )
0 0 0 INDEX UNIQUE SCAN TA_SCLENDORSE_PK (cr=374 pr=43 pw=0 time=1051749 us cost=1 size=0 card=1)
0 0 0 INDEX RANGE SCAN WORKER_ACTIVITY_WRKR_IX (cr=0 pr=0 pw=0 time=0 us cost=2 size=0 card=90)(ob)
0 0 0 TABLE ACCESS BY INDEX ROWID WORKER_ACTIVITY_TBL (cr=0 pr=0 pw=0 time=0 us cost=53 size=943 ca)
0 0 0 INDEX UNIQUE SCAN SCHEDULE_PK (cr=0 pr=0 pw=0 time=0 us cost=0 size=4 card=1)(object id 54342)
Rows Execution Plan
0 SELECT STATEMENT MODE: ALL_ROWS
0 SORT (UNIQUE)
0 CONCATENATION
0 NESTED LOOPS (ANTI)
19 NESTED LOOPS
0 NESTED LOOPS
0 NESTED LOOPS
0 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'ENDORSE_DELEGATE_HISTORY_TBL' (TABLE)
186 INDEX MODE: ANALYZED (RANGE SCAN) OF
'ENDORSE_DELEGATE_HIST_TEMP_IDX' (INDEX)
213 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'ENDORSE_ACTIVITY_TBL' (TABLE)
1 INDEX MODE: ANALYZED (UNIQUE SCAN) OF
'TA_SCLENDORSE_PK' (INDEX (UNIQUE))
1 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'WORKER_ACTIVITY_TBL' (TABLE)
15 INDEX MODE: ANALYZED (RANGE SCAN) OF
'WORKER_ACTIVITY_WRKR_IX' (INDEX)
15 INDEX MODE: ANALYZED (UNIQUE SCAN) OF 'SCHEDULE_PK'
(INDEX (UNIQUE))
1 INDEX MODE: ANALYZED (RANGE SCAN) OF 'INDACC_02X' (INDEX)
198 NESTED LOOPS (ANTI)
198 NESTED LOOPS
0 NESTED LOOPS
0 NESTED LOOPS
0 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'ENDORSE_DELEGATE_HISTORY_TBL' (TABLE)
0 INDEX MODE: ANALYZED (RANGE SCAN) OF
'ENDORSE_DELEGATE_HIST_DEF_IDX' (INDEX)
0 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'ENDORSE_ACTIVITY_TBL' (TABLE)
0 INDEX MODE: ANALYZED (UNIQUE SCAN) OF
'TA_SCLENDORSE_PK' (INDEX (UNIQUE))
0 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'WORKER_ACTIVITY_TBL' (TABLE)
0 INDEX MODE: ANALYZED (RANGE SCAN) OF
'WORKER_ACTIVITY_WRKR_IX' (INDEX)
0 INDEX MODE: ANALYZED (UNIQUE SCAN) OF 'SCHEDULE_PK'
(INDEX (UNIQUE))
0 INDEX MODE: ANALYZED (RANGE SCAN) OF 'INDACC_02X' (INDEX)
SQL ID: 7v8ndut1y3b99 Plan Hash: 2002979989
SELECT DEFAULT_ENDORSER
FROM
( SELECT DEFAULT_ENDORSER FROM ENDORSE_DELEGATE_HISTORY_V WHERE WORKER_ID =
:B3 AND TEMPORARY_DELEGATE IS NULL AND :B2 <= EXPIRATION_DATE AND :B1 >=
EFFECTIVE_DATE ORDER BY EXPIRATION_DATE DESC ) WHERE ROWNUM = 1
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 285 0.00 0.01 0 0 0 0
Fetch 285 0.11 3.24 195 13078 0 285
total 571 0.11 3.26 195 13078 0 285
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 3054 (TAS_AUTH) (recursive depth: 1)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
1 1 1 COUNT STOPKEY (cr=22 pr=20 pw=0 time=576320 us)
1 1 1 VIEW (cr=22 pr=20 pw=0 time=576285 us cost=23 size=72 card=12)
1 1 1 SORT ORDER BY STOPKEY (cr=22 pr=20 pw=0 time=576273 us cost=23 size=396 card=12)
2 2 2 TABLE ACCESS BY INDEX ROWID ENDORSE_DELEGATE_HISTORY_TBL (cr=22 pr=20 pw=0 time=576205 us cost)
20 20 20 INDEX RANGE SCAN ENDORSE_DELEGATE_HIST_PK (cr=3 pr=3 pw=0 time=197569 us cost=3 size=0 card=1)
Rows Execution Plan
0 SELECT STATEMENT MODE: ALL_ROWS
1 COUNT (STOPKEY)
1 VIEW
1 SORT (ORDER BY STOPKEY)
2 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'ENDORSE_DELEGATE_HISTORY_TBL' (TABLE)
20 INDEX MODE: ANALYZED (RANGE SCAN) OF
'ENDORSE_DELEGATE_HIST_PK' (INDEX (UNIQUE))
SQL ID: cf06fwacdmgfk Plan Hash: 1546270724
select 'x'
from
dual
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 1 0.00 0.00 0 0 0 1
total 3 0.00 0.00 0 0 0 1
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 331 (CA17062)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
1 1 1 FAST DUAL (cr=0 pr=0 pw=0 time=6 us cost=2 size=0 card=1)
Rows Execution Plan
0 SELECT STATEMENT MODE: ALL_ROWS
1 FAST DUAL
OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
Parse 5 0.00 0.00 0 0 0 0
Execute 6 0.00 0.00 0 0 0 3
Fetch 2 6.11 14.77 38 455 0 7
total 13 6.11 14.77 38 455 0 10
Misses in library cache during parse: 0
Misses in library cache during execute: 1
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
Parse 2 0.00 0.00 0 0 0 0
Execute 24300 0.97 1.51 0 0 0 0
Fetch 24300 56.49 135.87 788 9331822 0 570
total 48602 57.46 137.39 788 9331822 0 570
Misses in library cache during parse: 0
6 user SQL statements in session.
0 internal SQL statements in session.
6 SQL statements in session.
4 statements EXPLAINed in this session.
Trace file: TAST_ora_26885.trc
Trace file compatibility: 11.1.0.7
Sort options: default
1 session in tracefile.
6 user SQL statements in trace file.
0 internal SQL statements in trace file.
6 SQL statements in trace file.
6 unique SQL statements in trace file.
4 SQL statements EXPLAINed using schema:
dbo.plan_table
Schema was specified.
Existing table was used.
73341 lines in trace file.
178 elapsed seconds in trace file.
Here is the good query
SQL ID: 0g07hqvjma217 Plan Hash: 0
alter session set sql_trace = true
call count cpu elapsed disk query current rows
Parse 0 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 0 0.00 0.00 0 0 0 0
total 1 0.00 0.00 0 0 0 0
Misses in library cache during parse: 0
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 331 (CA17062)
SQL ID: 9m7787camwh4m Plan Hash: 0
begin :id := sys.dbms_transaction.local_transaction_id; end;
call count cpu elapsed disk query current rows
Parse 3 0.00 0.00 0 0 0 0
Execute 3 0.00 0.00 0 0 0 3
Fetch 0 0.00 0.00 0 0 0 0
total 6 0.00 0.00 0 0 0 3
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 331 (CA17062)
SELECT period, biweek_start, biweek_end, pay_period_complete, worker_count
FROM (SELECT to_char(en.start_date, 'yyyy/mm/dd') || ' to ' ||
to_char(en.start_date + 13, 'yyyy/mm/dd') period,
en.start_date biweek_start,
en.start_date + 13 biweek_end,
decode(sign(sysdate - en.start_date - 13),
-1,
'In Progress',
'Completed') pay_period_complete,
ta_mssauth_pkg.actual_endorser(en.start_date, '33811') worker_count
FROM (select worker_id,
default_endorser resolved_endorser,
effective_date,
expiration_date
from endorse_delegate_history_v
where default_endorser = '33811'
union
select worker_id,
temporary_delegate resolved_endorser,
effective_date,
expiration_date
from endorse_delegate_history_v
where temporary_delegate = '33811'
and temporary_delegate is not null ) de,
endorse_activity_v en
WHERE en.worker_id = de.worker_id
AND en.endorse_status = 'N'
AND en.start_date <= de.expiration_date
AND en.start_date + 13 >= de.effective_date
GROUP BY en.start_date)
-- WHERE worker_count > 0
call count cpu elapsed disk query current rows
Parse 1 0.02 0.03 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 1 0.02 0.13 0 841 0 6
total 3 0.04 0.16 0 841 0 6
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 331 (CA17062)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
6 6 6 HASH GROUP BY (cr=841 pr=0 pw=0 time=79927 us cost=617 size=1025 card=25)
21 21 21 NESTED LOOPS (cr=841 pr=0 pw=0 time=15116 us)
451 451 451 NESTED LOOPS (cr=409 pr=0 pw=0 time=33039 us cost=616 size=1025 card=25)
213 213 213 VIEW (cr=32 pr=0 pw=0 time=12117 us cost=58 size=4464 card=186)
213 213 213 SORT UNIQUE (cr=32 pr=0 pw=0 time=12005 us cost=58 size=5392 card=186)
213 213 213 UNION-ALL (cr=32 pr=0 pw=0 time=1313 us)
15 15 15 TABLE ACCESS BY INDEX ROWID ENDORSE_DELEGATE_HISTORY_TBL (cr=10 pr=0 pw=0 time=245 us cost=4
2 size=4170 card=139)
15 15 15 INDEX RANGE SCAN ENDORSE_DELEGATE_HIST_DEF_IDX (cr=3 pr=0 pw=0 time=122 us cost=3 size=0 ca
rd=139)(object id 54559)
198 198 198 TABLE ACCESS BY INDEX ROWID ENDORSE_DELEGATE_HISTORY_TBL (cr=22 pr=0 pw=0 time=427 us cost=1
4 size=1222 card=47)
198 198 198 INDEX RANGE SCAN ENDORSE_DELEGATE_HIST_TEMP_IDX (cr=3 pr=0 pw=0 time=222 us cost=3 size=0 c
ard=47)(object id 54560)
451 451 451 INDEX RANGE SCAN TA_SCLENDORSE_PK (cr=377 pr=0 pw=0 time=63328 us cost=2 size=0 card=1)(object
id 54553)
21 21 21 TABLE ACCESS BY INDEX ROWID ENDORSE_ACTIVITY_TBL (cr=432 pr=0 pw=0 time=2185 us cost=3 size=17 c
ard=1)
Rows Execution Plan
0 SELECT STATEMENT MODE: ALL_ROWS
6 HASH (GROUP BY)
21 NESTED LOOPS
451 NESTED LOOPS
213 VIEW
213 SORT (UNIQUE)
213 UNION-ALL
15 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'ENDORSE_DELEGATE_HISTORY_TBL' (TABLE)
15 INDEX MODE: ANALYZED (RANGE SCAN) OF
'ENDORSE_DELEGATE_HIST_DEF_IDX' (INDEX)
198 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'ENDORSE_DELEGATE_HISTORY_TBL' (TABLE)
198 INDEX MODE: ANALYZED (RANGE SCAN) OF
'ENDORSE_DELEGATE_HIST_TEMP_IDX' (INDEX)
451 INDEX MODE: ANALYZED (RANGE SCAN) OF 'TA_SCLENDORSE_PK'
(INDEX (UNIQUE))
21 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF
'ENDORSE_ACTIVITY_TBL' (TABLE)
SQL ID: gwpkaabt9naxy Plan Hash: 2668568437
SELECT DISTINCT DE.WORKER_ID
FROM
ENDORSE_DELEGATE_HISTORY_V DE , ENDORSE_ACTIVITY_V EN ,
WORKER_ACTIVITY_AUTH_V EA WHERE DE.WORKER_ID = EN.WORKER_ID AND
EN.WORKER_ID = EA.WORKER_ID AND EN.START_DATE = :B2 AND EN.ENDORSE_STATUS =
'N' AND EA.ACTIVITY_DATE >= EN.START_DATE AND EA.ACTIVITY_DATE <=
EN.START_DATE + 13 AND EN.START_DATE <= DE.EXPIRATION_DATE AND
EN.START_DATE + 13 >= DE.EFFECTIVE_DATE AND (DE.DEFAULT_ENDORSER = :B1 OR
DE.TEMPORARY_DELEGATE = :B1 ) ORDER BY DE.WORKER_ID
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 6 0.01 0.00 0 0 0 0
Fetch 6 0.01 0.01 0 700 -
Tuning SQL query with similar subqueries for select columns
Hi all,
My query is something like below:
1> SELECT
2> A.COL1,
3> SUM(CASE WHEN A.flag=100 AND NVL(B.flag,0)=0 AND
4> EXISTS (
5> SELECT 'ROW_EXISTS'
6> FROM A A0
7> WHERE A0.COL2=100 AND NVL(A0.flag,0)=0 AND 0.DIRN<>A.DIRN)
8> THEN 1
9> ELSE 0
10> END) SUM_COLUMN1,
11>SUM(CASE WHEN A.flag=100 AND
12> EXISTS (
13> SELECT 'ROW_EXISTS'
14> FROM A A0
15> WHERE A0.COL2=100 AND A0.DIRN<>A.DIRN)
16> THEN 1
17> ELSE 0
18>END) SUM_COLUMN2
19>FROM A,B
20>WHERE A.COL=B.KEY_COL
21>GROUP BY A.COL1
My problem is that I need to index scan the tables A aliased as A0 in the two exist queries. Lines: 5-7 and 13-15.
The major performance degradation i see with the query is that it has to scan A0 twice to compute SUM_COLUMN1 and SUM_COLUMN2.
Is there any way by which i can use the same subquery to scan the table just once to compute the columns.
The table A has more than a 100million records. So i need to improve performance of this query though it is a small issue.
Please feel free to ask me more if i'm not clear with the example.
Thanks in advance,
RajAlso you could use analytic functions:
SQL> select deptno, sum(case when e.mgr is not null and
2 exists (select 1 from emp e1 where e1.empno <> e.empno
3 and e1.deptno = 10 and e1.sal >= 1500)
4 then 1 else 0 end) sum1,
5 sum(case when e.mgr is not null and e.sal >= 1200 and
6 exists (select 1 from emp e1 where e1.empno <> e.empno and e1.deptno = 10)
7 then 1 else 0 end) sum2
8 from emp e
9 group by deptno
10 /
DEPTNO SUM1 SUM2
10 2 2
20 5 3
30 6 5
SQL> select deptno, sum(case when mgr is not null and
2 (d12 != empno or empno !=d11) then 1 else 0 end) sum1,
3 sum(case when mgr is not null and (d21 != empno or empno !=d22)
4 and sal >= 1200 then 1 else 0 end) sum2
5 from (
6 select deptno, empno, mgr, sal,
7 min(case when deptno = 10 and sal >= 1500
8 then empno else -1 end) over(order by null) d11,
9 max(case when deptno = 10 and sal >= 1500
10 then empno else -1 end) over(order by null) d12,
11 min(case when deptno = 10 then empno else -1 end) over(order by null) d21,
12 max(case when deptno = 10 then empno else -1 end) over(order by null) d22
13 from emp e
14 )
15 group by deptno
16 /
DEPTNO SUM1 SUM2
10 2 2
20 5 3
30 6 5In your case it could be something like
SELECT COL1,
SUM( CASE WHEN a_flag = 100 and nvl(b_flag,0)=0 and
(DIRN != MIN_A OR DIRN != MAX_A) THEN 1 ELSE 0 END) SUM_COLUMN1,
SUM( CASE WHEN a_flag = 100 and
(DIRN != MIN_B OR DIRN != MAX_B) THEN 1 ELSE 0 END) SUM_COLUMN2
FROM (
SELECT A.COL1, A.FLAG A_FLAG, B.FLAG B_FLAG, A.DIRN,
MIN(CASE WHEN A.COL2=100 and NVL(A.flag,0) = 0 THEN A.DIRN ELSE null END) OVER(order by null) MIN_A,
MAX(CASE WHEN A.COL2=100 and NVL(A.flag,0) = 0 THEN A.DIRN ELSE null END) OVER(order by null) MAX_A,
MIN(CASE WHEN A.COL2=100 THEN A.DIRN ELSE null END) OVER(order by null) MIN_B,
MAX(CASE WHEN A.COL2=100 THEN A.DIRN ELSE null END) OVER(order by null) MAX_B
FROM A,B
WHERE A.COL=B.KEY_COL
GROUP BY COL1
Rgds. -
Tuning SQL Query for base tables
Hi,
Can any please help me to tune the below query...... there is full table scan for po_action_history table
SELECT DISTINCT
DECODE(hr.full_name,'Default Buyer, Purchasing','PCH',
'Default Buyer, Travel','TRV',
'Purchase Order, Rapid','RPO',
'Data Interchange, Electronic','EDI',fuser1.user_name) BUYER_ID,
poa.attribute1 BUYER_GROUP,
poa.attribute2 BUYER_SUBGROUP
FROM po.po_action_history ac1,
po.po_headers_all ph,
po.po_agents poa,
hr.per_all_people_f hr,
applsys.fnd_user fuser1
WHERE ph.po_header_id = ac1.object_id
AND ac1.object_type_code = 'PO'
AND ac1.action_code = 'APPROVE'
AND sequence_num =(SELECT MIN(ac2.sequence_num)
FROM po.po_action_history ac2,
applsys.fnd_user fuser2
WHERE ph.po_header_id = ac2.object_id
AND ac2.object_type_code = 'PO'
AND ac2.action_code = 'APPROVE'
AND ac2.last_updated_by = fuser2.user_id)
--AND ac1.last_updated_by = fuser1.user_id
AND ph.agent_id = poa.agent_id
AND ph.agent_id = hr.person_id
AND hr.person_id = fuser1.employee_id
AND hr.effective_end_date > sysdate
AND trunc(ac1.last_update_date) between '01-JAN-08' and '07-JAN-08'
ORDER BY poa.attribute1
Explain Plan
PLAN_TABLE_OUTPUT
Plan hash value: 3325319312
Id Operation Name Rows
Bytes Cost (%CPU) Time
PLAN_TABLE_OUTPUT
| 0 | SELECT STATEMENT | | 27 |
2754 | 38963 (3) | 00:07:48 |
| 1 | SORT UNIQUE | | 27 |
2754 | 38962 (3) | 00:07:48 |
|* 2 | FILTER | | |
| | |
|* 3 | HASH JOIN | | 37 |
3774 | 36308 (3) | 00:07:16 |
PLAN_TABLE_OUTPUT
| 4 | TABLE ACCESS BY INDEX ROWID | FND_USER | 1 |
13 | 2 (0) | 00:00:01 |
| 5 | NESTED LOOPS | | 37 |
3293 | 36303 (3) | 00:07:16 |
| 6 | NESTED LOOPS | | 88 |
6688 | 36180 (3) | 00:07:15 |
| 7 | NESTED LOOPS | | 88 |
PLAN_TABLE_OUTPUT
3960 | 35916 (3) | 00:07:11 |
|* 8 | TABLE ACCESS FULL | PO_ACTION_HISTORY | 2110 |
71740 | 32478 (3) | 00:06:30 |
| 9 | TABLE ACCESS BY INDEX ROWID | PO_HEADERS_ALL | 1 |
11 | 2 (0) | 00:00:01 |
|* 10 | INDEX UNIQUE SCAN | PO_HEADERS_U1 | 1 |
| 1 (0) | 00:00:01 |
PLAN_TABLE_OUTPUT
| 11 | SORT AGGREGATE | | 1 |
36 | | |
| 12 | NESTED LOOPS | | 1 |
36 | 6 (0) | 00:00:01 |
|* 13 | TABLE ACCESS BY INDEX ROWID| PO_ACTION_HISTORY | 1 |
31 | 6 (0) | 00:00:01 |
|* 14 | INDEX RANGE SCAN | PO_ACTION_HISTORY_N1 | 5 |
| 3 (0) | 00:00:01 |
PLAN_TABLE_OUTPUT
|* 15 | INDEX UNIQUE SCAN | FND_USER_U1 | 1 |
5 | 0 (0) | 00:00:01 |
| 16 | TABLE ACCESS BY INDEX ROWID | PER_ALL_PEOPLE_F | 1 |
31 | 3 (0) | 00:00:01 |
|* 17 | INDEX RANGE SCAN | PER_PEOPLE_F_PK | 1 |
| 2 (0) | 00:00:01 |
|* 18 | INDEX RANGE SCAN | FND_USER_N1 | 1 |
PLAN_TABLE_OUTPUT
| 1 (0) | 00:00:01 |
| 19 | TABLE ACCESS FULL | PO_AGENTS | 183 |
2379 | 4 (0) | 00:00:01 |
Predicate Information (identified by operation id):
PLAN_TABLE_OUTPUT
2 - filter(TO_DATE('01-JAN-08')<=TO_DATE('07-JAN-08'))
3 - access("PH"."AGENT_ID"="POA"."AGENT_ID")
8 - filter("AC1"."ACTION_CODE"='APPROVE' AND "AC1"."OBJECT_TYPE_CODE"='PO'AND
TRUNC(INTERNAL_FUNCTION("AC1"."LAST_UPDATE_DATE"))>='01-JAN-08' AND
TRUNC(INTERNAL_FUNCTION("AC1"."LAST_UPDATE_DATE"))<='07-JAN-08')
10 - access("PH"."PO_HEADER_ID"="AC1"."OBJECT_ID")
PLAN_TABLE_OUTPUT
filter("SEQUENCE_NUM"= (SELECT MIN("AC2"."SEQUENCE_NUM") FROM "APPLSYS"."FND_USER"
"FUSER2","PO"."PO_ACTION_HISTORY" "AC2" WHERE "AC2"."OBJECT_ID"=:B1 AND "AC2"."ACTION_CODE"='APPROVE'
AND "AC2"."OBJECT_TYPE_CODE"='PO' AND "AC2"."LAST_UPDATED_BY"="FUSER2"."USER_ID"))
13 - filter("AC2"."ACTION_CODE"='APPROVE' AND "AC2"."OBJECT_TYPE_CODE"='PO')
14 - access("AC2"."OBJECT_ID"=:B1)
PLAN_TABLE_OUTPUT
15 - access("AC2"."LAST_UPDATED_BY"="FUSER2"."USER_ID")
17 - access("PH"."AGENT_ID"="PERSON_ID" AND "EFFECTIVE_END_DATE">SYSDATE@!)
filter("EFFECTIVE_END_DATE">SYSDATE@!)
18 - access("PERSON_ID"="FUSER1"."EMPLOYEE_ID")
ThanksHi,
any help for the above issue. -
How can i do SQl QUERY TUNING in Oracle 9i Database
Hello,
How can i do SQl QUERY TUNING in Oracle 9i Database You can start through reviewing the Performance Tuning Guide;
http://download.oracle.com/docs/cd/B10501_01/server.920/a96533/toc.htm
Good Luck!
Adith -
HI.
can somebody post me any links for ebooks on SQL Query tuning techniques for either 10g or 11g.
Tuning SQL Queries is something I would like to build my expertise on..
There may be a lot of advisors like SQL Query Advisor, Access Advisor and so on...but nothing like tuning your queries manually to maximum optimization.
Kindly help me in this !!Check Oracle docs http://download.oracle.com/docs/cd/B19306_01/server.102/b14211/toc.htm
But you should really also be checking:
http://www.asktom.oracle.com
http://tkyte.blogspot.com
AND all other blogs he referres to ( like Jonathan Lewis' site and blog ) you'll find many interesting articles over there, from 'the real world', regarding performance and tuning.
Edited by: hoek on Mar 24, 2009 10:40 AM -
Dear Experts,
I have a sql query which taking more than 2 hour of time ot execute.
the explain plan is :
PLAN_TABLE_OUTPUT
Plan hash value: 2694368390
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 379 | 44561 (1)| 00:08:55 |
| 1 | INLIST ITERATOR | | | | | |
| 2 | TABLE ACCESS BY INDEX ROWID | OPS_CITY_MAST | 2 | 30 | 5 (0)| 00:00:01 |
|* 3 | INDEX UNIQUE SCAN | OPS_CITY_MAST_IDX_01 | 2 | | 3 (0)| 00:00:01 |
| 4 | TABLE ACCESS BY INDEX ROWID | OPS_BR_MAST | 1 | 16 | 2 (0)| 00:00:01 |
|* 5 | INDEX UNIQUE SCAN | OPS_BR_MAST_IDX_01 | 1 | | 1 (0)| 00:00:01 |
| 6 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
|* 7 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
| 8 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
|* 9 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
| 10 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
|* 11 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
| 12 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
|* 13 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
| 14 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
|* 15 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
| 16 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
|* 17 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
| 18 | SORT GROUP BY NOSORT | | 1 | 31 | 10 (0)| 00:00:01 |
| 19 | NESTED LOOPS | | | | | |
| 20 | NESTED LOOPS | | 2 | 62 | 10 (0)| 00:00:01 |
| 21 | TABLE ACCESS BY INDEX ROWID | OPS_UULT_WB_DTLS | 2 | 24 | 6 (0)| 00:00:01 |
|* 22 | INDEX RANGE SCAN | OPS_UULT_WB_DTLS_IDX_03 | 2 | | 3 (0)| 00:00:01 |
|* 23 | INDEX UNIQUE SCAN | OPS_UPD_ULT_IDX_01 | 1 | | 1 (0)| 00:00:01 |
|* 24 | TABLE ACCESS BY INDEX ROWID | OPS_UPD_ULT | 1 | 19 | 2 (0)| 00:00:01 |
| 25 | NESTED LOOPS | | | | | |
| 26 | NESTED LOOPS | | 1 | 379 | 44561 (1)| 00:08:55 |
| 27 | NESTED LOOPS | | 1 | 360 | 44559 (1)| 00:08:55 |
| 28 | NESTED LOOPS | | 1 | 333 | 44558 (1)| 00:08:55 |
| 29 | NESTED LOOPS | | 1 | 312 | 44557 (1)| 00:08:55 |
| 30 | NESTED LOOPS | | 1 | 302 | 44555 (1)| 00:08:55 |
| 31 | NESTED LOOPS | | 1 | 281 | 44553 (1)| 00:08:55 |
|* 32 | HASH JOIN | | 4383 | 1112K| 35779 (2)| 00:07:10 |
|* 33 | HASH JOIN RIGHT OUTER | | 4383 | 1070K| 34631 (2)| 00:06:56 |
| 34 | TABLE ACCESS FULL | OPS_CUST_CNTR | 7270 | 94510 | 68 (0)| 00:00:01 |
|* 35 | HASH JOIN | | 4383 | 1014K| 34562 (2)| 00:06:55 |
| 36 | NESTED LOOPS OUTER | | 4414 | 875K| 33135 (2)| 00:06:38 |
|* 37 | HASH JOIN | | 4414 | 827K| 32963 (2)| 00:06:36 |
| 38 | TABLE ACCESS FULL | OPS_ST_UN_MAST | 36 | 504 | 3 (0)| 00:00:01 |
|* 39 | HASH JOIN | | 4414 | 767K| 32959 (2)| 00:06:36 |
|* 40 | HASH JOIN | | 4414 | 543K| 28417 (2)| 00:05:41 |
| 41 | NESTED LOOPS | | | | | |
| 42 | NESTED LOOPS | | 4414 | 495K| 26483 (2)| 00:05:18 |
|* 43 | HASH JOIN | | 4949 | 483K| 16641 (2)| 00:03:20 |
|* 44 | TABLE ACCESS BY INDEX ROWID| OPS_WAYBL | 4423 | 367K| 2292 (1)| 00:00:28 |
|* 45 | INDEX RANGE SCAN | OPS_WAYBL_IDX_11 | 5050 | | 16 (0)| 00:00:01 |
| 46 | TABLE ACCESS FULL | OPS_PULTD_WB_DTLS | 4474K| 64M| 14298 (2)| 00:02:52 |
|* 47 | INDEX UNIQUE SCAN | OPS_TS_RECONSILE_IDX_02 | 1 | | 1 (0)| 00:00:01 |
|* 48 | TABLE ACCESS BY INDEX ROWID | OPS_TS_RECONSILE | 1 | 15 | 2 (0)| 00:00:01 |
| 49 | TABLE ACCESS FULL | OPS_CC_CORCEE_ADDR | 998K| 10M| 1922 (2)| 00:00:24 |
| 50 | TABLE ACCESS FULL | OPS_ADDR_MAST | 1006K| 49M| 4531 (1)| 00:00:55 |
| 51 | TABLE ACCESS BY INDEX ROWID | OPS_WB_DOD_DTLS | 1 | 11 | 1 (0)| 00:00:01 |
|* 52 | INDEX UNIQUE SCAN | OPS_WB_DOD_DTLS_IDX_02 | 1 | | 0 (0)| 00:00:01 |
| 53 | TABLE ACCESS FULL | OPS_TRIP_SHT | 423K| 13M| 1422 (2)| 00:00:18 |
| 54 | TABLE ACCESS FULL | OPS_PROV_LT_DLVRY | 446K| 4361K| 1142 (2)| 00:00:14 |
| 55 | TABLE ACCESS BY INDEX ROWID | OPS_PLTD_WB_DTLS | 1 | 21 | 2 (0)| 00:00:01 |
|* 56 | INDEX UNIQUE SCAN | OPS_PLTD_WB_DTLS_IDX_04 | 1 | | 1 (0)| 00:00:01 |
| 57 | TABLE ACCESS BY INDEX ROWID | OPS_ULTD_WB_DTLS | 1 | 21 | 2 (0)| 00:00:01 |
|* 58 | INDEX UNIQUE SCAN | OPS_ULTD_WB_DTLS_IDX_02 | 1 | | 1 (0)| 00:00:01 |
|* 59 | TABLE ACCESS BY INDEX ROWID | OPS_UPD_LT_DLVRY | 1 | 10 | 2 (0)| 00:00:01 |
|* 60 | INDEX UNIQUE SCAN | OPS_UPD_LT_DLVRY_IDX_01 | 1 | | 1 (0)| 00:00:01 |
| 61 | TABLE ACCESS BY INDEX ROWID | OPS_TRPT_VHLS | 1 | 21 | 1 (0)| 00:00:01 |
|* 62 | INDEX UNIQUE SCAN | OPS_TRPT_VHLS_IDX_01 | 1 | | 0 (0)| 00:00:01 |
| 63 | TABLE ACCESS BY INDEX ROWID | PO_VENDORS | 1 | 27 | 1 (0)| 00:00:01 |
|* 64 | INDEX UNIQUE SCAN | VENDOR_UNIQUE | 1 | | 0 (0)| 00:00:01 |
|* 65 | INDEX UNIQUE SCAN | OPS_GATE_PASS_IDX_01 | 1 | | 1 (0)| 00:00:01 |
|* 66 | TABLE ACCESS BY INDEX ROWID | OPS_GATE_PASS | 1 | 19 | 2 (0)| 00:00:01 |
Predicate Information (identified by operation id):
3 - access("OCM"."ID"=:B1 OR "OCM"."ID"=:B2)
5 - access("OBM"."ID"=:B1)
7 - access("CGA"."DOC_TYPE"='WB' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=6)
9 - access("CGA"."DOC_TYPE"='WB' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=7)
11 - access("CGA"."DOC_TYPE"='GP' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=15)
13 - access("CGA"."DOC_TYPE"='GP' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=16)
15 - access("CGA"."DOC_TYPE"='GP' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=17)
17 - access("CGA"."DOC_TYPE"='GP' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=23)
22 - access("DT"."WAYBL_ID"=:B1)
23 - access("ULT"."ID"="DT"."UPD_ULT_ID")
24 - filter("ULT"."FROM_BR_MAST_ID"=:B1)
32 - access("OPL"."ID"="TS"."PROV_LT_DLVRY_ID")
33 - access("CNTR"."ID"(+)="SYS_ALIAS_10"."CUST_CNTR_ID")
35 - access("TSREC"."TRIP_SHT_ID"="TS"."ID")
37 - access("ST"."ID"="AD"."ST_UN_MAST_ID")
39 - access("AD"."ID"="CCADD"."ADDR_MAST_ID")
40 - access("CCADD"."ID"="SYS_ALIAS_10"."CC_CEE_ADDR_ID")
43 - access("SYS_ALIAS_10"."ID"="DTL"."WAYBL_ID")
44 - filter("SYS_ALIAS_10"."GL_TRFD" IS NULL OR "SYS_ALIAS_10"."GL_TRFD"='Y')
45 - access(TRUNC(INTERNAL_FUNCTION("FIRST_DLVRY_DT"))=TO_DATE(' 2011-08-15 00:00:00', 'syyyy-mm-dd
hh24:mi:ss'))
47 - access("DTL"."ID"="TSREC"."PULTD_WB_DTLS_ID")
48 - filter("TSREC"."STATUS_LID"=157)
52 - access("SYS_ALIAS_10"."ID"="OWD"."WAYBL_ID"(+))
56 - access("LTDTL"."PROV_LT_DLVRY_ID"="OPL"."ID" AND "LTDTL"."WAYBL_ID"="DTL"."WAYBL_ID")
58 - access("ULTDTL"."PLTD_WB_DTLS_ID"="LTDTL"."ID")
59 - filter("UPLT"."PROV_LT_DLVRY_ID"="OPL"."ID")
60 - access("ULTDTL"."UPD_LT_DLVRY_ID"="UPLT"."ID")
62 - access("OTV"."ID"="OPL"."TRPT_VHLS_ID")
64 - access("PO"."VENDOR_ID"="OTV"."VENDOR_ID")
65 - access("SYS_ALIAS_9"."ID"="ULTDTL"."GATE_PASS_ID")
66 - filter("SYS_ALIAS_9"."GL_TRFD" IS NULL OR "SYS_ALIAS_9"."GL_TRFD"='Y')
so, please help me.
Regards,
Viveka Nand
Edited by: 891502 on Oct 14, 2011 4:39 AM891502 wrote:
now am putted in the correct format,In the link it tells you to provide the query. Which you have not done.
It tells you how to format the plan. Which you have not done.
It tells you to provide database version and optimizer parameters. Which you have not done.
It tells you to provide autotrace statistics and trace output. Which you have not done.
So in what way is this format correct ?
so, please give me the way to resolved it.With the information you have provided we can say with certainty that if you write your query this way it will be fast.
select null from dual; -
SQL Query Performance needed.
Hi All,
I am getting performance issue with my below sql query. When I fired it, It is taking 823.438 seconds, but when I ran query in, it is taking 8.578 seconds, and query after in is taking 7.579 seconds.
SELECT BAL.L_ID, BAL.L_TYPE, BAL.L_NAME, BAL.NATURAL_ACCOUNT,
BAL.LOCATION, BAL.PRODUCT, BAL.INTERCOMPANY, BAL.FUTURE1, BAL.FUTURE2, BAL.CURRENCY, BAL.AMOUNT_PTD, BAL.AMOUNT_YTD, BAL.CREATION_DATE,
BAL.CREATED_BY, BAL.LAST_UPDATE_DATE, BAL.LAST_UPDATED_BY, BAL.STATUS, BAL.ANET_STATUS, BAL.COG_STATUS, BAL.comb_id, BAL.MESSAGE,
SEG.SEGMENT_DESCRIPTION FROM ACC_SEGMENTS_V_TST SEG , ACC_BALANCE_STG BAL where BAL.NATURAL_ACCOUNT = SEG.SEGMENT_VALUE AND SEG.SEGMENT_COLUMN = 'SEGMENT99' AND BAL.ACCOUNTING_PERIOD = 'MAY-10' and BAL.comb_id
in
(select comb_id from
(select comb_id, rownum r from
(select distinct(comb_id),LAST_UPDATE_DATE from ACC_BALANCE_STG where accounting_period='MAY-10' order by LAST_UPDATE_DATE )
where rownum <=100) where r >0)
Please help me in fine tuning above. I am using Oracle 10g database. There are total of 8000 records. Let me know if any other info required.
Thanks in advance.In recent versions of Oracle an EXISTS predicate should produce the same execution plan as the corresponding IN clause.
Follow the advice in the tuning threads as suggested by SomeoneElse.
It looks to me like you could avoid the double pass on ACC_BALANCE_STG by using an analytical function like ROW_NUMBER() and then joining to ACC_SEGMENTS_V_TST SEG, maybe using subquery refactoring to make it look nicer.
e.g. something like (untested)
WITH subq_bal as
((SELECT *
FROM (SELECT BAL.L_ID, BAL.L_TYPE, BAL.L_NAME, BAL.NATURAL_ACCOUNT,
BAL.LOCATION, BAL.PRODUCT, BAL.INTERCOMPANY, BAL.FUTURE1, BAL.FUTURE2,
BAL.CURRENCY, BAL.AMOUNT_PTD, BAL.AMOUNT_YTD, BAL.CREATION_DATE,
BAL.CREATED_BY, BAL.LAST_UPDATE_DATE, BAL.LAST_UPDATED_BY, BAL.STATUS, BAL.ANET_STATUS,
BAL.COG_STATUS, BAL.comb_id, BAL.MESSAGE,
ROW_NUMBER() OVER (ORDER BY LAST_UPDATE_DATE) rn
FROM acc_balance_stg
WHERE accounting_period='MAY-10')
WHERE rn <= 100)
SELECT *
FROM subq_bal bal
, acc_Segments_v_tst seg
where BAL.NATURAL_ACCOUNT = SEG.SEGMENT_VALUE
AND SEG.SEGMENT_COLUMN = 'SEGMENT99';However, the parentheses you use around comb_id make me question what your intention is here in the subquery?
Do you have multiple rows in ACC_BALANCE_STG for the same comb_id and last_update_date?
If so you may want to do a MAX on last_update_date, group by comb_id before doing the analytic restriction.
Edited by: DomBrooks on Jun 16, 2010 5:56 PM -
Hi,
Facing Database performance issue while runing overnight batches.
Generate tfprof output for that batch and found some sql query which is having high elapsed time. Could any one please let me know what is the issue for this. It will also be great help if anyone suggest what need to be done as per tuning of this sql queries so as to get better responce time.
Waiting for your reply.
Effected SQL List:
INSERT INTO INVTRNEE (TRANS_SESSION, TRANS_SEQUENCE, TRANS_ORG_CHILD,
TRANS_PRD_CHILD, TRANS_TRN_CODE, TRANS_TYPE_CODE, TRANS_DATE, INV_MRPT_CODE,
INV_DRPT_CODE, TRANS_CURR_CODE, PROC_SOURCE, TRANS_REF, TRANS_REF2,
TRANS_QTY, TRANS_RETL, TRANS_COST, TRANS_VAT, TRANS_POS_EXT_TOTAL,
INNER_PK_TECH_KEY, TRANS_INNERS, TRANS_EACHES, TRANS_UOM, TRANS_WEIGHT,
TRANS_WEIGHT_UOM )
VALUES
(:B22 , :B1 , :B2 , :B3 , :B4 , :B5 , :B21 , :B6 , :B7 , :B8 , :B20 , :B19 ,
NULL, :B9 , :B10 , :B11 , 0.0, :B12 , :B13 , :B14 , :B15 , :B16 , :B17 ,
:B18 )
call count cpu elapsed disk query current rows
Parse 722 0.09 0.04 0 0 0 0
Execute 1060 7.96 83.01 11442 21598 88401 149973
Fetch 0 0.00 0.00 0 0 0 0
total 1782 8.05 83.06 11442 21598 88401 149973
Misses in library cache during parse: 1
Optimizer goal: CHOOSE
UPDATE /*+ ROWID(TRFDTLEE) */TRFDTLEE SET TRF_STATUS = :B2
WHERE
ROWID = :B1
call count cpu elapsed disk query current rows
Parse 635 0.03 0.01 0 0 0 0
Execute 49902 14.48 271.25 41803 80704 355837 49902
Fetch 0 0.00 0.00 0 0 0 0
total 50537 14.51 271.27 41803 80704 355837 49902
Misses in library cache during parse: 1
Optimizer goal: CHOOSE
DECLARE
var_trans_session invtrnee.trans_session%TYPE;
BEGIN
-- ADDED BY SHANKAR ON 08/29/97
-- GET THE NEXT AVAILABLE TRANS_SESSION
bastkey('trans_session',0,var_trans_session,'T');
-- MAS001
uk_trfbapuo_auto(var_trans_session,'UPLOAD','T',300);
-- MAS001 end
END;
call count cpu elapsed disk query current rows
Parse 0 0.00 0.00 0 0 0 0
Execute 1 24191.23 24028.57 8172196 10533885 187888 1
Fetch 0 0.00 0.00 0 0 0 0
total 1 24191.23 24028.57 8172196 10533885 187888 1
Misses in library cache during parse: 0
Misses in library cache during execute: 1
Optimizer goal: CHOOSE
SELECT INNER_PK_TECH_KEY
FROM
PRDPCDEE WHERE PRD_LVL_CHILD = :B1 AND LOOSE_PACK_FLAG = 'T'
call count cpu elapsed disk query current rows
Parse 1 0.01 0.00 0 0 0 0
Execute 56081 1.90 2.03 0 0 0 0
Fetch 56081 11.07 458.58 53792 246017 0 56081
total 112163 12.98 460.61 53792 246017 0 56081
Misses in library cache during parse: 1
Optimizer goal: CHOOSE
******************First off, be aware of the assumptions I'm making. The SQL you presented above strongly suggests (to me at least) that you have cursor for loops. If that's the case, you need to review what their purpose is and look to convert them into single statement DML commands. For example if you have something like this
DECLARE
ln_Count NUMBER;
ln_SomeValue NUMBER;
BEGIN
FOR lcr_Row IN ( SELECT pk_id,col1,col2 FROM some_table)
LOOP
SELECT
COUNT(*)
INTO
ln_COunt
FROM
target_table
WHERE
pk_id = lcr_Row.pk_id;
IF ln_Count = 0 THEN
SELECT
some_value
INTO
ln_SomeValue
FROM
some_other_table
WHERE
pk_id = lcr_Row.col1
INSERT
INTO
target_table
( pk_id,
some_other_value,
col2
VALUES
( lcr_Row.col1,
ln_SomeValue,
lcr_Row.col2
ELSE
UPDATE
target_table
SET
some_other_value = ln_SomeValue
WHERE
pk_id = lcr_Row.col1;
END IF;
END LOOP;
END; it could be rewritten as
DECLARE
BEGIN
MERGE INTO target_table b
USING ( SELECT
a.pk_id,
a.col2,
b.some_value
FROM
some_table a,
some_other_table b
WHERE
b.pk_id = a.col1
) e
ON (b.pk_id = e.pk_id)
WHEN MATCHED THEN
UPDATE SET b.some_other_value = e.some_value
WHEN NOT MATCHED THEN
INSERT ( b.pk_id,
b.col2,
b.some_other_value)
VALUES( b.pk_id,
b.col2,
b.some_value);
END;It's going to take a bit of analysis and work but the fastest and most scalable way to approach processing data is to use SQL rather than PL/SQL. PL/SQL data processing i.e. cursor loops should be an option of last resort.
HTH
David -
Doubt in conversion of SQL query to oracle
Hi guys,
I'm new to oracle. I have a SQL query like the following,
Select * From my_table With(Index(PK_my_key))
I want to convert this to oracle. How can I specify the syntax *'With(Index(PK_my_key)) '* in oracle.
Please advice.
RamIs it possible to test that and find out the performance of the query? You need to check the execution plan of the query.
See these informative threads that explain in more detail :
When your query takes too long... When your query takes too long ...
How to post a SQL statement tuning request HOW TO: Post a SQL statement tuning request - template posting
Also, please post your database version (the result of: select * from v$version; ).
INDEX hints are nowadays (almost) never needed, unless you're on ancient database version.
As SomeoneElse already said: let Optimizer decide. -
Performance of OBIEE report whereas SQL query behind it executed in 1-2 min
Facing lot of performance issues in OBIEE reports, whereas SQL query behind the report fetch data in 2-3 minutes. Reprot take around 1-2 hrs. Can anyone help to resolve this please?
(my 2c worth)... if the physical query runs for a reasonably short time but the report takes much longer to return it suggests the rendering process (ie. pivot view) is lagging.
To test this theory, go into Answers and remove all views from the request and leave a simple table view on the compound view. Run the report and the report should return around the same time the physical request takes to run because the table view is a very raw, un aggregated rendering process. Table views are nice and fast. If that works then you can focus on tuning your other views, ie. Pivot view. I've seen a simple pivot view take minutes to render what looks like a simple report because the underlying recordset is quite large.
I hope this helped. another quick trick you could try is applying aggregation in the formulas to aggregate things at the physical sql level so the returning result set isn't 5,000. depends what you're up to with the end report.
Edited by: user8300108 on 25-Feb-2010 04:40
Maybe you are looking for
-
How to place text in arc or write in a full circle
Project for a 4th grade class. How can I write "Happy Mother's Day To: From: " inside of a circle? I want the text to be circular, not straight across inside of a circle. The circle will be printed, cut with scissors and placed in
-
SQL Developer - Adding a column comment via SQL
When I add a comment in SQL*Plus via SQL, such as COMMENT ON COLUMN "MY_OWNER"."MY_TABLE"."MY_COLUMN" IS 'column comment' it works fine. When I try the same thing in SQL Developer I receive a message "No SQL statement entered". I realize this can be
-
Code not working... again :P
This is my 'paddle' game. You bounce a ball around the around the screen, and it's all working. I'm on the final part, which is what happens when you miss the ball (it goes off the left side of the screen). I'd like to print out a message to the midd
-
Upcoming version of DNGProfilEditor
Hello, I would like to know if is there a new version of DNGProfileEditor waiting to be released. Will this new version let the user use other types of colorcharts out of the GM24? Will be possible, inserting someway external colorimetric/spectral da
-
Web Analysis vs. Interactive Reporting
<p>What is the difference between those two?</p><p> </p><p> </p>