Solved

# Optimization of nested queries

Posted on 2008-10-23
414 Views
Is there a more optimized way to wirte the below query, if yes how and why !

select e.DSORGUT as AGENCEDSORGUT, e.CDORGFNC as AGENCECDORGFNC, e.NOBRN as AGENCENOBRN,
f.DSORGUT as AGENCEDSORGUT, f.CDORGFNC as AGENCECDORGFNC, f.NOBRN as AGENCENOBRN,
g.DSORGUT as AGENCEDSORGUT, g.CDORGFNC as AGENCECDORGFNC, g.NOBRN as AGENCENOBRN
from SYNOUP e, SYNOUP f, SYNOUP g
where e.NOINP = (select b.NOINPREL from SYNRIP b, SYNREF c where b.NOINP = c.NOINP and b.CDRIC = 'A' and b.CDRIC IS NOT NULL )
and f.NOINP = (select b.NOINPREL from SYNRIP b, SYNREF c where b.NOINP = c.NOINP and b.CDRIC = 'D' and b.CDRIC IS NOT NULL )
and g.NOINP = (select b.NOINPREL from SYNRIP b, SYNREF c where b.NOINP = c.NOINP and b.CDRIC = 'R' and b.CDRIC IS NOT NULL )
0
Question by:gursheenz2008
• 3

LVL 14

Expert Comment

``````with adr as (

select

max(case when b.CDRIC = 'A' THEN b.NOINPREL else null end) as noinprel_a,

max(case when b.CDRIC = 'D' THEN b.NOINPREL else null end) as noinprel_d,

max(case when b.CDRIC = 'R' THEN b.NOINPREL else null end) as noinprel_r,

from SYNRIP b, SYNREF c

where b.NOINP = c.NOINP and b.CDRIC in ('A', 'D', 'R') and b.CDRIC IS NOT NULL

)

select e.DSORGUT as AGENCEDSORGUT, e.CDORGFNC as AGENCECDORGFNC, e.NOBRN as AGENCENOBRN,

f.DSORGUT as AGENCEDSORGUT, f.CDORGFNC as AGENCECDORGFNC, f.NOBRN as AGENCENOBRN,

g.DSORGUT as AGENCEDSORGUT, g.CDORGFNC as AGENCECDORGFNC, g.NOBRN as AGENCENOBRN

from SYNOUP e, SYNOUP f, SYNOUP g, adr

where e.NOINP = adr.noinprel_a

and f.NOINP = adr.noinprel_d

and g.NOINP = adr.noinprel_r
``````
0

LVL 14

Expert Comment

This techinique is called "Subquery factoring".
0

Author Comment

Thanks for this !

Please correct my understanding !

1.max(case when b.CDRIC = 'A' THEN b.NOINPREL else null end) as noinprel_a,
This will take the MAX of NOINPREL if there's a value, else null value.

Why we again need b.CDRIC in ('A', 'D', 'R')?

2. "with"  Clause will treat the result of the query as a temp table(GTT)/ in line view.

3. This is not a PL/SQL block, it's a SQL statement

with adr as ( ... ) select ...
and g.NOINP = adr.noinprel_r ;

4.When Oracel will drop this temp table(GTT)/ in line view.
0

LVL 73

Expert Comment

is noinp a unique key for synoup?
0

LVL 14

Accepted Solution

GGuzdziol earned 250 total points
1. It may make Oracle uses index on that field.
2&4. Results of the query will be put in temporary table in temporary tablespace. Once query is finished space may be reused by other processes.
0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any othâ€¦
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from thâ€¦
Via a live example, show how to take different types of Oracle backups using RMAN.

#### 728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!