1

My existing query looks like:

SELECT p.subcode as proc_subcode,
       r.subcode as rv_subcode,
       r.rlv,
       f.cfid,
       f.geozip,
       p.proc,
       p.sh_descr,
       f.begproc,
       f.endproc,
       p.descr,
       r.modi,
       f.per25
  FROM user.code p, user.relative r, user.frequency f
 WHERE f.ID = 'XOF'
   AND f.GEOZIP = '200'
   AND f.REL_DATE = '10-SEP-13'
   AND p.proc IN ('A0021', 'A0080', 'A0090', 'A0100', 'A0110', 'A0120',
        'A0130', 'A0140', 'A0160', 'A0170', 'A0180', 'A0190',
        'A0200', 'A0210', 'A0225')
   AND r.ID = f.ID
   AND f.REL_DATE = r.REL_DATE
   AND f.ID = p.ID
   AND f.REL_DATE = p.REL_DATE
   AND p.proc = r.proc
   AND p.proc between f.begproc AND f.endproc
   AND (('A0021' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0021' AND
       r.PROC = 'A0021' AND f.CFID = '00') OR
       ('A0080' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0080' AND
       r.PROC = 'A0080' AND f.CFID = '00') OR
       ('A0090' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0090' AND
       r.PROC = 'A0090' AND f.CFID = '00') OR
       ('A0100' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0100' AND
       r.PROC = 'A0100' AND f.CFID = '00') OR
       ('A0110' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0110' AND
       r.PROC = 'A0110' AND f.CFID = '00') OR
       ('A0120' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0120' AND
       r.PROC = 'A0120' AND f.CFID = '00') OR
       ('A0130' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0130' AND
       r.PROC = 'A0130' AND f.CFID = '00') OR
       ('A0140' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0140' AND
       r.PROC = 'A0140' AND f.CFID = '00') OR
       ('A0160' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0160' AND
       r.PROC = 'A0160' AND f.CFID = '00') OR
       ('A0170' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0170' AND
       r.PROC = 'A0170' AND f.CFID = '00') OR
       ('A0180' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0180' AND
       r.PROC = 'A0180' AND f.CFID = '00') OR
       ('A0190' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0190' AND
       r.PROC = 'A0190' AND f.CFID = '00') OR
       ('A0200' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0200' AND
       r.PROC = 'A0200' AND f.CFID = '00') OR
       ('A0210' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0210' AND
       r.PROC = 'A0210' AND f.CFID = '00') OR
       ('A0225' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0225' AND
       r.PROC = 'A0225' AND f.CFID = '00'))
 ORDER BY p.proc, r.modi, f.REL_DATE asc

The above query works well, however, I'm planning to add 100s of p.proc in range, for ex. it would be like A0021-A1000. So, I want to improve the sql query performance.

Can anybody help?

Edit 1:

I suppose possible solution would be to remove IN clause for p.proc and make use of between. I'm not sure of the last AND condition though.

Edit 2:

Explanation of the query

I'm basically getting all the medical cost for a specific region using geozip and passing the code, for ex. A0021 means surgery code & 200 means New York & 10-september-2013 is the release date and getting results for the product called XOF.

4
  • 2
    I removed the mysql tag because the question is explicitly about Oracle. Commented Mar 13, 2014 at 12:20
  • Could you show us data and explain meaning of your query? Commented Mar 13, 2014 at 13:09
  • 1
    "toms rule #1: never compare strings to dates and dates to strings. Always compare strings to string and dates to dates!" Commented Mar 13, 2014 at 13:11
  • @MikkaRin - just edited the question to explain what I'm doing. Commented Mar 13, 2014 at 13:15

1 Answer 1

1

First of all

 WHERE f.ID = 'XOF'
   AND f.GEOZIP = '200'
   AND f.REL_DATE = '10-SEP-13'
   AND p.proc IN ('A0021', 'A0080', 'A0090', 'A0100', 'A0110', 'A0120',
        'A0130', 'A0140', 'A0160', 'A0170', 'A0180', 'A0190',
        'A0200', 'A0210', 'A0225')
   AND r.ID = f.ID
   AND f.REL_DATE = r.REL_DATE
   AND f.ID = p.ID
   AND f.REL_DATE = p.REL_DATE
   AND p.proc = r.proc
   AND p.proc between f.begproc AND f.endproc
   AND (('A0021' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0021' AND
       r.PROC = 'A0021' AND f.CFID = '00') OR
       ('A0080' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0080' AND
       r.PROC = 'A0080' AND f.CFID = '00') OR
       ('A0090' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0090' AND
       r.PROC = 'A0090' AND f.CFID = '00') OR....

in your query p.proc=r.proc. So you no need to write it each time in these statements:

 ('A0090' BETWEEN f.BEGPROC AND f.ENDPROC AND p.PROC = 'A0090' AND
   r.PROC = 'A0090' AND f.CFID = '00') OR

in all these statements also f.CFID='00'. You no need to write it in each statement. Currently I think that all what you need is :

WHERE f.ID = 'XOF'
       AND f.GEOZIP = '200'
       AND f.REL_DATE = '10-SEP-13'
       AND p.proc IN ('A0021', 'A0080', 'A0090', 'A0100', 'A0110', 'A0120',
            'A0130', 'A0140', 'A0160', 'A0170', 'A0180', 'A0190',
            'A0200', 'A0210', 'A0225')
       AND r.ID = f.ID
       AND f.REL_DATE = r.REL_DATE
       AND f.ID = p.ID
       AND f.REL_DATE = p.REL_DATE
       AND p.proc = r.proc
       AND p.proc between f.begproc AND f.endproc
       AND f.CFID='00'

these 4 statements

           AND p.proc IN ('A0021', 'A0080', 'A0090', 'A0100', 'A0110', 'A0120',
                'A0130', 'A0140', 'A0160', 'A0170', 'A0180', 'A0190',
                'A0200', 'A0210', 'A0225')
           AND p.proc = r.proc
           AND p.proc between f.begproc AND f.endproc
           AND f.CFID='00'

make the same that all these BETWEEEN statements.

please check this query on your data

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks MikkaRin. Is there a way to avoid p.proc IN clause. My user may input 1000 code in range, for ex. A0021-A1000.
Also, f.CFID='00' is not always constant, depending on the query, it may become f.CFID = '01' for different code
I think there is no way to avoid IN clause, since you store proc in string. For CFID you may do where clause like this: AND ((p.proc IN (...) AND f.CFID='00') OR (p.proc IN (...) AND f.CFID='01')) AND ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.