本地索引中 前缀索引和非前缀索引的测试 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>
回来测试一下
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>
相关文章推荐
- 在Ubuntu 12.04安装和设置SSH服务
- CentOS6.7安装Oracle 11g2R傻瓜图文教程
- oracle的启动和关闭
- Oracle中的SQL分页查询原理和方法详解
- 部署informatica出现0ra 01280
- oracle 日期格式小结
- Oracle 逐条和批量插入数据方式对比
- Oracle Shared Server 和Oracle Dedicated Server的使用和配置
- Oracle Dedicated server&shared server
- oracle共享与专用模式的动态转换及区别(转载)
- Oracle学习应用中的点点滴滴--自我记录长期更新
- Oracle shared server配置
- 转:ORACLE的JDBC连接方式:OCI和THIN
- 只有oracle中有rownum
- oracle内存结构
- 使用Oracle绿色客户端(InstantClient)连接远程Oracle的配置方法
- Oracle默认的用户名和密码
- Oracle Dedicated server 和 Shared server(专用模式 和 共享模式) 说明
- 从ORACLE RAC角度看跨数据中心的存储双活配置注意事项
- oracle使用exp与imp对数据迁移备份的方法