If statement 在PL/SQL中,case语句或if-elseif等更有效率的是什么

If statement 在PL/SQL中,case语句或if-elseif等更有效率的是什么,if-statement,plsql,case,conditional-statements,If Statement,Plsql,Case,Conditional Statements,例如: CASE WHEN selector = 'value1' THEN S1; WHEN selector = 'value2' THEN S2; WHEN selector = 'value3' THEN S3; ... ELSE Sn; -- default case END CASE; 或其他: IF(boolean_expression 1)THEN S1; -- Executes when the boolean express

例如:

CASE
    WHEN selector = 'value1' THEN S1;
    WHEN selector = 'value2' THEN S2;
    WHEN selector = 'value3' THEN S3;
    ...
    ELSE Sn;  -- default case
END CASE;
或其他:

IF(boolean_expression 1)THEN 
   S1; -- Executes when the boolean expression 1 is true 
ELSIF( boolean_expression 2) THEN
   S2;  -- Executes when the boolean expression 2 is true 
ELSIF( boolean_expression 3) THEN
   S3; -- Executes when the boolean expression 3 is true 
ELSE 
   S4; -- executes when the none of the above condition is true 
END IF;

我猜您低估了查询优化器。在我看来,最好集中精力编写正确且可读的sql查询,而不是对有可疑好处的事情进行微优化


在这两种情况下,Oracle可能会在执行时执行相同的操作。

尽管文档中说“CASE语句可读性更高,效率更高。因此,如果可能,请将冗长的IF-THEN-ELSIF语句重写为CASE语句。”,我没有发现三种类型的条件句之间有显著差异

另一方面,布尔表达式中使用的数据类型不同。例如PLS_INTEGER比INTEGER更有效

请参见下面的示例(在我的服务器上,它给出的执行时间为103102104毫秒):

打开服务器输出
声明
tim整数;

t整数:=1;--我建议您编写一个小基准,运行多次,然后将结果作为您自己问题的答案发布。使用11gR1提供的配置文件配置基准!实际上,你不应该为这种微观优化而烦恼。我猜编译器将为您的两个示例生成相同的本机/解释代码。相反,请注意Oracle数据库文档一章中提到的实际性能问题。
set serveroutput on
declare
  tim integer;
  t integer := 1; -- <<< -- try pls_integer instead integer
  procedure print(text in varchar2) is begin DBMS_OUTPUT.PUT_LINE(text); end;
begin
 tim := dbms_utility.get_time;
 for i in 1..10000
 loop
   for x in 1..10
   loop
     t := x*10;
     case t
       when 1     then  t := 1;
       when 2     then  t := 1;
       when 3     then  t := 1;
       when 4     then  t := 1;
       when 5     then  t := 1;
       when 6     then  t := 1;
       when 7     then  t := 1;
       when 8     then  t := 1;
       when 9     then  t := 1;
       when 10    then  t := 1;
       when 11    then  t := 1;
       when 12    then  t := 1;
       when 13    then  t := 1;
       when 14    then  t := 1;
       when 15    then  t := 1;
       when 16    then  t := 1;
       when 17    then  t := 1;
       when 18    then  t := 1;
       when 19    then  t := 1;
       when 20    then  t := 1;
       when 21    then  t := 1;
       when 22    then  t := 1;
       when 23    then  t := 1;
       when 24    then  t := 1;
       when 25    then  t := 1;
       when 26    then  t := 1;
       when 27    then  t := 1;
       when 28    then  t := 1;
       when 29    then  t := 1;
       when 30    then  t := 1;
       when 31    then  t := 1;
       when 32    then  t := 1;
       when 33    then  t := 1;
       when 34    then  t := 1;
       when 35    then  t := 1;
       when 36    then  t := 1;
       when 37    then  t := 1;
       when 38    then  t := 1;
       when 39    then  t := 1;
       when 40    then  t := 1;
       when 41    then  t := 1;
       when 42    then  t := 1;
       when 43    then  t := 1;
       when 44    then  t := 1;
       when 45    then  t := 1;
       when 46    then  t := 1;
       when 47    then  t := 1;
       when 48    then  t := 1;
       when 49    then  t := 1;
       when 50    then  t := 1;
       when 51    then  t := 1;
       when 52    then  t := 1;
       when 53    then  t := 1;
       when 54    then  t := 1;
       when 55    then  t := 1;
       when 56    then  t := 1;
       when 57    then  t := 1;
       when 58    then  t := 1;
       when 59    then  t := 1;
       when 60    then  t := 1;
       when 61    then  t := 1;
       when 62    then  t := 1;
       when 63    then  t := 1;
       when 64    then  t := 1;
       when 65    then  t := 1;
       when 66    then  t := 1;
       when 67    then  t := 1;
       when 68    then  t := 1;
       when 69    then  t := 1;
       when 70    then  t := 1;
       when 71    then  t := 1;
       when 72    then  t := 1;
       when 73    then  t := 1;
       when 74    then  t := 1;
       when 75    then  t := 1;
       when 76    then  t := 1;
       when 77    then  t := 1;
       when 78    then  t := 1;
       when 79    then  t := 1;
       when 80    then  t := 1;
       when 81    then  t := 1;
       when 82    then  t := 1;
       when 83    then  t := 1;
       when 84    then  t := 1;
       when 85    then  t := 1;
       when 86    then  t := 1;
       when 87    then  t := 1;
       when 88    then  t := 1;
       when 89    then  t := 1;
       when 90    then  t := 1;
       when 91    then  t := 1;
       when 92    then  t := 1;
       when 93    then  t := 1;
       when 94    then  t := 1;
       when 95    then  t := 1;
       when 96    then  t := 1;
       when 97    then  t := 1;
       when 98    then  t := 1;
       when 99    then  t := 1;
       when 100   then  t := 1;
       when 101   then  t := 1;
       else t := 0;
     end case;  
   end loop;
 end loop;
 print(dbms_utility.get_time-tim);
 tim := dbms_utility.get_time;
 for i in 1..10000
 loop
   for x in 1..10
   loop
     t := x*10;
     case 
       when t = 1     then  t := 1;
       when t = 2     then  t := 1;
       when t = 3     then  t := 1;
       when t = 4     then  t := 1;
       when t = 5     then  t := 1;
       when t = 6     then  t := 1;
       when t = 7     then  t := 1;
       when t = 8     then  t := 1;
       when t = 9     then  t := 1;
       when t = 10    then  t := 1;
       when t = 11    then  t := 1;
       when t = 12    then  t := 1;
       when t = 13    then  t := 1;
       when t = 14    then  t := 1;
       when t = 15    then  t := 1;
       when t = 16    then  t := 1;
       when t = 17    then  t := 1;
       when t = 18    then  t := 1;
       when t = 19    then  t := 1;
       when t = 20    then  t := 1;
       when t = 21    then  t := 1;
       when t = 22    then  t := 1;
       when t = 23    then  t := 1;
       when t = 24    then  t := 1;
       when t = 25    then  t := 1;
       when t = 26    then  t := 1;
       when t = 27    then  t := 1;
       when t = 28    then  t := 1;
       when t = 29    then  t := 1;
       when t = 30    then  t := 1;
       when t = 31    then  t := 1;
       when t = 32    then  t := 1;
       when t = 33    then  t := 1;
       when t = 34    then  t := 1;
       when t = 35    then  t := 1;
       when t = 36    then  t := 1;
       when t = 37    then  t := 1;
       when t = 38    then  t := 1;
       when t = 39    then  t := 1;
       when t = 40    then  t := 1;
       when t = 41    then  t := 1;
       when t = 42    then  t := 1;
       when t = 43    then  t := 1;
       when t = 44    then  t := 1;
       when t = 45    then  t := 1;
       when t = 46    then  t := 1;
       when t = 47    then  t := 1;
       when t = 48    then  t := 1;
       when t = 49    then  t := 1;
       when t = 50    then  t := 1;
       when t = 51    then  t := 1;
       when t = 52    then  t := 1;
       when t = 53    then  t := 1;
       when t = 54    then  t := 1;
       when t = 55    then  t := 1;
       when t = 56    then  t := 1;
       when t = 57    then  t := 1;
       when t = 58    then  t := 1;
       when t = 59    then  t := 1;
       when t = 60    then  t := 1;
       when t = 61    then  t := 1;
       when t = 62    then  t := 1;
       when t = 63    then  t := 1;
       when t = 64    then  t := 1;
       when t = 65    then  t := 1;
       when t = 66    then  t := 1;
       when t = 67    then  t := 1;
       when t = 68    then  t := 1;
       when t = 69    then  t := 1;
       when t = 70    then  t := 1;
       when t = 71    then  t := 1;
       when t = 72    then  t := 1;
       when t = 73    then  t := 1;
       when t = 74    then  t := 1;
       when t = 75    then  t := 1;
       when t = 76    then  t := 1;
       when t = 77    then  t := 1;
       when t = 78    then  t := 1;
       when t = 79    then  t := 1;
       when t = 80    then  t := 1;
       when t = 81    then  t := 1;
       when t = 82    then  t := 1;
       when t = 83    then  t := 1;
       when t = 84    then  t := 1;
       when t = 85    then  t := 1;
       when t = 86    then  t := 1;
       when t = 87    then  t := 1;
       when t = 88    then  t := 1;
       when t = 89    then  t := 1;
       when t = 90    then  t := 1;
       when t = 91    then  t := 1;
       when t = 92    then  t := 1;
       when t = 93    then  t := 1;
       when t = 94    then  t := 1;
       when t = 95    then  t := 1;
       when t = 96    then  t := 1;
       when t = 97    then  t := 1;
       when t = 98    then  t := 1;
       when t = 99    then  t := 1;
       when t = 100   then  t := 1;
       when t = 101   then  t := 1;
       else t := 0;
     end case;  
   end loop;
 end loop;
 print(dbms_utility.get_time-tim);
 tim := dbms_utility.get_time;
 for i in 1..10000
 loop
   for x in 1..10
   loop
     t := x*10;
     if     t = 1     then  t := 1;
     elsif  t = 2     then  t := 1;
     elsif  t = 3     then  t := 1;
     elsif  t = 4     then  t := 1;
     elsif  t = 5     then  t := 1;
     elsif  t = 6     then  t := 1;
     elsif  t = 7     then  t := 1;
     elsif  t = 8     then  t := 1;
     elsif  t = 9     then  t := 1;
     elsif  t = 10    then  t := 1;
     elsif  t = 11    then  t := 1;
     elsif  t = 12    then  t := 1;
     elsif  t = 13    then  t := 1;
     elsif  t = 14    then  t := 1;
     elsif  t = 15    then  t := 1;
     elsif  t = 16    then  t := 1;
     elsif  t = 17    then  t := 1;
     elsif  t = 18    then  t := 1;
     elsif  t = 19    then  t := 1;
     elsif  t = 20    then  t := 1;
     elsif  t = 21    then  t := 1;
     elsif  t = 22    then  t := 1;
     elsif  t = 23    then  t := 1;
     elsif  t = 24    then  t := 1;
     elsif  t = 25    then  t := 1;
     elsif  t = 26    then  t := 1;
     elsif  t = 27    then  t := 1;
     elsif  t = 28    then  t := 1;
     elsif  t = 29    then  t := 1;
     elsif  t = 30    then  t := 1;
     elsif  t = 31    then  t := 1;
     elsif  t = 32    then  t := 1;
     elsif  t = 33    then  t := 1;
     elsif  t = 34    then  t := 1;
     elsif  t = 35    then  t := 1;
     elsif  t = 36    then  t := 1;
     elsif  t = 37    then  t := 1;
     elsif  t = 38    then  t := 1;
     elsif  t = 39    then  t := 1;
     elsif  t = 40    then  t := 1;
     elsif  t = 41    then  t := 1;
     elsif  t = 42    then  t := 1;
     elsif  t = 43    then  t := 1;
     elsif  t = 44    then  t := 1;
     elsif  t = 45    then  t := 1;
     elsif  t = 46    then  t := 1;
     elsif  t = 47    then  t := 1;
     elsif  t = 48    then  t := 1;
     elsif  t = 49    then  t := 1;
     elsif  t = 50    then  t := 1;
     elsif  t = 51    then  t := 1;
     elsif  t = 52    then  t := 1;
     elsif  t = 53    then  t := 1;
     elsif  t = 54    then  t := 1;
     elsif  t = 55    then  t := 1;
     elsif  t = 56    then  t := 1;
     elsif  t = 57    then  t := 1;
     elsif  t = 58    then  t := 1;
     elsif  t = 59    then  t := 1;
     elsif  t = 60    then  t := 1;
     elsif  t = 61    then  t := 1;
     elsif  t = 62    then  t := 1;
     elsif  t = 63    then  t := 1;
     elsif  t = 64    then  t := 1;
     elsif  t = 65    then  t := 1;
     elsif  t = 66    then  t := 1;
     elsif  t = 67    then  t := 1;
     elsif  t = 68    then  t := 1;
     elsif  t = 69    then  t := 1;
     elsif  t = 70    then  t := 1;
     elsif  t = 71    then  t := 1;
     elsif  t = 72    then  t := 1;
     elsif  t = 73    then  t := 1;
     elsif  t = 74    then  t := 1;
     elsif  t = 75    then  t := 1;
     elsif  t = 76    then  t := 1;
     elsif  t = 77    then  t := 1;
     elsif  t = 78    then  t := 1;
     elsif  t = 79    then  t := 1;
     elsif  t = 80    then  t := 1;
     elsif  t = 81    then  t := 1;
     elsif  t = 82    then  t := 1;
     elsif  t = 83    then  t := 1;
     elsif  t = 84    then  t := 1;
     elsif  t = 85    then  t := 1;
     elsif  t = 86    then  t := 1;
     elsif  t = 87    then  t := 1;
     elsif  t = 88    then  t := 1;
     elsif  t = 89    then  t := 1;
     elsif  t = 90    then  t := 1;
     elsif  t = 91    then  t := 1;
     elsif  t = 92    then  t := 1;
     elsif  t = 93    then  t := 1;
     elsif  t = 94    then  t := 1;
     elsif  t = 95    then  t := 1;
     elsif  t = 96    then  t := 1;
     elsif  t = 97    then  t := 1;
     elsif  t = 98    then  t := 1;
     elsif  t = 99    then  t := 1;
     elsif  t = 100   then  t := 1;
     elsif  t = 101   then  t := 1;
     else t := 0;
     end if;  
   end loop;
 end loop;
 print(dbms_utility.get_time-tim);
end;
/