Solved

Optimization of nested queries

Posted on 2008-10-23
5
426 Views
Last Modified: 2013-12-18
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
Comment
Question by:gursheenz2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 14

Expert Comment

by:GGuzdziol
ID: 22784988

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

Open in new window

0
 
LVL 14

Expert Comment

by:GGuzdziol
ID: 22784994
This techinique is called "Subquery factoring".
0
 

Author Comment

by:gursheenz2008
ID: 22785432
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 74

Expert Comment

by:sdstuber
ID: 22785445
is noinp a unique key for synoup?
0
 
LVL 14

Accepted Solution

by:
GGuzdziol earned 250 total points
ID: 22785535
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.
3. Yes, it's SQL statement: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#i2077142
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

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

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

Join & Ask a Question