简体   繁体   English

在Oracle中替代REGEXP_LIKE

[英]Alternative to REGEXP_LIKE in oracle

Can you please tell me if there is any alternative function which I can use for REGEXP_LIKE ? 您能否告诉我REGEXP_LIKE是否可以使用其他功能?

I have the following situation: 我有以下情况:

Names:
ID NAME
1  Alex
2  Jim
3  Tom

Invoices:
ID Amount Name_IDs
1  100    1;2;
2  200    2;3;

Wanted output:
Invoice_ID Names     Amount:
1          Alex;Jim; 100
2          Jim;Tom;  200

I know that the database model is not quite technically right, but now I'm using a query for this with REGEXP_LIKE to add in a listagg() the names, but the performance is very very slow. 我知道数据库模型在技术上不是很正确,但是现在我正在使用REGEXP_LIKE对此查询,以在listagg()中添加名称,但是性能非常慢。

SELECT inv.id as invoice_id, 
       SELECT listagg(n.name,';') WITHIN GROUP (ORDER BY NULL) from names where REGEXP_LIKE(inv.names, '(^|\W)' || n.name || '(\W|$) 'as names
       inv.amount as amount
from invoices inv;

Can you please give me any idea how to improve this query ? 您能给我任何想法如何改进此查询吗?

Thank you! 谢谢!

If you have an index on your names table then this should be faster: 如果您的names表上有索引,那么它应该更快:

select i.id,
       i.amount,
       (select listagg(n.name, ';') within group(order by null)
          from (select trim(regexp_substr(str, '[^;]+', 1, level)) ASPLIT
                  from (select i.name_ids as str from dual)
                connect by regexp_substr(str, '[^;]+', 1, level) is not null) t
          join names n
            on n.id = t.asplit)
  from invoices i;

ie first split by Name_IDs and join with the names table. 即首先按Name_IDs分割,然后与namesName_IDs

I suggest that you change your data model to something like the following: 我建议您将数据模型更改为以下内容:

NAMES:
ID_NAMES NAME
10       Alex
20       Jim
30       Tom

INVOICES:
ID_INVOICE Amount
1          100
2          200

INVOICE_NAMES:
ID_INVOICE  ID_NAME
1           10
1           20
2           20
2           30

Best of luck. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM