Friday, March 30, 2012

help optimizing query

I have a query that is taking about 12 minutes to run. I'm sure there has to be a way to speed it up...i'm just not sure how. any suggestions on how to rewrite this would be much appreciated. i think it has something to do with the "not in" join.

Select
Distinct T.TSNUMB
from
TSR T
where
T.TSSTAT='C'
and T.TSCDAT >= '01-Oct-03'
and T.TSCDAT <= sysdate
and T.TSORGA in (Select distinct O.ORORGA
from OrgCode O
where
O.ORTCON like '123456%'
and T.TSNUMB not in (select distinct w.wcnumb
from
wobaskdtl w
where w.wcbask like '%-V'))You could try using NOT EXISTS instead of NOT IN:

Select
Distinct T.TSNUMB
from
TSR T
where
T.TSSTAT='C'
and T.TSCDAT >= '01-Oct-03'
and T.TSCDAT <= sysdate
and T.TSORGA in (Select distinct O.ORORGA
from OrgCode O
where
O.ORTCON like '123456%'
and not exists (select null
from
wobaskdtl w
where w.wcbask like '%-V' and w.wcnumb = t.tsnumb))

Also make sure your tables have been analyzed and the statistics are not stale (I'm inferring this is Oracle?)|||thank you...not sure i completely understand what is going on there (have to sit down and really look at it) but it works.sql

No comments:

Post a Comment