oracle全文索引测试

  1. 指定词法分析器lexer
  2. --chinese_lexer对中文的分词效率较高,但数据库的编码必须是UTF8
    SYS@orcl>exec ctx_ddl.create_preference('my_lexer', 'chinese_lexer');
  3. 创建单个字段的全文索引
  4. SYS@orcl>desc t;
    Name				   Null?    Type
    --------------------
    NAME					    VARCHAR2(50)
    ROLE					    VARCHAR2(50)
    
    SYS@orcl>create index index_t on t(role) indextype is ctxsys.context
    2  parameters('lexer my_lexer');
    Index created.
  5. 使用全文索引进行查询
  6. SYS@orcl>select * from t where contains(role, '开发') > 0;
    --使用score函数查询关键词的匹配度
    SYS@orcl>select score(1), name, role from t where contains(role, '开发', 1) > 0;
  7. 同步和优化索引
  8. --默认情况下,全文索引不会同步更新,需要提交job定期更新
    SYS@orcl>exec ctx_ddl.sync_index('index_t');
    PL/SQL procedure successfully completed.
    
    --也可以在创建索引的时候指定提交时同步索引,但因为同步索引频率太高,容易造成索引严重碎片
    --一般情况下不建议这么做
    SYS@orcl>drop index index_t;
    Index dropped.
    
    SYS@orcl>create index index_t on t(role) indextype is ctxsys.context
    2  parameters('lexer my_lexer sync(on commit)');
    Index created.
    --定期优化索引以提高查询效率
    SYS@orcl>exec ctx_ddl.optimize_index('index_t', 'full');
    SYS@orcl>exec ctx_ddl.optimize_index('index_t', 'fast');
  9. 创建多个字段的全文索引
  10. --创建多列的datastore
    SYS@orcl>exec ctx_ddl.create_preference('t_name_role_ds', ‘multi_column_datastore');
    PL/SQL procedure successfully completed.
    
    SYS@orcl>exec ctx_ddl.set_attribute('t_name_role_ds', 'columns', 'name,role');
    PL/SQL procedure successfully completed.
    
    --创建索引
    SYS@orcl>create index index2_t on t(name)
    2  indextype is ctxsys.context
    3  parameters('lexer my_lexer datastore t_name_role_ds');
    Index created.

2012年2月20日,补充一下比较真实的测试数据

数据量:22500条,文本文件500K

表占用空间1M,索引约2.5M

SEGMENT_NAME                   BYTES/1024     BLOCKS
------------------------------ ---------- ----------
ORDER_SPECIAL_TEXT                   1024        128
DR$IND_SPECIAL_TEXT$I                2048        256
DR$IND_SPECIAL_TEXT$R                  64          8
DR$IND_SPECIAL_TEXT$X                 384         48

创建全文索引时间35秒,第一次运行逻辑读800左右,重复运行时逻辑读200左右;响应时间最慢0.15秒,重复运行时相应时间在0.05秒左右。

 

参考资料:
Oracle9i中全文检索的创建与使用
oracle全文索引的简单配置

Comments are closed, but trackbacks and pingbacks are open.