Performance of regexp_replace vs translate in Oracle?(Oracle 中 regexp_replace 与翻译的性能?)
问题描述
对于简单的事情,最好使用 translate
函数,前提是它占用的 CPU 更少,还是 regexp_replace
是要走的路?
For simple things is it better to use the translate
function on the premise that it is less CPU intensive or is regexp_replace
the way to go?
这个问题来自如何在 Oracle REGEXP_REPLACE 函数中将括号替换为连字符?
推荐答案
我认为您遇到了简单的优化问题.regexp 表达式的计算成本非常高,结果被缓存,希望将来能再次使用.如果你真的使用不同的字符串来转换,你会发现适度的翻译自然更快,因为它是它的专门功能.
I think you're running into simple optimization. The regexp expression is so expensive to compute that the result is cached in the hope that it will be used again in the future. If you actually use distinct strings to convert, you will see that the modest translate is naturally faster because it is its specialized function.
这是我的示例,在 11.1.0.7.0
上运行:
Here's my example, running on 11.1.0.7.0
:
SQL> DECLARE
2 TYPE t IS TABLE OF VARCHAR2(4000);
3 l t;
4 l_level NUMBER := 1000;
5 l_time TIMESTAMP;
6 l_char VARCHAR2(4000);
7 BEGIN
8 -- init
9 EXECUTE IMMEDIATE 'ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL=2';
10 SELECT dbms_random.STRING('p', 2000)
11 BULK COLLECT
12 INTO l FROM dual
13 CONNECT BY LEVEL <= l_level;
14 -- regex
15 l_time := systimestamp;
16 FOR i IN 1 .. l.count LOOP
17 l_char := regexp_replace(l(i), '[]()[]', '-', 1, 0);
18 END LOOP;
19 dbms_output.put_line('regex :' || (systimestamp - l_time));
20 -- tranlate
21 l_time := systimestamp;
22 FOR i IN 1 .. l.count LOOP
23 l_char := translate(l(i), '()[]', '----');
24 END LOOP;
25 dbms_output.put_line('translate :' || (systimestamp - l_time));
26 END;
27 /
regex :+000000000 00:00:00.979305000
translate :+000000000 00:00:00.238773000
PL/SQL procedure successfully completed
在 11.2.0.3.0
上:
regex :+000000000 00:00:00.617290000
translate :+000000000 00:00:00.138205000
结论:总的来说,我怀疑 translate
会赢.
Conclusion: In general I suspect translate
will win.
这篇关于Oracle 中 regexp_replace 与翻译的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Oracle 中 regexp_replace 与翻译的性能?


- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01