Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Sukhvinder.

Asked: March 05, 2018 - 10:12 pm UTC

Last updated: March 08, 2018 - 2:59 am UTC

Version: 11.2.0.3

Viewed 1000+ times

You Asked

Hi Tom,

Thanks for being a great support for me over the years. I always look for your solutions whenever I am in need.

Could not find anything on this hence posting my first question.

In my query I am using few big tables with millions of rows in them.
My Query is something like.

Select A.A1, A.A2, B.B1, C.C3,........,NVL(B.B2,B.B3)
From A, B,C,D,E
Where A.A1=B.B5
.
.
.
.
And NVL(B.B2,B.B3) <='01/Jan/2018'

Now the issue I have is that for number of records both B2 and B3 columns are null,
So in this case I need to check for a column in another table F which can be joined
on table E with one to one relationship as F.F1=E.E3 where F1 is primary key.
When I change filter condition to NVL(B.B2,NVL(B3,F5)) <='01/Jan/2018'
it kills the performance of the query. Can you please suggest a way to make it better.

Cheers,

and Connor said...

A lot depends on the data distribution, but some things you could explore:

1) Splitting this into 2 so we only join to F when necessary.

Select A.A1, A.A2, B.B1, C.C3,........,NVL(B.B2,B.B3)
From A, B,C,D,E
Where A.A1=B.B5
...
And NVL(B.B2,B.B3) <='01/Jan/2018'
union all

Select A.A1, A.A2, B.B1, C.C3,........,NVL(B.B2,B.B3)
From A, B,C,D,E, F
Where A.A1=B.B5
and b.b2 is null
and b.b3 is null
And F5 <='01/Jan/2018'


Then you can optimize each half of the query in isolation, eg an index ON NVL(B.B2,B.B3) for the top half etc

2) scalar subquery

If you only need 1 (or just a few) elements from F, you could do:

Select A.A1, A.A2, B.B1, C.C3,........,NVL(B.B2,B.B3)
From A, B,C,D,E
Where A.A1=B.B5
...
And ( NVL(B.B2,B.B3) <='01/Jan/2018'
  or
   ( select ... from F where F5 <='01/Jan/2018' ) 
)


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks

Sukhvinder Bhullar, March 08, 2018 - 1:13 am UTC

Thanks a lot Connor for your quick response.
Will try it out.
Connor McDonald
March 08, 2018 - 2:59 am UTC

glad we could help

More to Explore

Performance

Get all the information about database performance in the Database Performance guide.