Posted on 2010-04-28 11:02
leekiang 閱讀(2169)
評論(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倍。