0

I'm using Oracle 11g. I have a query that takes 6 seconds to execute

 select *
   from tab1
  where exists (select 1
           from tab2
          where tab2.col1 = tab1.col1
            and tab2.col2 = pkg1.fn1('STRING'));

The highlighted function returns a constant value = 1. (The function has 1 query which when executed standalone, returns result in < 1 second.

If I hardcode 1 instead of calling the function, query returns same results in < 1 second.

I do not want to use RESULT_CACHE in function, etc. I want to rewrite the query so that it is faster.

Approach 1: No time difference The query within the function is brought out and used as a subquery

and tab2.col2 = pkg1.fn1('STRING')

changed to

and tab2.col2 = (query within the function is written here)

Approach 2: Time increased The query within the function is brought out and used as an inner join with the overall query within the exists clause.

Approach 3: No change

with factoring as (select pkg1.fn1('STRING') as return_val from dual)
 select *
   from tab1
  where exists (select 1
           from tab2, factoring
          where tab2.col1 = tab1.col1
            and tab2.col2 = factoring.return_val);

Approach 4: No change

with factoring as (select pkg1.fn1('STRING') as return_val from dual)
 select *
   from tab1, factoring
  where exists (select 1
           from tab2
          where tab2.col1 = tab1.col1
            and tab2.col2 = factoring.return_val);

I want to understand below
1. why approach 3 and 4 didn't work
2. how to optimize this query?

Nikhil
  • 163
  • 2
  • 14
  • What do the execution plans show? In your first query, is changing from a function call to a literal changing the plan (e.g. which index is hit)? Is the function determinisitic, and can/should it be? – Alex Poole Dec 06 '18 at 12:20
  • Execution plans have no difference when changed to literal. The function behaves like a deterministic function, but I don't want to change anything in the function. What can be done to the query? – Nikhil Dec 10 '18 at 03:51

0 Answers0