MDA/MDD/TDD/DDD/DDDDDDD
posts - 536, comments - 111, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

oracle模糊查询

Posted on 2010-04-28 11:02 leekiang 阅读(2150) 评论(1)  编辑  收藏 所属分类: oracle
需要查询某字段是否包含一个值111是否存在于1111,2111,1112,1121,1113,中 ,
因为根据","逗号分开,要求的答案是:不存在。

用传统的like '%111,%',显然不合适,这样虽然111不存在但是依然能查到该条记录。
所以应该用以下语句实现:
select * from Table where ','+columA  like '%,111,%'

like '%AAA%'   这样的左右模糊查询不能用上索引,Oracle没法通过B-TREE找到相应的叶子节点,位图索引也是一样
而like '...%'和 Like '%...'是可以走索引的,后者需要reverse一下

使用where instr(column_name,'AAA')> 0没有什么效果


如果确定like部分选择性很强,试试:
select * from xxfl where rowid in (select rowid from xxfl where hphm like '%34443%' ) and jgsj between to_date('xxxx-xx-xx xx:xx:xx','yyyy-mm-dd hh24:mi:ss') and to_date('xxxx-xx-xx xx:xx:xx','yyyy-mm-dd hh24:mi:ss');

参考:
http://www.javaeye.com/topic/653713
http://www.itpub.net/viewthread.php?tid=1218563
http://sandish.itpub.net/post/4899/464369

别人的笔记:
sql中的like '%xx%'模糊查询无法走索引,影响执行速度。经测试itpub版主ifree的index_ffs+rowid方法比较有效,记录一下。
这里是示例:
scott@ORCL> CREATE INDEX SCOTT.i_dept_name
  2   ON SCOTT.DEPT(DNAME)
  3  ;

Index created.

scott@ORCL> Analyze Table SCOTT.DEPT Compute Statistics ;

Table analyzed.

scott@ORCL> select * from scott.dept where
  2  rowid in (
  3  select /*+ index_ffs(a i_dept_dname) */
  4  rowid from scott.dept a where dname like '%A%')
  5  ;

这个方法要求like查询出的记录不能太多,在我的应用中,这一方法使sql效率提高了近10倍。

评论

# re: oracle模糊查询[未登录]  回复  更多评论   

2011-08-01 23:19 by vv
ddd

只有注册用户登录后才能发表评论。


网站导航: