如何优化具有多个OR条件的pl sql if语句
我是pl sql新手,您能告诉我如何优化下面的if语句吗如何优化具有多个OR条件的pl sql if语句,sql,oracle,plsql,Sql,Oracle,Plsql,我是pl sql新手,您能告诉我如何优化下面的if语句吗 IF (inSeries=‘90’) OR (inSeries=‘91’) OR (inSeries=‘92’) OR (inSeries=‘93’) OR (inSeries=‘94’) THEN 就像在sql中一样,我们可以使用 WHERE inSeries IN (‘90’,’91’,’92’,’93’,’94’) 优化器很可能会重写查询,使您的IN变为或无论如何。比较查询中的第3行和最后一行: SQL> select j
IF (inSeries=‘90’) OR (inSeries=‘91’) OR (inSeries=‘92’) OR (inSeries=‘93’) OR (inSeries=‘94’) THEN
就像在sql中一样,我们可以使用
WHERE inSeries IN (‘90’,’91’,’92’,’93’,’94’)
优化器很可能会重写查询,使您的IN变为或无论如何。比较查询中的第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>
在PLSQL中,“In”条件也像条件一样工作
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
但是,根据业务逻辑,还有另一种方法,正如我从您的问题中看到的,它是串联增量,您可以直接使用大于和小于组合 您可以将SQL查询本身与“EXISTS”关键字一起使用
IF EXISTS (SELECT * FROM <table_name> WHERE inSeries IN (‘90’,’91’,’92’,’93’,’94’))
您的WHERE-IN语法已经相当优化。使用它有什么错?我想优化我的if语句。对不起,这里的IN只是一个例子,IN操作符在PL/SQL中工作,您试过了吗?