我有一种情况,我的数据如下所示:
第18章第10单元第16节
使用substr:
declare l_start number := DBMS_UTILITY.get_cpu_time; begin for i in ( with t as ( select 'Chapter ' || level || ' Unit ' || level || ' Sect ' || level d from dual connect by rownum < 100000 ) select substr(d, 1, instr(d, ' ', 1, 2) - 1) chapter , substr(d, instr(d, ' ', 1, 2), instr(d, ' ', 1, 4) - instr(d, ' ', 1, 2) ) unit , substr(d, instr(d, ' ', 1, 4), length(d) - instr(d, ' ', 1, 4) + 1 ) sect from t ) loop null; end loop; DBMS_OUTPUT.put_line((DBMS_UTILITY.get_cpu_time - l_start) || ' hsec'); end; 126 hsec
使用正则表达式:
declare l_start number := DBMS_UTILITY.get_cpu_time; begin for i in ( with t as ( select 'Chapter ' || level || ' Unit ' || level || ' Sect ' || level d from dual connect by rownum < 100000 ) select regexp_substr(d, 'Chapter [0-9]*') chapter , regexp_substr(d, 'Unit [0-9]*') unit , regexp_substr(d, 'Sect [0-9]*') sect from t ) loop null; end loop; DBMS_OUTPUT.put_line((DBMS_UTILITY.get_cpu_time - l_start) || ' hsec'); end; 190 hsec
因此,使用regexp的解决方案速度较慢,但可读性更高,如果您是我,我将使用regexp。