反向索引與模糊查詢

安佰勝發表於2011-05-06
反向索引與模糊查詢
---------------------
 
版本
SQL> select * from v$version;
BANNER                                                                         
----------------------------------------------------------------               
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod               
PL/SQL Release 10.2.0.4.0 - Production                                         
CORE 10.2.0.4.0 Production                                                     
TNS for 32-bit Windows: Version 10.2.0.4.0 - Production                        
NLSRTL Version 10.2.0.4.0 - Production                                         
 
測試資料
SQL> conn an/an
已連線。
SQL> create table atest(id int,name varchar2(100));
表已建立。
SQL> insert into atest select 1,'anbaisheng' from dual;
已建立 1 行。
SQL> insert into atest select 2,'xiangxiang' from dual;
已建立 1 行。
SQL> insert into atest select 3,'baby' from dual;
已建立 1 行。
SQL> commit;
提交完成。
SQL> select * from atest;
        ID NAME                                                                
---------- --------------------                                                
         1 anbaisheng                                                          
         2 xiangxiang                                                          
         3 baby                                                                
 
 
建立正續索引
SQL> create index ind_name on atest(name);
索引已建立。
SQL> set autotrace on
SQL> select * from atest where name like 'a%';
        ID NAME                                                                
---------- --------------------                                                
         1 anbaisheng                                                          
                                                                               
--------------------------------------------------------------------------------
| Id  | Operation                   | Name     | Rows  | Bytes | Cost (%CPU)| Ti
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |     1 |    65 |     2   (0)| 00
|   1 |  TABLE ACCESS BY INDEX ROWID| ATEST    |     1 |    65 |     2   (0)| 00
|*  2 |   INDEX RANGE SCAN          | IND_NAME |     1 |       |     1   (0)| 00
--------------------------------------------------------------------------------
                                                                               
Predicate Information (identified by operation id):                            
---------------------------------------------------                            
                                                                               
   2 - access("NAME" LIKE 'a%')                                                
       filter("NAME" LIKE 'a%')                                                

SQL> select * from atest where name like '%y';
        ID NAME                                                                
---------- --------------------                                                
         3 baby                                                                

---------------------------------------------------------------------------    
| Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time     |    
---------------------------------------------------------------------------    
|   0 | SELECT STATEMENT  |       |     1 |    65 |     3   (0)| 00:00:01 |    
|*  1 |  TABLE ACCESS FULL| ATEST |     1 |    65 |     3   (0)| 00:00:01 |    
---------------------------------------------------------------------------    
                                                                               
Predicate Information (identified by operation id):                            
---------------------------------------------------                            
                                                                               
   1 - filter("NAME" LIKE '%y')                                                
                                                                             
正向索引後模糊查詢有效,前模糊無效
 
建立反向索引
SQL> drop index ind_name;
索引已刪除。
SQL> create index ind_name on atest reverse(name);
索引已建立。
SQL>  select * from atest where name like '%y';
        ID NAME                                                                
---------- --------------------                                                
         3 baby                                                                

---------------------------------------------------------------------------    
| Id  | Operation         | Name  | Rows  | Bytes | Cost (%CPU)| Time     |    
---------------------------------------------------------------------------    
|   0 | SELECT STATEMENT  |       |     1 |    65 |     3   (0)| 00:00:01 |    
|*  1 |  TABLE ACCESS FULL| ATEST |     1 |    65 |     3   (0)| 00:00:01 |    
---------------------------------------------------------------------------    
                                                                               
Predicate Information (identified by operation id):                            
---------------------------------------------------                            
                                                                               
   1 - filter("NAME" LIKE '%y')                                                
                                                                               

SQL>  select * from atest where name like reverse ('%y');
未選定行
                                                                               
--------------------------------------------------------------------------------
| Id  | Operation                   | Name     | Rows  | Bytes | Cost (%CPU)| Ti
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |     1 |    65 |     0   (0)| 00
|   1 |  TABLE ACCESS BY INDEX ROWID| ATEST    |     1 |    65 |     0   (0)| 00
|*  2 |   INDEX RANGE SCAN          | IND_NAME |     1 |       |     0   (0)| 00
--------------------------------------------------------------------------------
                                                                               
Predicate Information (identified by operation id):                            
---------------------------------------------------                            
                                                                               
   2 - access("NAME" LIKE 'y%')                                                
       filter("NAME" LIKE 'y%')                                                
                                                                               
就算建上反向索引,不使用reverse關鍵字前模糊也沒用,後模糊無效
 
聯合部分反向索引
這個conception裡都沒說,自己試了下,還成
SQL> create index ind_idname on atest (id ,reverse(name));
索引已建立。
SQL> select * from atest where id=1 and name like reverse ('%y');
未選定行
                                                                               
--------------------------------------------------------------------------------
| Id  | Operation                   | Name     | Rows  | Bytes | Cost (%CPU)| Ti
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |          |     1 |    65 |     0   (0)| 00
|*  1 |  TABLE ACCESS BY INDEX ROWID| ATEST    |     1 |    65 |     0   (0)| 00
|*  2 |   INDEX RANGE SCAN          | IND_NAME |     1 |       |     0   (0)| 00
--------------------------------------------------------------------------------
                                                                               
Predicate Information (identified by operation id):                            
---------------------------------------------------                            
                                                                               
   1 - filter("ID"=1)                                                          
   2 - access("NAME" LIKE 'y%')                                                
       filter("NAME" LIKE 'y%')                                                
                                                                               
SQL>  select * from atest where id=1 and name like '%y';
未選定行
--------------------------------------------------------------------------------
| Id  | Operation                   | Name       | Rows  | Bytes | Cost (%CPU)|
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |            |     1 |    65 |     2   (0)|
|*  1 |  TABLE ACCESS BY INDEX ROWID| ATEST      |     1 |    65 |     2   (0)|
|*  2 |   INDEX RANGE SCAN          | IND_IDNAME |     1 |       |     1   (0)|
--------------------------------------------------------------------------------
                                                                               
Predicate Information (identified by operation id):                            
---------------------------------------------------                            
                                                                               
   1 - filter("NAME" LIKE '%y')                                                
   2 - access("ID"=1)                                                          
                                                                               
就算是聯合索引,不使用reverse的話意義也不大
 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13177610/viewspace-694601/,如需轉載,請註明出處,否則將追究法律責任。

相關文章