hint指定index的深入理解

创建一个表,含有位图index和b-tree index

SQL> create table t as select object_id id ,object_name from dba_objects;
Table created.
SQL> create index b_tree_ind on t(id);
Index created.

SQL> create bitmap index b_bm_name on t(object_name);
Index created.

SQL> exec dbms_stats.gather_table_stats('HR','T',cascade=> true);
SELE
PL/SQL procedure successfully completed.

SQL> set autotrace trace exp stat

SQL> select id from t;
50365 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      | 47448 |   602K|    57   (2)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| T    | 47448 |   602K|    57   (2)| 00:00:01 |
--------------------------------------------------------------------------
Note
-----
   - dynamic sampling used for this statement
Statistics
----------------------------------------------------------
         28  recursive calls
          0  db block gets
       3646  consistent gets
          0  physical reads
          0  redo size
     728900  bytes sent via SQL*Net to client
      37312  bytes received via SQL*Net from client
       3359  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      50365  rows processed

这里因为object_id列可能有null值,所以不会使用b_tree_t_xifenfei索引,预料之中事件

SQL> select /*+ index(t b_tree_ind) */ id from t
50365 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3465251059
------------------------------------------------------------------------------------------
| Id  | Operation                    | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |           | 47448 |   602K|   805   (1)| 00:00:10 |
|   1 |  TABLE ACCESS BY INDEX ROWID | T         | 47448 |   602K|   805   (1)| 00:00:10 |
|   2 |   BITMAP CONVERSION TO ROWIDS|           |       |       |            |          |
|   3 |    BITMAP INDEX FULL SCAN    | B_BM_NAME |       |       |            |          |
------------------------------------------------------------------------------------------
Note
-----
   - dynamic sampling used for this statement
Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
      27318  consistent gets
          0  physical reads
          0  redo size
    2147500  bytes sent via SQL*Net to client
      37312  bytes received via SQL*Net from client
       3359  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      50365  rows processed

这里因为object_id列可能有null值,所以不会使用b_tree_t_xifenfei索引,这里的疑惑是:
就算不会使用b_tree_t_xifenfei index也不应该会使用BITMAP_T_XIFENFEI index,因为使用这个的cost会大于全表扫描

SQL> select /*+ index(t aaa) */ id from t;
50365 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 3465251059
------------------------------------------------------------------------------------------
| Id  | Operation                    | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |           | 50365 |   245K|   805   (1)| 00:00:10 |
|   1 |  TABLE ACCESS BY INDEX ROWID | T         | 50365 |   245K|   805   (1)| 00:00:10 |
|   2 |   BITMAP CONVERSION TO ROWIDS|           |       |       |            |          |
|   3 |    BITMAP INDEX FULL SCAN    | B_BM_NAME |       |       |            |          |
------------------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
      27318  consistent gets
          0  physical reads
          0  redo size
    2147500  bytes sent via SQL*Net to client
      37312  bytes received via SQL*Net from client
       3359  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      50365  rows processed

这里使用了一个无效的index,也使用了BITMAP_T_XIFENFEI,让人更加的感觉奇怪。
官方文档上有如下解释
If the INDEX hint specifies a single available index, then the database performs a scan on this index. The optimizer does not consider a full table scan or a scan of another index on the table.
For a hint on a combination of multiple indexes, Oracle recommends using INDEX_COMBINE rather than INDEX, because it is a more versatile hint. If the INDEX hint specifies a list of available indexes, then the optimizer considers the cost of a scan on each index in the list and then performs the index scan with the lowest cost. The database can also choose to scan multiple indexes from this list and merge the results, if such an access path has the lowest cost. The database does not consider a full table scan or a scan on an index not listed in the hint.
If the INDEX hint specifies no indexes, then the optimizer considers the cost of a scan on each available index on the table and then performs the index scan with the lowest cost. The database can also choose to scan multiple indexes and merge the results, if such an access path has the lowest cost. The optimizer does not consider a full table scan.
如果我们使用hint指定了一个无效的index,优化器会扫描表中所有可以使用的index,然后选择cost最小的index或者index组合,而不会选择全表扫描。
因为我们hint指定b_tree_t_xifenfei index的时候,因为object_id可能有值为空(列没定义为not null),所以不能使用该index,从而也就是相当于一个无效的index,从而扫描该表的其他可以使用的index,导致使用了位图索引(该类型index不排除null),而不是全表扫描.在9i和10g上有上述现象,11g时Oracle对上述现象有所改进
注意:使用hint指定index的时候需要慎重,如果不合适或者无效,可能导致程序效率更低


感谢惜分飞的指点

参考至:http://www.xifenfei.com/2865.html

如有错误,欢迎指正

邮箱:[email protected]

你可能感兴趣的:(Oracle,Tuning)