oracle10g的全文索引


http://hi.baidu.com/lichangzai/blog/item/fe7fc90176b41807728da543.html
http://www.oracle.com/global/cn/oramag/oracle/04-sep/o54text.html
http://yangtingkun.itpub.net/post/468/228849

在對海量的文本數據進行搜索時,建議使用全文檢索功能。
創建全文索引環境

1.在sysdba權限下驗證是否有ctxsys用戶和ctxapp角色:
sql>select username from all_users;
如果沒有該用戶,則需要打開dbca工具中選擇configrue database options,然后選擇所有數據庫組件安裝即可。

2.用在sysdba權限下或ctxsys用戶下授予prt用戶執行ctx_ddl的權限
sql>grant execute on ctx_ddl to prt;

3.prt用戶下創建分詞
sql>conn prt/prt
sql>exec ctx_ddl.create_preference ('PRT_CHINESE_LEXER', 'chinese_vgram_lexer');

4.在某個字段上創建全文索引
create index idx_ctx_movie_desc on movie(desc) indextype is ctxsys.context parameters('LEXER PRT_CHINES_LEXER');

5.定期同步優化全文索引

/*索引建好后,由於對表所發生的任何DML語句,都不會自動修改索引,因此,必須定時同步(sync)和優化(optimize)索引,以正確反映數據的變化,所以要定期同步和優化全文索引。*/
create or replace procedure p_lexer_movie is
begin
 ctx_ddl.sync_index('IDX_CTX_MOVIE_DESC');
 ctx_ddl.optimize_index('IDX_CTX_MOVIE_DESC','FULL');
end;

var i number;
begin
dbms_job.submit(:j,'p_lexer_movie;',sysdate,'sysdate + 1/48');
commit;
end;
/

全文索引的作用包括:1.提高查詢速度,2.切詞,3.針對所有數據類型(包括blob等)進行全文檢索,4.可以對超過150種文件類型(如doc,txt,pdf,xml)進行檢索

1.提高查詢速度
select * from movie where containts(desc,'探險')>0;
查看執行計划走的是domain index:IDX_CTX_MOVIE_DESC

在數據量很大的情況下比如下sql速度提高n倍
select * from movie where instr(desc,'探險')>0;

經過測試,在5600萬條記錄的表中,查詢出符合條件的117條記錄,用全文索引只需要2.391s,而用第二種方法,則需要99.515s才能出結果。

2.實現切詞功能(yangtingkun):
create or replace function f_split_chinese(as_input in varchar2)
  return varchar2 as
  pragma autonomous_transaction;
  v_return varchar2(32767);
begin
  begin
    execute immediate 'drop table t_temp_table purge';
  exception
    when others then
      null;
  end;

  execute immediate 'create table t_temp_table (str varchar2(4000))';
  execute immediate 'insert into t_temp_table values (:1)'
    using as_input;
  execute immediate 'create index ind_t_temp_table_str on t_temp_table(str) indextype is ctxsys.context
                            parameters(''lexer SMS_CHINESE_LEXER'')';
  execute immediate 'select max(ltrim(sys_connect_by_path(token_text, '',''), '',''))
                      from
                      (
                      select token_text, row_number() over(order by token_text) rn
                      from dr$ind_t_temp_table_str$i
                      )
                      start with rn = 1
                      connect by prior rn + 1 = rn'
    into v_return;
  execute immediate 'drop table t_temp_table purge';
  return v_return;
end;

SQL> select f_split_chinese('優化你的文本檢索') from dual;
f_split_chinese('優化你的文本?
-----------------------------------------
本檢,的文,化你,檢索,你的,索,文本,優化

sql>exec ctx_ddl.drop_preference('PRT_CHINESE_LEXER');
sql>exec ctx_ddl.create_preference('PRT_CHINESE_LEXER', 'CHINESE_LEXER');

SQL> select f_split_chinese('優化你的文本檢索') from dual;
F_SPLIT_CHINESE('優化你的文本?
---------------------------------
檢索,你的,文本,優化

 

轉帖自:http://space.itpub.net/559237/viewspace-592339


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM