设为首页 加入收藏

TOP

Oracle不使用索引的几种情况列举(二)
2017-01-24 08:15:17 】 浏览:611
Tags:Oracle 使用 索引 情况 列举
索引:一个对应值M,另一个对应值F。
zx@ORCL>create table t1 as select decode(mod(rownum,2),0,'M','F') gender,all_objects.* from all_objects;
?
Table created.
?
zx@ORCL>create index idx_t1 on t1(gender,object_id);
?
Index created.
?
zx@ORCL>exec dbms_stats.gather_table_stats(user,'T1',cascade=>true);
?
PL/SQL procedure successfully completed.
?
zx@ORCL>set autotrace traceonly explain
zx@ORCL>select * from t1 where object_id=42;
?
Execution Plan
----------------------------------------------------------
Plan hash value: 4072187533
?
-------------------------------------------------------------------------------------
| Id? | Operation? ? ? ? ? ? ? ? ? | Name? | Rows? | Bytes | Cost (%CPU)| Time? ? |
-------------------------------------------------------------------------------------
|? 0 | SELECT STATEMENT? ? ? ? ? ? |? ? ? |? ? 1 |? 100 |? ? 4? (0)| 00:00:01 |
|? 1 |? TABLE ACCESS BY INDEX ROWID| T1? ? |? ? 1 |? 100 |? ? 4? (0)| 00:00:01 |
|*? 2 |? INDEX SKIP SCAN? ? ? ? ? | IDX_T1 |? ? 1 |? ? ? |? ? 3? (0)| 00:00:01 |
-------------------------------------------------------------------------------------
?
Predicate Information (identified by operation id):
---------------------------------------------------
?
? 2 - access("OBJECT_ID"=42)
? ? ? filter("OBJECT_ID"=42)


INDEX SKIP SCAN 步骤告诉Oralce要跳跃式扫描这个索引,查询GENDER值有改变的地方,并从那里开始向下读树,然后在所考虑的各个虚拟索引中查询OBJECT_id=42。如果大幅增加GENDER的可取值,如下:
zx@ORCL>alter table t1 modify GENDER varchar2(2);
?
Table altered.
?
zx@ORCL>update t1 set gender=(chr(mod(rownum,1024)));
?
84656 rows updated.
?
zx@ORCL>commit;
?
Commit complete.
?
zx@ORCL>exec dbms_stats.gather_table_stats(user,'T1',cascade=>true);
?
PL/SQL procedure successfully completed.
?
zx@ORCL>set autotrace traceonly explain
zx@ORCL>select * from t1 where object_id=42;
?
Execution Plan
----------------------------------------------------------
Plan hash value: 1601196873
?
--------------------------------------------------------------------------
| Id? | Operation? ? ? ? | Name | Rows? | Bytes | Cost (%CPU)| Time? ? |
--------------------------------------------------------------------------
|? 0 | SELECT STATEMENT? |? ? ? |? ? 1 |? 101 |? 344? (1)| 00:00:05 |
|*? 1 |? TABLE ACCESS FULL| T1? |? ? 1 |? 101 |? 344? (1)| 00:00:05 |
--------------------------------------------------------------------------
?
Predicate Information (identified by operation id):
---------------------------------------------------
?
? 1 - filter("OBJECT_ID"=42)


情况2:


在使用select count(*) from t查询(或类似的查询),而且在表T上有一个B*树索引。不过,优化器并不是统计索引条目,而是在全面扫描这个表(尽管索引比表要小)。在这种情况下,索引可能建立在一个允许有NULL值的列上。由于对于索引键完全为null的行不会建立相应的索引条目,所以索引中的行数可能并不是表中的行数。这里优化器的选择是对的,如若不然,倘若它使用索引来统计行数,则可能会得到一个错误的答案。


zx@ORCL>desc t;
?Name? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Null?? ? Type
?----------------------------------------------------------------------------------------------------- -------- --------------------------------------------------------------------
?X? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NUMBER
?Y? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NUMBER
?Z? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CHAR(23)
zx@ORCL>select count(*) from t;
?
Execution Plan
----------------------------------------------------------
Plan hash value: 2966233522
?
-----------------------------------------------------------

首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇RMAN无法删除归档日志 下一篇Oracle order by子句对NULL的排序

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目