請不要使用:WHERE','|| to_char(:P5_USER_ID_LIST)||','like'%,'|| to_char(u.user_id)||',%',因爲您會強制完整表掃描,儘管用戶表可能沒有那麼多,所以影響會很小,但是在企業環境中的其他表中,這是一個問題。
編輯:我已經放在一起的腳本來演示正則表達式方法和通配符類似的方法之間的差異。正則表達式不僅更快,而且更強大。
-- Create table
create table CSV_TEST
(
NUM NUMBER not null,
STR VARCHAR2(20)
);
create sequence csv_test_seq;
begin
for j in 1..10 loop
for i in 1..500000 loop
insert into csv_test(num, str) values (csv_test_seq.nextval, to_char(csv_test_seq.nextval));
end loop;
commit;
end loop;
end;
/
-- Create/Recreate primary, unique and foreign key constraints
alter table CSV_TEST
add constraint CSV_TEST_PK primary key (NUM)
using index ;
alter table CSV_TEST
add constraint CSV_TEST_FK unique (STR)
using index;
select sysdate from dual;
select *
from csv_test t
where t.num in (Select Regexp_Substr('100001, 100002, 100003 , 100004, 100005','[^,]+', 1, Level) From Dual
Connect By Regexp_Substr('100001, 100002,100003, 100004, 100005', '[^,]+', 1, Level) Is Not Null);
select sysdate from dual;
select *
from csv_test t
where ('%,' || '100001,100002, 100003, 100004 ,100005' || ',%') like '%,' || num || ',%';
select sysdate from dual;
select *
from csv_test t
where t.num in (Select Regexp_Substr('100001, 100002, 100003 , 100004, 100005','[^,]+', 1, Level) From Dual
Connect By Regexp_Substr('100001, 100002,100003, 100004, 100005', '[^,]+', 1, Level) Is Not Null);
select sysdate from dual;
select *
from csv_test t
where ('%,' || '100001,100002, 100003, 100004 ,100005' || ',%') like '%,' || num || ',%';
select sysdate from dual;
drop table csv_test;
drop sequence csv_test_seq;
不要這樣做 - 首先,如果查詢頻繁地運行多個user_id,它將使共享池氾濫許多不可用的遊標,並且很快整個數據庫將有一個硬解析問題。如果你不是非常小心的話,還有一個重要的SQL注入風險。 –