合适的索引与执行计划

RDBMS12.2.0.1

-- 创建测试用的表

create table t2 as select * from dba_objects ; 
SYS@test>select count(*) from t2;    

  COUNT(*)
----------
 113246208

-- 测试内容
1 查询不使用索引
select object_name from t2 where object_id=2;
2 查询使用where条件中的索引
select object_name from t2 where object_id=2;
3 select和where中都有索引
select object_name from t2 where object_id=2;
4 创建组合索引,where条件为前导列
select object_name from t2 where object_id=2;
5 创建组合索引,select的字段为前导列
select object_name from t2 where object_id=2;
6 查询使用select语句中的字段
select object_name from t2 where object_id=2;
7 修改object_id=2 ,object_id上有索引(该索引较差)
update t2 set object_id=2;
commit;
-- 走了索引INDEX FAST FULL SCAN ,没有回表,效率相对比全表扫描少
select object_id from t2 where object_id=2 ;
select /*+ full(T2) */ object_id from t2 where object_id=2 ;
-- 换个写法,因为object_id都是2,所以优化器认为使用全表扫描效率比较好。
select * from t2 where object_id=2 ;
select /*+ index(T2 IDX_OBJID_T2) */ * from t2 where object_id=2 ;


-- 测试过程

1 没有索引。执行计划走了全表扫描。成本93632

select object_name from t2 where object_id=2;

SYS@test>select object_name from t2 where object_id=2;

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 1513984157

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  3794 |   292K| 93632   (1)| 00:00:04 |
|*  1 |  TABLE ACCESS FULL| T2   |  3794 |   292K| 93632   (1)| 00:00:04 |
--------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"=2)

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)


Statistics
----------------------------------------------------------
          5  recursive calls
          0  db block gets
     688360  consistent gets
     344839  physical reads
          0  redo size
        347  bytes sent via SQL*Net to client
        596  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed

SYS@test>

2 object_id有索引 ,执行计划使用了索引扫描,回了表

create index idx_objid_t2 on t2(object_id) ;
select object_name from t2 where object_id=2;

SYS@test>select object_name from t2 where object_id=2;

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 2765286821

----------------------------------------------------------------------------------------------------
| Id  | Operation                           | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |              |     1 |    79 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID BATCHED| T2           |     1 |    79 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN                  | IDX_OBJID_T2 |     1 |       |     2   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------

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

   2 - access("OBJECT_ID"=2)

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)


Statistics
----------------------------------------------------------
          9  recursive calls
          0  db block gets
        109  consistent gets
          2  physical reads
          0  redo size
        347  bytes sent via SQL*Net to client
        596  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed

SYS@test>

3 object_id和object_name有索引 ,但是只用了object_id列上的索引

create index idx_obname on t2(object_name);
select object_name from t2 where object_id=2;

--select /*+ index_join(T2 IDX_OBJID_T2 idx_obname)*/ object_name from t2 where object_id=2;   -- 不适合索引join,适合索引join的情况,where条件join 

SYS@test>select object_name from t2 where object_id=2;

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 2765286821

----------------------------------------------------------------------------------------------------
| Id  | Operation                           | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |              |     1 |    79 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID BATCHED| T2           |     1 |    79 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN                  | IDX_OBJID_T2 |     1 |       |     2   (0)| 00:00:01 |
----------------------------------------------------------------------------------------------------

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

   2 - access("OBJECT_ID"=2)

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)


Statistics
----------------------------------------------------------
          9  recursive calls
          0  db block gets
        110  consistent gets
          0  physical reads
          0  redo size
        347  bytes sent via SQL*Net to client
        596  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed

SYS@test>

4 object_id和object_name组合索引 ,使用了组合索引

drop index idx_objid_t2;
drop index idx_obname;
create index idx_obj_id_name on t2(object_id,object_name);
select object_name from t2 where object_id=2;

SYS@test>select object_name from t2 where object_id=2;

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 438204577

------------------------------------------------------------------------------------
| Id  | Operation        | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |                 |     1 |    79 |     3   (0)| 00:00:01 |
|*  1 |  INDEX RANGE SCAN| IDX_OBJ_ID_NAME |     1 |    79 |     3   (0)| 00:00:01 |
------------------------------------------------------------------------------------

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

   1 - access("OBJECT_ID"=2)

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)


Statistics
----------------------------------------------------------
          9  recursive calls
          0  db block gets
        111  consistent gets
          3  physical reads
          0  redo size
        347  bytes sent via SQL*Net to client
        596  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed

SYS@test>

5 object_name和object_id组合索引 ,使用了组合索引,但是成本天壤之别(索引的前导列不同)

drop index idx_obj_id_name ;
create index idx_obj_name_id on t2(object_name,object_id);
select object_name from t2 where object_id=2;

SYS@test>select object_name from t2 where object_id=2;

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 3993461820

----------------------------------------------------------------------------------------
| Id  | Operation            | Name            | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |                 |  3794 |   292K| 31757   (1)| 00:00:02 |
|*  1 |  INDEX FAST FULL SCAN| IDX_OBJ_NAME_ID |  3794 |   292K| 31757   (1)| 00:00:02 |
----------------------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"=2)

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)


Statistics
----------------------------------------------------------
          5  recursive calls
          0  db block gets
     116967  consistent gets
     116822  physical reads
          0  redo size
        347  bytes sent via SQL*Net to client
        596  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed

SYS@test>

6 object_name列上有索引,不会走索引,加hint提示,强行走索引 ,尽管有hint ,但是O还是忽略了

select object_name from t2 where object_id=2;
select /*+ index(t2 idx_obname)*/ object_name from t2 where object_id=2;

SYS@test>select object_name from t2 where object_id=2;

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 1513984157

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  3794 |   292K| 93632   (1)| 00:00:04 |
|*  1 |  TABLE ACCESS FULL| T2   |  3794 |   292K| 93632   (1)| 00:00:04 |
--------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"=2)

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)


Statistics
----------------------------------------------------------
         49  recursive calls
          0  db block gets
     688392  consistent gets
     344839  physical reads
          0  redo size
        347  bytes sent via SQL*Net to client
        596  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          4  sorts (memory)
          0  sorts (disk)
          0  rows processed

SYS@test>

SYS@test>select /*+ index(t2 idx_obname)*/ object_name from t2 where object_id=2;

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 1513984157

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  3794 |   292K| 93632   (1)| 00:00:04 |
|*  1 |  TABLE ACCESS FULL| T2   |  3794 |   292K| 93632   (1)| 00:00:04 |
--------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"=2)

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)


Statistics
----------------------------------------------------------
          4  recursive calls
          0  db block gets
     688355  consistent gets
     344839  physical reads
          0  redo size
        347  bytes sent via SQL*Net to client
        596  bytes received via SQL*Net from client
          1  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          0  rows processed

SYS@test>

7 修改object_id=2 ,object_id上有索引(该索引较差)

SYS@test>select object_id from t2 where object_id=2 ;

20277760 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 170535381

-------------------------------------------------------------------------------------
| Id  | Operation            | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |              |    23M|   297M| 10851   (1)| 00:00:01 |
|*  1 |  INDEX FAST FULL SCAN| IDX_OBJID_T2 |    23M|   297M| 10851   (1)| 00:00:01 |
-------------------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"=2)

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)


Statistics
----------------------------------------------------------
          9  recursive calls
          0  db block gets
    1389106  consistent gets
      39877  physical reads
       6644  redo size
  359592737  bytes sent via SQL*Net to client
   14870957  bytes received via SQL*Net from client
    1351852  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
   20277760  rows processed

SYS@test>
SYS@test>select /*+ full(T2) */ object_id from t2 where object_id=2 ;

20277760 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1513984157

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |    20M|    58M| 93605   (1)| 00:00:04 |
|*  1 |  TABLE ACCESS FULL| T2   |    20M|    58M| 93605   (1)| 00:00:04 |
--------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"=2)


Statistics
----------------------------------------------------------
          2  recursive calls
          0  db block gets
    2018085  consistent gets
     344839  physical reads
          0  redo size
  359592737  bytes sent via SQL*Net to client
   14870957  bytes received via SQL*Net from client
    1351852  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
   20277760  rows processed

SYS@test>

-- 直接走全表扫描,效率相对走索引好一些

SYS@test>select * from t2 where object_id=2 ;


20277760 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 1513984157

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |    20M|  2185M| 93847   (1)| 00:00:04 |
|*  1 |  TABLE ACCESS FULL| T2   |    20M|  2185M| 93847   (1)| 00:00:04 |
--------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"=2)


Statistics
----------------------------------------------------------
         26  recursive calls
          0  db block gets
    2018151  consistent gets
     344839  physical reads
          0  redo size
 1166602921  bytes sent via SQL*Net to client
   14870957  bytes received via SQL*Net from client
    1351852  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
   20277760  rows processed

SYS@test>SYS@test>

-- 强行走索引,效率更差

SYS@test>select /*+ index(T2 IDX_OBJID_T2) */ * from t2 where object_id=2 ;

20277760 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 2765286821

----------------------------------------------------------------------------------------------------
| Id  | Operation                           | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |              |    20M|  2185M|   401K  (1)| 00:00:16 |
|   1 |  TABLE ACCESS BY INDEX ROWID BATCHED| T2           |    20M|  2185M|   401K  (1)| 00:00:16 |
|*  2 |   INDEX RANGE SCAN                  | IDX_OBJID_T2 |    19M|       | 37665   (1)| 00:00:02 |
----------------------------------------------------------------------------------------------------

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

   2 - access("OBJECT_ID"=2)


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
    3406712  consistent gets
     335716  physical reads
   30345216  redo size
 2708848447  bytes sent via SQL*Net to client
   14870957  bytes received via SQL*Net from client
    1351852  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
   20277760  rows processed

SYS@test>

 

END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值