Oracle Fuzzy Query (2. How to Test the Time of Fuzzy Query and Whether to Use Index) Reverse Index and Fuzzy Query

Source: http://blog.csdn.net/haiross/article/details/12974581
Comments: The database index is divided into forward and reverse indexes. When writing sql for fuzzy query, pay attention to the

reverse
index and fuzzy query. Introduction to the usage of the fuzzy index before applying it:

1. Reverse index

SQL> create index ind_name on atest reverse(name);

SQL> select * from atest where name like reverse ('%y');


2. Reverse the union part Index

SQL> create index ind_idname on atest (id ,reverse(name));
the index has been created.
SQL> select * from atest where id=1 and name like reverse ('%y');
unselected rows

The specific usage is shown as follows:

reverse index and fuzzy query
-------------- -------

Version
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                                        

test data
SQL> conn an/an
connected.
SQL> create table atest(id int,name varchar2(100));
The table has been created.
SQL> insert into atest select 1,'anbaisheng' from dual;
1 row created.
SQL> insert into atest select 2,'xiangxiang' from dual;
1 row created.
SQL> insert into atest select 3,'baby' from dual;
1 row created.
SQL> commit;
commit completed.
SQL> select * from atest;
        ID NAME                                                               
---------- --------------------                                               
         1 anbaisheng                                                         
         2 xiangxiang                                                         
         3 baby                                                               


Create a positive continuous index
SQL > create index ind_name on atest(name);
the index has been created.
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') The                                               
                                                                            
fuzzy query after the forward index is valid, but the former fuzzy query is invalid.

Create a reverse index
SQL> drop index ind_name;
the index has been deleted.
SQL> create index ind_name on atest reverse(name); The
index has been created.
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%')                                               
                                                                              
Even if an inverted index is built, it is useless to blur before and after the reverse keyword is not used.

Combined partial reverse index
This concept does not say that, I tried it myself, and it still works. into
SQL> create index ind_idname on atest (id , reverse(name));
the index has been created.
SQL> select * from atest where id=1 and name like reverse ('%y');
no row selected
                                                                              
------------------------- -------------------------------------------------- -----
| Id | Operation | Name | Rows | Bytes
| -------------------------------------------------- ---
| 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';
row not selected
--------------------------------------------------------------------------------
| 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)                                                         
                                                                              
even if it is a joint index, it doesn't make much sense without reverse

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327007036&siteId=291194637