Solved

Tune Oracle query

Posted on 2011-02-15
6
637 Views
Last Modified: 2012-05-11
Hello experts,

I have one query which is taking more time due to huge records in the tables.
My query returns 300000 records and which takes time.

Please help in tuning this query.

Please find my query below:

WITH a1 AS
     (
        SELECT subscriber_id
          FROM ps_sms_subscrb_tbl a
         WHERE 'A' = a.eff_status
           AND SUBSTR (a.sic_cd, 1, 2) IN
                  ('70',
                   '71',
                   '72',
                   '73',
                   '74',
                   '75',
                   '76',
                   '77',
                   '78',
                   '79',
                   '80',
                   '81',
                   '82',
                   '83',
                   '84',
                   '85',
                   '86',
                   '87',
                   '88',
                   '89'
                  )
           AND a.effdt =
                  (SELECT MAX (a1.effdt)
                     FROM ps_sms_subscrb_tbl a1
                    WHERE a1.subscriber_id = a.subscriber_id
                      AND a1.effdt <= TO_DATE ('12/31/2010', 'MM/DD/YYYY')))
SELECT /*+ INDEX_JOIN(A1) */
       PERCENTILE_CONT (0.25) WITHIN GROUP (ORDER BY annual_rt) percentile_25,
       PERCENTILE_CONT (0.50) WITHIN GROUP (ORDER BY annual_rt) percentile_50,
       PERCENTILE_CONT (0.75) WITHIN GROUP (ORDER BY annual_rt) percentile_75
  FROM a1, ps_job j
 WHERE j.paygroup = a1.subscriber_id
   AND j.al_empl_status IN ('A', 'L')
   AND j.effdt =
          (SELECT MAX (effdt)
             FROM ps_job j21
            WHERE j.emplid = j21.emplid
              AND j.file_nbr = j21.file_nbr
              AND j.paygroup = j21.paygroup
              AND j21.effdt <= TO_DATE ('12/31/2010', 'MM/DD/YYYY'))
   AND j.effseq =
          (SELECT MAX (effseq)
             FROM ps_job j22
            WHERE j.emplid = j22.emplid
              AND j.file_nbr = j22.file_nbr
              AND j.effdt = j22.effdt
              AND j.paygroup = j22.paygroup)

Open in new window

0
Comment
Question by:pinkuray
[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
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
Mark Geerlings earned 250 total points
ID: 34896459
It is likely not because the query returns 300,000 records that it is slow.  (Although, obviously returning that many records will take some time!)  The bigger problem is likely: how many records does Oracle have to read through to find these 300,000?

How many records does the table ps_job contain?  Does it have a composite index that contains the four columns you reference in "where" clauses in the sub-queries?  Or, even better, does it have a composite five-column index that also includes: effseq?

How much of the total time for this query is spent processing the "with" statement first?  (If you don't know, run that query separately.)  Depending on the size of that table and depending on whether the sic_cd column in indexed or not, that query may run much faster like this instead:

SELECT subscriber_id
          FROM ps_sms_subscrb_tbl a
         WHERE 'A' = a.eff_status
           AND a.sic_cd between '70' and '8999'
           AND a.effdt =
                  (SELECT MAX (a1.effdt)
                     FROM ps_sms_subscrb_tbl a1
                    WHERE a1.subscriber_id = a.subscriber_id
                      AND a1.effdt <= TO_DATE ('12/31/2010', 'MM/DD/YYYY')))


One other point  (that may or may not make a big performance difference): I wonder why you put the known values in the "where" clauses on the left sometimes, like this example in the "with" statement:
WHERE 'A' = a.eff_status
or this example from the sub-queries:
WHERE j.emplid = j21.emplid
              AND j.file_nbr = j21.file_nbr
              etc.

I would write those with the "known" values on the right like this:
WHERE a.eff_status = 'A'
and
WHERE j21.emplid = j.emplid
              AND j21.file_nbr = j.file_nbr
              etc.

I understand that Oracle's query optimizer expects to find the "known" values in "where" clauses on the right.  If you put the known value on the left, that forces another CPU cycle or two for the optimizer.
0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
ID: 34896578
Another thing that might be slowing this down is the "select MAX()" in the three places you use them.

What are the stats (explain plan) for those?

Can you also post an explain plan for the entire query?

If you are unfamiliar wit how to do this:
explain plan for
select --- whatever select you wish to run

then to display it:
select * from table(dbms_xplan.display);
0
 
LVL 4

Author Comment

by:pinkuray
ID: 34896677
can you please modify the code as you said?
0
Independent Software Vendors: 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!

 
LVL 4

Author Comment

by:pinkuray
ID: 34896711
I will also post the explain plan  soon.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 34896750
>>I will also post the explain plan  soon.

Break the individual queries down to their core pieces to see where the main delay is.
0
 
LVL 4

Author Closing Comment

by:pinkuray
ID: 34923772
thanks for all your help
0

Featured Post

Technology Partners: 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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
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 recover a database from a user managed backup

691 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