I need to write a query which basically needs to (left) join rows from two tables. My initial query looks like this:
select *
from Table1 T1
left join Table2 T2 on T1.f1 = T2.f1
and (decode(T2.f2,
'1',
'value1',
'2',
'value2',
'3',
'value3') = T1.f2 or T2.f2 = '4')
What I am tring to do is in Table1
and Table2
there is two fields (T1.f1
,T2.f1
) which can be used as join condition directly, but another two fields which need some transformation before being used in join condition since the values in these two columns do not have the same value set.
And now this query runs really unacceptably slow. I tried to removed the whole
and (decode(T2.f2,
'1',
'value1',
'2',
'value2',
'3',
'value3') = T1.f2 or T2.f2 = '4')
condition and it runs OK. Then I tried to remove the T2.f2 = '4'
part, i.e., the conditions looks like this:
left join Table2 T2 on T1.f1 = T2.f1
and (decode(T2.f2,
'1',
'value1',
'2',
'value2',
'3',
'value3') = T1.f2 )
and it also runs OK. So how should I modify the query (of course having the same result as original) to make it run fast? What is holding the execution?