另一种方法是将数组传递给数据库,并在IN子句中使用TABLE()函数.这可能比临时表执行得更好.它肯定比运行多个查询更有效.但是如果你有大量的会话来做这件事,你将需要监视PGA内存使用情况.另外,我不确定将它连接到Hibernate是多么容易.
注意:TABLE()函数在SQL引擎中运行,因此它们需要我们声明SQL类型.
create or replace type tags_nt as table of varchar2(10);
/
以下示例使用几千个随机标记填充数组.然后它使用查询的IN子句中的数组.
declare
search_tags tags_nt;
n pls_integer;
begin
select name
bulk collect into search_tags
from ( select name
from temp_tags
order by dbms_random.value )
where rownum <= 2000;
select count(*)
into n
from big_table
where name in ( select * from table (search_tags) );
dbms_output.put_line('tags match '||n||' rows!');
end;
/