在比较两种索引对select产生的影响之前,先要搞清楚,什么是local prefixed index,什么叫local nonprefixed index。其实,这两种
1、搞清楚两种索引的概念
在比较两种索引对select产生的影响之前,先要搞清楚,什么是local prefixed index,什么叫local nonprefixed index。其实,这两种索引,都是属于分区local索引,所以,这两种类型的索引,只有可能在分区表上才会出现。
1.1 什么是local prefixed index是指索引中的列,就是分区表的分区键列,或者是索引中的列,包含表的分区键值列,并且为前置位
置在索引最前部位置的本地分区索引。
例如,emp表是按时间范围分区的表,分区键列是create_time,,如果分区索引中的列为create_time,
或是以(create_time,emp_no)列的本地复合索引
1.2 什么是local nonprefixed index在理解了什么是local prefixedindex后,再来理解什么是local nonprefixed index就容易了。
是指索引中的列,未包含分区表的分区键列,或者是分区键值列不在前置位置的本地分区索引
例如,emp表是按时间范围分区的表,分区键列是create_time,如果分区索引中的列为不包含create_time列,或者是象(emp_no ,create_time)这种create_time列不在索引前置位置的本地分区索引
2、如何查询索引的类型视图:dba_part_indexes
locality字段:记录是否为local索引
alignment字段:记录是prefixed索引还是non_prefixed索引
3、准备与验证测试环境3.1 创建分区表
create table tivoli.li_db_session_t(
dbname
allsess
activess
timstap date)
timstap)
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
to_date
);
插入4406727行数据,整个表大小为312mb。
3.2 创建五种场景的索引
--local prefixed index类型一:
--local prefixed index类型二:
--local nonprefixed index类型一:
--local nonprefixed index类型二:
--全局索引:
(该索引,由于字段与tivoli.li_idx_04安全一致,所以,无法两个索引并存,需要先删除tivoli.li_idx_04后,才能创建tivoli.li_idx_05索引)
create index tivoli.li_idx_05on tivoli.li_db_session_t(dbname,allsess,timstap,activess);
3.3 对表与索引进行统计分析
begin
dbms_stats.gather_table_statstabnameestimate_percent
end;
3.4 验证所创建索引的类型输出结果如下:
index_name
partitioning_type
subpartitioning_type
locality
alignment
li_idx_01
range
none
local
prefixed
li_idx_02
range
none
local
prefixed
li_idx_03
range
none
local
non_prefixed
li_idx_04
range
none
local
non_prefixed
li_idx_05因为还没有创建所以查询没有结果,实际上,如果li_idx_05不是分区索引,所以,即便该索引建立起来了,在dba_part_indexes视图中也不会出现。
4、五种索引类型下的性能对比以一条select语句为测试语句。
4.1 场景一:local prefixed类型,索引列为表分区键列
sql> set autotrace traceonly
sql> set linesize 999
sql> select /*+ index(t li_idx_01)*/ * from tivoli.li_db_session_t t where t.allsess=28 and t.dbname='costdb' and t.timstap >to_date('2011-01-01','yyyy-mm-dd') and t.timstap
498 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 3409921846
----------------------------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time | pstart| pstop |
----------------------------------------------------------------------------------------------------------------------
| 0 | select statement | | 10 | 200 | 208k (1)| 00:41:38 | | |
| 1 | partition range iterator | | 10 | 200 | 208k (1)| 00:41:38 | 11 | 12 |
|* 2 | table access by local index rowid| li_db_session_t | 10 | 200 | 208k (1)| 00:41:38 | 11 | 12 |