如何使用正则表达式将数组中的项目与数据库列中的项目进行比较?

dub*_*ber 3 oracle plsql pattern-matching string-matching bigdata

我正在尝试采用这样的数组中的元素列表:

['GRADE', 'GRATE', 'GRAPE', /*About 1000 other entries here ...*/ ]
Run Code Online (Sandbox Code Playgroud)

并将它们与出现在Oracle数据库中的列中匹配的项匹配,例如:

1|'ANTERIOR'
2|'ANTEROGRADE'
3|'INGRATE'
4|'RETROGRADE'
5|'REIGN'
...|...
/*About 1,000,000 other entries here*/
Run Code Online (Sandbox Code Playgroud)

对于该G个单词数组中的每个条目,我想遍历Oracle数据库的word列,并尝试查找数组中每个条目的右侧匹配项。在此示例中,数据库中的条目2、3和4将全部匹配。

在任何其他编程语言中,它看起来都是这样的:

for entry in array:
  for each in column:
    if entry.right_match(each):
      print entry
Run Code Online (Sandbox Code Playgroud)

如何在PL / SQL中执行此操作?

kro*_*lko 5

在PL / SQL中,可以通过以下方式完成:

declare
   SUBTYPE my_varchar2_t IS varchar2( 100 );
   TYPE Roster IS TABLE OF my_varchar2_t;  
   names Roster := Roster( 'GRADE', 'GRATE', 'GRAPE');
begin
  FOR c IN ( SELECT id, name FROM my_table )
  LOOP
      FOR i IN names.FIRST .. names.LAST LOOP 
         IF regexp_like( c.name,   names( i )  ) THEN
              DBMS_OUTPUT.PUT_LINE( c.id || '  ' || c.name );
         END IF;
      END LOOP;
  END LOOP;
end;
/
Run Code Online (Sandbox Code Playgroud)

但这是逐行处理的,对于大表它会非常慢。

我认为以如下所示的方式执行可能会更好:

create table test123 as
select 1 id ,'ANTERIOR' name from dual union all
select 2,'ANTEROGRADE' from dual union all
select 3,'INGRATE' from dual union all
select 4,'RETROGRADE' from dual union all
select 5,'REIGN' from dual ;

create type my_table_typ is table of varchar2( 100 );
/

select *
from table( my_table_typ( 'GRADE', 'GRATE', 'GRAPE' )) x
join test123 y on regexp_like( y.name, x.column_value ) 
;

COLUMN_VALUE  ID         NAME      
------------- ---------- -----------
GRADE                  2 ANTEROGRADE 
GRATE                  3 INGRATE     
GRADE                  4 RETROGRADE
Run Code Online (Sandbox Code Playgroud)