简体   繁体   English

如何优化具有多个OR条件的pl sql if语句

[英]how to optimize pl sql if statement with multiple OR conditions

I'm new to pl sql can you please let me know how i can optimize the below if statement? 我是pl sql的新手,请告诉我如何优化以下if语句?

IF (inSeries=‘90’) OR (inSeries=‘91’) OR (inSeries=‘92’) OR (inSeries=‘93’) OR (inSeries=‘94’) THEN

like in sql we can use 像在SQL中,我们可以使用

WHERE inSeries IN (‘90’,’91’,’92’,’93’,’94’)

In PLSQL also 'IN' condition works as IF condition 在PLSQL中,“ IN”条件也可用作IF条件

declare
inSeries varchar2(2) := '90';
begin
if inseries in ('90','91','92','93','94')
then
dbms_output.put_line(inseries ||':this is within series');
else
dbms_output.put_line(inseries ||':this is out of series');
end if;
end;

-- output
90:this is within series
80:this is out of series

but there is another way depending on the business logic, as i can see from your question that its in series increment, you can directly use greater than and less than combination... 但是还有另一种方式取决于业务逻辑,正如我从您的问题中看到的那样,它以系列递增,您可以直接使用大于和小于组合...

Optimizer will most probably rewrite query so your IN will become OR anyway. 优化程序很可能会重写查询,因此您的IN仍将变为OR Compare line 3 in the query and the very last line: 比较查询中的第3行和最后一行:

SQL> select job
  2  from emp
  3  where deptno in (10, 20, 30, 40);

JOB
---------
CLERK
SALESMAN
<snip>

14 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 3956160932

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |    14 |   154 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| EMP  |    14 |   154 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("DEPTNO"=10 OR "DEPTNO"=20 OR "DEPTNO"=30 OR "DEPTNO"=40)

SQL>

您可以将SQL查询本身与“ EXISTS”关键字一起使用。

IF EXISTS (SELECT * FROM <table_name> WHERE inSeries IN (‘90’,’91’,’92’,’93’,’94’))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM