暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 从没有PL/SQL过程的字符串中解析 “#”

ASKTOM 2018-11-14
352

问题描述

亲爱的汤姆,
首先,感谢您的网站,提示...总是有用的...我最近遇到了一个sql问题,我设计了answears的开头,但我认为应该对其进行优化。
我有一个表格,其中包含来自社交网络平台的帖子。这些帖子由id标识,它包含文本 (所以,字符串),我想从这些字符串 # 或html url中提取 (解析)。我想在没有pl/SQL过程和没有正则表达式的情况下工作 (或者不太可能)。我的假设是,一个 # 以 “#” 开头,以 “” (空格) 结尾。因此,如果我知道我的字符串中给定 # 的开始位置和下一个空白处的位置,通过一个简单的soustraction,我将能够检索我的hahstag与SUBSTR命令。这是我的工作:

--- create intermediate view with targeted pattern position
create or replace view Start_Index_position as
with post as
(select id_post, text from "ma_table")
select id_post, instr(text,'#', 1, level) as position, text
from post
connect by level <= regexp_count(text, '#');
复制


它为我提供了一个视图,其中每次SQL在字符串中找到 “#” 的起始位置

然后我创建了一个工作视图,从中我将能够计算每个 # 的字符串长度,当然,提取与 “SUBSTR” #:

--- create working table view with full references and blank position for each pattern match and string_lenght for each one
create or replace view _#_index as
select id_post, position as hashtag_pos, INSTR(text,' ', position) as blank_position, INSTR(text,' ', position) - position as string_length, text
from start_index_position;
复制


最后用一个简单的查询如下:
select substr(text, hashtag_pos, string_length)from _#_index;
复制


...我可以找回我的 #。

这个过程几乎没有错误:
-我认为它没有优化。
-如果我的 # 或我要查找的任何模式在字符串的末尾,它将检索空值,因为字符串末尾没有空格。
-我认为我没有在他的最佳级别上使用 “通过” “级别” Oracle分层系统。真的没有吗?

再次感谢您的网站和您的回答

我为我的英语道歉,我是法国人

特里斯坦


补充信息:

测试样品:

create table test (a int, b varchar2(5000), c date);
复制

使用此数据示例:
insert into test values( 1, "#PROTESTOBR: Protestos em várias cidades brasileiras são destaque nos principais sites internacionais! O mundo começa a conhecer o Brasil de verdade!  http://www.guardian.co.uk/world/2013/jun/18/brazil-protests-erupt-huge-scale  http://www.washingtonpost.com/sports/crowds-of-protesters-demonstrate-in-at-least-8-brazilian-cities-venting-complaints-about-life/2013/06/17/4b4d27e2-d7b5-11e2-b418-9dfa095e125d_story.html  http://www.corriere.it/esteri/13_giugno_18/brasile-proteste-rio-trasporti_b593f39c-d7ab-11e2-a4df-7eff8733b462.shtml  http://www.lemonde.fr/ameriques/article/2013/06/17/manifestations-au-bresil-le-gouvernement-menace-du-carton-rouge_3431786_3222.html  http://www.lemonde.fr/ameriques/article/2013/06/17/manifestations-au-bresil-le-gouvernement-menace-du-carton-rouge_3431786_3222.html  http://www.bbc.co.uk/portuguese/noticias/130617_protestos_live.shtml  http://elpais.com/elpais/portada_america.html  #vemprarua #vempraruarj #vempraruabrasil #ogiganteacordou #geracaoinvencivel  @J.Oliveira  Curta a Página Geração Invencível: http://www.facebook.com/GeracaoInvencive", to_date('17-june-2013'));
insert into test values( 2, "#AoVivo no acampamento #BrasilComLula com o Deputado Paulo Pimenta  #BrasilComLula #MoroPersegueLula", to_date('10-may-2017');
复制


好的,让我们尝试使用两个数据集的这个小表来解决我的解决方案。会起作用的。但是,只要我有更多的数据 (我的实际表包含960741个帖子,这意味着我想复制上面解释的方法的960741字符串)。
那么,如何优化/如何在更大范围内部署它?这个想法是使用substr/instr比costy正则表达式更多地从字符串中提取所有 # (递归地,一个字符串可能包含多个 #)?

谢谢汤姆!

专家解答

像这样的东西?是的,它使用正则表达式,但我会在转向更复杂的选项之前对其进行基准测试

SQL> create table test (a int, b varchar2(4000), c date);

Table created.

SQL>
SQL> insert into test values(
  2  1,
  3  '#PROTESTOBR: Protestos em várias cidades brasileiras são destaque nos principais sites internacionais! O mundo começa a conhecer o Brasil de verdade!      http://www.guardian.co.uk/world/2013/jun/18/b
razil-protests-erupt-huge-scale  http://www.washingtonpost.com/sports/crowds-of-protesters-demonstrate-in-at-least-8-brazilian-cities-venting-complaints-about-life/2013/06/17/4b4d27e2-d7b5-11e2-b418-9dfa095
e125d_story.html    http://www.corriere.it/esteri/13_giugno_18/brasile-proteste-rio-trasporti_b593f39c-d7ab-11e2-a4df-7eff8733b462.shtml  http://www.lemonde.fr/ameriques/article/2013/06/17/manifestations-au
-bresil-le-gouvernement-menace-du-carton-rouge_3431786_3222.html  http://www.lemonde.fr/ameriques/article/2013/06/17/manifestations-au-bresil-le-gouvernement-menace-du-carton-rouge_3431786_3222.html  http:/
/www.bbc.co.uk/portuguese/noticias/130617_protestos_live.shtml  http://elpais.com/elpais/portada_america.html  #vemprarua #vempraruarj #vempraruabrasil #ogiganteacordou #geracaoinvencivel  @J.Oliveira  Curt
a a Página Geração Invencível: http://www.facebook.com/GeracaoInvencive',
  4  to_date('17-june-2013')
  5  );

1 row created.

SQL>
SQL> insert into test values( 2, '#AoVivo no acampamento #BrasilComLula com o Deputado Paulo Pimenta  #BrasilComLula #MoroPersegueLula', to_date('10-may-2017'));

1 row created.

SQL>
SQL>
SQL> select a, r.column_value, trim(regexp_substr(b,'#.*? ', 1, r.column_value)) str
  2  from   test,
  3         table(cast(
  4           multiset(
  5             select level from dual
  6             connect by level <= regexp_count(test.b,'#.*? ')
  7           ) as sys.OdciNumberList
  8         )) r;

         A COLUMN_VALUE STR
---------- ------------ ----------------------------------------
         1            1 #PROTESTOBR:
         1            2 #vemprarua
         1            3 #vempraruarj
         1            4 #vempraruabrasil
         1            5 #ogiganteacordou
         1            6 #geracaoinvencivel
         2            1 #AoVivo
         2            2 #BrasilComLula
         2            3 #BrasilComLula

9 rows selected.

SQL>
复制


文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论