您的位置:首页 > 数据库 > Oracle

本地索引中 前缀索引和非前缀索引的测试 oracle

2016-05-29 22:14 369 查看
周末参加了acoug,听了老杨关于分区的介绍。

回来测试一下

 SQL> select * from v$version where rownum=1
2 ;

BANNER CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production 0

SQL>
 --首先建立分区表,先测local前缀索引

SQL> create table ml_1(id int,name varchar2(40))
2 partition by range(id)
3 (partition p1 values less than(100),
4 partition p2 values less than(200));

Table created

--创建id为前缀的本地索引
SQL> create index idx_ml_1 on ml_1(id,name) local;

Index created

SQL> insert into ml_1 select rownum rn,rownum||'mengl' sss from dual connect by level <200;

199 rows inserted

SQL> commit;

Commit complete

SQL>
SQL> begin
2 dbms_stats.gather_table_stats(user,'ML_1',cascade=>true);
3 end;
4
5 /

PL/SQL procedure successfully completed

SQL> explain plan for select * from ml_1 where id=23 and name='123mengl';

Explained

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1572451692
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 11 | 1 (0)| 00:00:0
| 1 | PARTITION RANGE SINGLE| | 1 | 11 | 1 (0)| 00:00:0
|* 2 | INDEX RANGE SCAN | IDX_ML_1 | 1 | 11 | 1 (0)| 00:00:0
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"=23 AND "NAME"='123mengl')

14 rows selected

SQL>

--假如查询中 不带分区键,那么无法使用到 分区消除。

TABLE ACCESS BY LOCAL INDEX ROWID BATCHED但是多了一个12.1的新特性,一次访问多个块(批量访问减少块的次数)

 create index idx_ml_2 on ml_1(name  ) local;

SQL> explain plan for select * from ml_1 where name='123mengl' ;

Explained

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1987278749
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 11 |
| 1 | PARTITION RANGE ALL | | 1 | 11 |
| 2 | TABLE ACCESS BY LOCAL INDEX ROWID BATCHED| ML_1 | 1 | 11 |
|* 3 | INDEX RANGE SCAN | IDX_ML_2 | 1 | |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("NAME"='123mengl')

15 rows selected

SQL>

--假如使用组合索引(前缀),查询不带分区键,那么使用的是skip scan, 不会出现TABLE ACCESS BY LOCAL INDEX ROWID BATCHED
SQL> create index idx_ml_1 on ml_1(id,name ) local;

Index created

SQL> explain plan for select * from ml_1 where name='123mengl' ;

Explained

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 2720485687
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 11 | 2 (0)| 00:00:01 |
| 1 | PARTITION RANGE ALL| | 1 | 11 | 2 (0)| 00:00:01 |
|* 2 | INDEX SKIP SCAN | IDX_ML_1 | 1 | 11 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("NAME"='123mengl')
filter("NAME"='123mengl')

15 rows selected

SQL> select * from v$version where rownum=1 2 ; BANNER CON_ID-------------------------------------------------------------------------------- ----------Oracle
Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production 0 SQL>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: