Th3Nic3Guy
Th3Nic3Guy

Reputation: 1979

Case clause execution procedure

Hi i have a SQL which is extremely slow.

select case when (value=1) 
then (select <Some Math Logic> from table where table.id=table_2.id) 
else 'false' end 
from table_2 where <where clause>

what i want to know is how does the case clause work..??

the part select <Some Math Logic> from table is working on huge table set.

in Java we see that for an if statement

if(condition_1==true) 
{return output;} 
else 
{return false;}

if the if statement is false then the part inside it is never processed, then what i want to know is if it is the same in the oracle SQL also.

does it work on the following logic..??

or does it take the below logic:

please help

edit: Guys i dont want to tune the query shown above... this is just a dummy one.

what i need to know is how the case clause works step by step.

please share if any one has some input for that

Upvotes: 0

Views: 228

Answers (3)

user1805212
user1805212

Reputation:

It seems that the logic is exactly like that in java.

I used the following logic to test your scenario:

I created a dummy function as below which will just write something on the dbms_output and return only 10.

CREATE OR REPLACE
  FUNCTION DISP_MSG
    RETURN VARCHAR2
  AS
    ret_val VARCHAR2(20):='10';
  BEGIN
    dbms_output.enable;
    dbms_output.put_line('executed');
    RETURN ret_val;
  END DISP_MSG;

then i created an anonymous block to test the same:

DECLARE
  var VARCHAR2(100);
BEGIN
  SELECT CASE WHEN (1!=1) THEN DISP_MSG ELSE '1' END INTO var FROM dual;
  dbms_output.put_line('j = '||var);
END;

Output:

j = 1

but when i tried the below:

DECLARE
  var VARCHAR2(100);
BEGIN
  SELECT CASE WHEN (1=1) THEN DISP_MSG ELSE '1' END INTO var FROM dual;
  dbms_output.put_line('j = '||var);
END;

Output:

executed

j = 10

so its quite clear what is the execution logic

Upvotes: 0

Brandon
Brandon

Reputation: 10038

Good question. You need to see the execution plan to know for sure. The database engine is free to use any algorithm it sees fit so long as it gets you the results you asked for.

It could even outer join table to get the results in anticipation of value = 1. Or it could run the select from table and store the results into a temporary table that it can scan when it runs the main query.

Most likely, however, it is running the subquery for every row where value = 1. Hard to tell without seeing the plan.

It also depends on the details of . Are you taking aggregates? If so, a true join may be impossible and it may have to recalculate the answer for every row. If it's looking at values right on the table rows, then it may be able to optimize that away.

If you take the case statement out, does the overall query perform much faster? Want to make sure you are analyzing the correct sub-query.

Upvotes: 0

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Does this solve your problem?

SELECT
   COALESCE(<Some Math Logic>, 'false')
FROM table_2 T2
    LEFT JOIN table T
        ON T.Id = T2.Id
           and T2.value = 1
WHERE <where clause>

Upvotes: 2

Related Questions