Oracle PL/SQL: 创建短路查询
Oracle PL/SQL: Create a short circuit query
我有点老派,所以我称之为短路。
但我在 Oracle 中有 3 个 table。我还有一个 "ProcessFlag." 如果 table 中的任何数据符合我的条件,我想设置 procesdFlag = 1.
我显然可以用 if 和 else 语句来做到这一点。
select count(*) into myCounter from from Table1 where processflag = 1;
if myCounter = 0 then
select count(*) into myCounter from Table2 where processflag = 1;
if myCounter = 0 then
select count(*) into myCounter from Table3 where processflag = 1;
if myCounter = 0 then
processFlag = 0;
else
processFlag = 1;
end if
else
processFlag = 1;
end if
else
processFlag = 1;
end if
所以,假设我 if/eles 的所有内容都是正确的,我想您明白我想做什么。如果在任何 table 中有任何行的 processFlag = 1,那么我要处理。
这只是一个用于演示的小示例,但我的真实世界有大约 10 个 table,如果我不这样做,我真的不想嵌套那么深的 if/else 语句不需要。而且其中一些table有很多数据,所以如果在第一个table中发现了一些东西,就不需要检查后续的table(这就是为什么我称之为短路,因为一旦找到东西,就不需要处理剩余的代码。
如果在 Oracle 中有 cleaner/efficient 方法可以做到这一点?
你想要这样的东西吗?
select . . .
from . . .
where exists (select 1 from table1 where processflag = 1) or
exists (select 1 from table2 where processflag = 1) or
exists (select 1 from table3 where processflag = 1) ;
也就是说,您可以使查询以一堆 exists
条件为条件。如果您使用的是 PL/SQL.
,则可以使用类似于实际设置标志的方法
你可以使用rownum:
select count(*) into myCounter
from (
select * from Table1 where processflag = 1
) l where rownum = 1;
您可以使用case when
进行短路查询,如下所示::
Select case
when (select 1 from table1 where processflag = 1 and rownum =1) = 1 -- small table in terms of number of rows
then 1
when (select 1 from table2 where processflag = 1 and rownum =1) = 1 -- larger than table1 table in terms of number of rows and so on
then 1
..
Else 0 end into processFlag
From dual;
Case when
找到一个匹配项后将停止执行该语句。
您还可以在 case when
语句中给出 table 的顺序以获得更好的性能,首先使用小的 tables,最后使用大的 tables when
良好性能条款。
干杯!!
我有点老派,所以我称之为短路。
但我在 Oracle 中有 3 个 table。我还有一个 "ProcessFlag." 如果 table 中的任何数据符合我的条件,我想设置 procesdFlag = 1.
我显然可以用 if 和 else 语句来做到这一点。
select count(*) into myCounter from from Table1 where processflag = 1;
if myCounter = 0 then
select count(*) into myCounter from Table2 where processflag = 1;
if myCounter = 0 then
select count(*) into myCounter from Table3 where processflag = 1;
if myCounter = 0 then
processFlag = 0;
else
processFlag = 1;
end if
else
processFlag = 1;
end if
else
processFlag = 1;
end if
所以,假设我 if/eles 的所有内容都是正确的,我想您明白我想做什么。如果在任何 table 中有任何行的 processFlag = 1,那么我要处理。
这只是一个用于演示的小示例,但我的真实世界有大约 10 个 table,如果我不这样做,我真的不想嵌套那么深的 if/else 语句不需要。而且其中一些table有很多数据,所以如果在第一个table中发现了一些东西,就不需要检查后续的table(这就是为什么我称之为短路,因为一旦找到东西,就不需要处理剩余的代码。
如果在 Oracle 中有 cleaner/efficient 方法可以做到这一点?
你想要这样的东西吗?
select . . .
from . . .
where exists (select 1 from table1 where processflag = 1) or
exists (select 1 from table2 where processflag = 1) or
exists (select 1 from table3 where processflag = 1) ;
也就是说,您可以使查询以一堆 exists
条件为条件。如果您使用的是 PL/SQL.
你可以使用rownum:
select count(*) into myCounter
from (
select * from Table1 where processflag = 1
) l where rownum = 1;
您可以使用case when
进行短路查询,如下所示::
Select case
when (select 1 from table1 where processflag = 1 and rownum =1) = 1 -- small table in terms of number of rows
then 1
when (select 1 from table2 where processflag = 1 and rownum =1) = 1 -- larger than table1 table in terms of number of rows and so on
then 1
..
Else 0 end into processFlag
From dual;
Case when
找到一个匹配项后将停止执行该语句。
您还可以在 case when
语句中给出 table 的顺序以获得更好的性能,首先使用小的 tables,最后使用大的 tables when
良好性能条款。
干杯!!