Home > Database > Mysql Tutorial > body text

Oracle PL/SQL浅析IF和ELSIF的不平等

WBOY
Release: 2016-06-07 15:03:53
Original
1001 people have browsed it

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入 之前写过一篇博客,叫:Oracle PL/SQL 从if 到 then的"艺术鉴赏",地址:点击打开链接 现在范围扩展至每个平行的IF条件,是否应该注意点啥 在条件是平行的时候,是否IF一定是可以无序的呢? 先看个小

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入

  之前写过一篇博客,叫:Oracle PL/SQL 从if 到 then的"艺术鉴赏",地址:点击打开链接

  现在范围扩展至每个平行的IF条件,是否应该注意点啥

  在条件是平行的时候,是否IF一定是可以无序的呢?

  先看个小实验:

  [sql]

  hr@ORCL> ed

  Wrote file afiedt.buf

  1  create or replace procedure p_test_if(p_num pls_integer)

  2  as

  3  v_num PLS_INTEGER:=p_num;

  4  begin

  5    for i in 11000000

  6      loop

  7        if p_num=1

  8          then

  9            v_num:=p_num+1;

  10        elsif p_num=2

  11          then

  12            v_num:=p_num+1;

  13        elsif p_num=3

  14          then

  15            v_num:=p_num+1;

  16        else

  17            v_num:=p_num+1;

  18        end if;

  19      end loop;

  20* end p_test_if;

  hr@ORCL> /

  Procedure created.

  hr@ORCL> set timing on

  hr@ORCL> exec p_test_if(1);

  PL/SQL procedure successfully completed.

  Elapsed: 00:00:00.13

  hr@ORCL> exec p_test_if(2);

  PL/SQL procedure successfully completed.

  Elapsed: 00:00:00.18

  hr@ORCL> exec p_test_if(3);

  PL/SQL procedure successfully completed.

  Elapsed: 00:00:00.25

  三次调用的时间都不相同,调用值落在IF逻辑越靠前,查询越快速原理其实很简单,条件靠前可以减少判断的次数,因为满足条件后不会再尝试后面的表达式所以,即便IF逻辑是平行的,也会根据调用PL/SQL过程或函数的入参的值的分布情况来决定IF的顺序入参越是频繁调用的值,对应的IF逻辑就越需要靠前因为Oracle对IF的调用是靠前的调用执行的更快速

  这个告诉我们,在PL/SQL开发中,对经常调用的函数或过程的输入值的分布范围最好有一定的了解对经常要输入的参数条件,代码中要放在IF的前面,会有一定的性能提高。

Oracle PL/SQL浅析IF和ELSIF的不平等

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!