Conditions for index range scan, index fast full scan, index skip scan

Source link: https://blog.csdn.net/robinson1988/article/details/4980611

 

index range scan:

1. For unique index, if <,> ,between ...and... appears after the where condition, then index range scan may be executed. If the where condition is followed by =, then index unique will be executed. scan.

2. For none unique index, if =,>,<,beweed...and... appear after the where condition, it is possible to execute index range scan.

3. For the combined index, if the leading column of the combined index appears after the where condition, it is possible to perform an index range scan.

 

index fast full scan (index fast full scan):

If the columns after the select statement are all included in the composite index, and the leading column of the composite index does not appear after the where, and most of the data needs to be retrieved, then index fast full scan may be performed at this time. Conditions for index fast full scan to occur:

1. Must be a composite index. 2. The bootstrap column is not in the where condition

 

index skip scan

When the query can get results through the combined index, and the returned results are few, and the where condition does not contain the index guide column, the index skip scan may be executed.

Conditions under which an index skip scan occurs:

1. Must be a composite index.

2. The bootstrap column does not appear in the where condition

 

Let’s test the appeal theory through a simple experiment:

SQL> create table test as select * from dba_objects;

Table has been created.

SQL> create unique index ind_id on test(object_id); ind_id is a unique index

Index has been created.

SQL> create index ind_owner on test(owner); ind_owner is a non-unique index

Index has been created.

SQL> create index ooo on test(owner,object_name,object_type); ooo is a composite index

Index has been created.

SQL> exec dbms_stats.gather_table_stats('ROBINSON','TEST');

PL/SQL procedure completed successfully.

SQL> set autot trace
SQL> select owner from test where object_id=10;
执行计划
----------------------------------------------------------
Plan hash value: 2544773305

--------------------------------------------------------------------------------------
| Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |        |     1 |    11 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST   |     1 |    11 |     2   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | IND_ID |     1 |       |     1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------

SQL> select owner from test where object_id<10;

8 rows have been selected.
Execution plan
------------------------------------------------ ----------
Plan hash value: 1361604213

--------------------------------------------------------------------------------------
| Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |        |     7 |    77 |     3   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TEST   |     7 |    77 |     3   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | IND_ID |     7 |       |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------------------

For a unique index, when an index range scan occurs, multiple rows of records are returned, followed by >,<,between ..and..

SQL> select owner from test where owner='SCOTT';

13 rows have been selected.
Execution plan
------------------------------------------------ ----------
Plan hash value: 2280863269

------------------------------------------------------------------------------
| Id  | Operation        | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |           |  2936 | 17616 |     7   (0)| 00:00:01 |
|*  1 |  INDEX RANGE SCAN| IND_OWNER |  2936 | 17616 |     7   (0)| 00:00:01 |
------------------------------------------------------------------------------

For non-unique indexes, even if the restriction condition behind where is =, it is possible to return multiple rows, so perform index range scan

SQL> select object_name,object_type from test where owner='ROBINSON';

15 rows have been selected.
Execution plan
------------------------------------------------ ----------
Plan hash value: 2845720098

 

-------------------------------------------------------------------------
| Id  | Operation        | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------
|   0 | SELECT STATEMENT |      |  2936 |   114K|    23   (0)| 00:00:01 |
|*  1 |  INDEX RANGE SCAN| OOO  |  2936 |   114K|    23   (0)| 00:00:01 |
-------------------------------------------------------------------------

Because 000 is not the only index, and the guide column of index ooo is used after where, index range scan is performed.

SQL> select owner, object_name,object_type from test where object_name='EMP' ;
执行计划
----------------------------------------------------------
Plan hash value: 1799988433

-------------------------------------------------------------------------
| Id  | Operation        | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------
|   0 | SELECT STATEMENT |      |     2 |    80 |    19   (0)| 00:00:01 |
|*  1 |  INDEX SKIP SCAN | OOO  |     2 |    80 |    19   (0)| 00:00:01 |
-------------------------------------------------------------------------

Because the information required by the query can be obtained through the index ooo, and there is no leading column owner behind where, and the number of rows returned is very small (there is only one row here), so CBO chooses index skip scan, where autotrace does not show how many rows are returned, the following shows one time

SQL> set autot off
SQL> select owner, object_name,object_type from test where object_name='EMP' ;

OWNER                          OBJECT_NAME          OBJECT_TYPE
------------------------------ -------------------- -------------------
SCOTT                          EMP                  TABLE

SQL> select owner, object_name,object_type from test where object_type='INDEX';

Rows 1701 have been selected.
Execution plan
------------------------------------------------ ----------
Plan hash value: 3464522019

-----------------------------------------------------------------------------
| Id  | Operation            | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |      |  1721 | 68840 |    70   (3)| 00:00:01 |
|*  1 |  INDEX FAST FULL SCAN| OOO  |  1721 | 68840 |    70   (3)| 00:00:01 |
-----------------------------------------------------------------------------

Because the information required for the query can be obtained through the index ooo, and there is no leading column owner behind where, and the number of rows returned is large (1701 rows), CBO chooses index fast full scan, which avoids full table scan.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325319181&siteId=291194637