Go Premium for a chance to win a PS4. Enter to Win


Tune Oracle query

Posted on 2011-02-15
Medium Priority
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:

        SELECT subscriber_id
          FROM ps_sms_subscrb_tbl a
         WHERE 'A' = a.eff_status
           AND SUBSTR (a.sic_cd, 1, 2) IN
           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')))
       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

Question by:pinkuray
  • 3
  • 2
LVL 35

Accepted Solution

Mark Geerlings earned 1000 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

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

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.
LVL 78

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 1000 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);

Author Comment

ID: 34896677
can you please modify the code as you said?
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!


Author Comment

ID: 34896711
I will also post the explain plan  soon.
LVL 78

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.

Author Closing Comment

ID: 34923772
thanks for all your help

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

886 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