Solved

Tune Oracle query

Posted on 2011-02-15
6
636 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
  • 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 76

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Author Comment

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to recover a database from a user managed backup
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

831 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