Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 643
  • Last Modified:

Tune Oracle query

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
pinkuray
Asked:
pinkuray
  • 3
  • 2
2 Solutions
 
Mark GeerlingsDatabase AdministratorCommented:
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
 
slightwv (䄆 Netminder) Commented:
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
 
pinkurayAuthor Commented:
can you please modify the code as you said?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
pinkurayAuthor Commented:
I will also post the explain plan  soon.
0
 
slightwv (䄆 Netminder) Commented:
>>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
 
pinkurayAuthor Commented:
thanks for all your help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now