Case clause execution procedure

徘徊边缘 提交于 2019-12-14 03:18:22

问题


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..??

  • Check Case condition
  • if true, process inside output
  • else process other output.

or does it take the below logic:

  • pre-process all sub-queries
  • Check Case condition
  • if true, display inside output
  • else display other output.

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


回答1:


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




回答2:


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>



回答3:


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.



来源:https://stackoverflow.com/questions/15384979/case-clause-execution-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!