substr function to create the index test

Junior partner in the technology group, said communication is often a SQL query slow, single-table SQL column as a condition, the column is an int value type, the type of index created by default.

A text .SQL
substr index creation function test
SELECT * from ( SELECT substr (nm, 0 , . 17 ) Nm1 lrrq from bbm2019) WHERE nm1in ( ' 55552389655808973 ' )

create a test table
SQL>create table tt as select * from dba_objects;
SQL> desc TT 
 the OBJECT_ID NUMBER
two optimization ideas.
2.1 by modifying the SQL text
SQL
> SELECT * from ( SELECT substr (object_id, 0 , . 4 ) CC from TT) T WHERE t.cc in ( ' 2559 ' ); --- -------------------------------------------------- --------------------- | Id | Operation | the Name | the Rows | Bytes | Cost (% the CPU) | Time | ---------- -------------------------------------------------- -------------- | 0 | the SELECT a STATEMENT | | 870 | 4350 | 347 (1)| 00:00:05 | |* 1 | TABLE ACCESS FULL| TT | 870 | 4350 | 347 (1)| 00:00:05 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter(SUBSTR(TO_CHAR("OBJECT_ID"),0,4)='2559') SQL> select substr(object_id,0,4) from tt where substr(object_id,0,4) in ('2559'); -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 870 | 4350 | 347 (1)| 00:00:05 | |* 1 | TABLE ACCESS FULL| TT | 870 | 4350 | 347 (1)| 00:00:05 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter(SUBSTR(TO_CHAR("OBJECT_ID"),0,4 ) = ' 2559 ' ) is invalid, the SQL or a full table scan, except testing.
2.2 adjusted index character format, SQL access using% fuzzy match SELECT substr (nm, 0 , . 17 ) Nm1 lrrq from bbm2019 WHERE nm1in like ' 55552389655808973% ' ; the SQL > Create index tt_obje_ind ON TT (object_id); the SQL > SET AUTOTRACE ON the SQL > SELECT object_id from TT WHERE object_id like ' 25599% ' ; -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 14 | 182 | 347 (1)| 00:00:05 | |* 1 | TABLE ACCESS FULL| TT | 14 | 182 | 347 (1)| 00:00:05 | -------------------------------------------------- ------------------------
can be found when the table field is a numeric type, use the character format like access, it is unable to obtain the results.
The SQL > drop index tt_obje_ind; the SQL > Create index tt_obje_ind ON TT (TO_CHAR (object_id)); the SQL > SELECT object_id from TT WHERE object_id like ' 25599% ' ; ---------------- -------------------------------------------------- ------------------------- | Id | Operation | the Name | the Rows | Bytes | Cost (% the CPU) | Time | ------ -------------------------------------------------- ----------------------------------- | 0 | SELECT STATEMENT | | 1 | 35 | 3 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| TT | 1 | 35 | 3 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | TT_OBJE_IND | 1 | | 2 (0)| 00:00:01 | ------------------------------------------------------------------------------------------- SQL> select object_id from tt where object_id like '2559%'; ------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 11 | 385 | 5 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| TT | 11 | 385 | 5 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | TT_OBJE_IND | 11 | | 2 (0)| 00:00:01 | -------------------------------------------------------------------------------------------

      2 - access(TO_CHAR("OBJECT_ID") LIKE '2559%')
          filter(TO_CHAR("OBJECT_ID") LIKE '2559%')


To_char can be adjusted using the index storage format to a character type, where the conditions can be queried using% taking the index for quick access.
The SQL > SELECT object_id from TT WHERE object_id = ' 25599 ' ; the SQL > SELECT object_id from TT WHERE object_id in ' 25599 ' ; the SQL > SELECT substr (object_id, 0 , . 4 ) from TT WHERE object_id in ( ' 2559 ' ); -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 14 | 490 | 347 (1)| 00:00:05 | |* 1 | TABLE ACCESS FULL| TT | 14 | 490 | 347 (1)| 00:00:05 | -------------------------------------------------- ------------------------
find use in = did not take the index!
   1 - filter ( "OBJECT_ID" = 25599) can be found to_char not shown
here, the first time that the conversion risk oracle priority, shadow might wonder if taking the index, due to the oracle to_number a higher priority. So even if we to_char wording ' xx 'character equivalent value queries,
the Oracle automatically converted to numeric type, because the index is a character type, and therefore can not take the index.
SQL> select object_id from tt where to_char(object_id)='25599';  

-------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 870 | 23490 | 72 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TT | 870 | 23490 | 72 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | TT_OBJE_IND | 348 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

2 - access(TO_CHAR("OBJECT_ID")='25599')

Does not create a functional index directly to_char class, query 

2.3 Create Substr functional index
the SQL
> drop index tt_obje_ind;
the SQL
> Create index tt_obje_ind ON TT (substr (object_id, 0 , . 4 ));
the SQL
> SELECT substr (object_id, 0 , . 4 ) from TT WHERE substr (object_id, 0 , . 4 ) in ( ' 2559 ' );
------------------------------ --------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (% CPU) | Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 11 | 110 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| TT_OBJE_IND | 11 | 110 | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access(SUBSTR(TO_CHAR("OBJECT_ID"),0,4)='2559')
SQL
> create index tt_obje_ind on tt(substr(to_char(object_id),0,4));
SQL
> select substr(object_id,0,4) from tt where substr(object_id,0,4) in ('2559');
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 11 | 110 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| TT_OBJE_IND | 11 | 110 | 1 (0)| 00:00:01 |
-------------------------------------------------- ------------------------------ Predicate Information (IDENTIFIED Operation by the above mentioned id): ----------- ---------------------------------------- 1 - Access (SUBSTR (the TO_CHAR ( " OBJECT_ID " ), 0 , 4 ) = ' 2559 ' ) to create a functional index substr, oracle will automatically to_char conversion, may be displayed together with the syntax to create an index (time to increase, with the best grammar specifications -)



on tt(substr(to_char(object_id),0,4)); 

 

Guess you like

Origin www.cnblogs.com/lvcha001/p/11387249.html