博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hint指定index的深入理解
阅读量:4963 次
发布时间:2019-06-12

本文共 5278 字,大约阅读时间需要 17 分钟。

http://czmmiao.iteye.com/blog/1480247创建一个表,含有位图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的时候需要慎重,如果不合适或者无效,可能导致程序效率更低

转载于:https://www.cnblogs.com/svennee/p/4083364.html

你可能感兴趣的文章
Windows向Linux上传文件夹
查看>>
20180104-高级特性-Slice
查看>>
6个SQL Server 2005性能优化工具介绍
查看>>
nginx启动、关闭命令、重启nginx报错open() "/var/run/nginx/nginx.pid" failed
查看>>
BZOJ 3097 Hash Killer I
查看>>
UINavigationController的视图层理关系
查看>>
html阴影效果怎么做,css 内阴影怎么做
查看>>
宏观经济
查看>>
综合练习:词频统计
查看>>
BZOJ1026: [SCOI2009]windy数
查看>>
样板操作数
查看>>
64位UBUNTU下安装adobe reader后无法启动
查看>>
iTextSharp带中文转换出来的PDF文档显示乱码
查看>>
组件:slot插槽
查看>>
走进C++程序世界------异常处理
查看>>
Nginx配置文件nginx.conf中文详解(转)
查看>>
POJ 1308 Is It A Tree?(并查集)
查看>>
N进制到M进制的转换问题
查看>>
利用sed把一行的文本文件改成每句一行
查看>>
Android应用开发:核心技术解析与最佳实践pdf
查看>>