5.2.1.5妙用三特征之高低较低 drop table t1 purge; drop table t2 purge; drop table t3 purge;
drop table t4 purge; drop table t5 purge; drop table t6 purge; drop table t7 purge; create table t1 as select rownum as id,rownum+1 as id2 from dual connect by level<=5; create table t2 as select rownum as id,rownum+1 as id2 from dual connect by level<=50; create table t3 as select rownum as id,rownum+1 as id2 from dual connect by level<=500; create table t4 as select rownum as id,rownum+1 as id2 from dual connect by level<=5000; create table t5 as select rownum as id,rownum+1 as id2 from dual connect by level<=50000; create table t6 as select rownum as id,rownum+1 as id2 from dual connect by level<=500000; create table t7 as select rownum as id,rownum+1 as id2 from dual connect by level<=5000000;
create index idx_id_t1 on t1(id); create index idx_id_t2 on t2(id); create index idx_id_t3 on t3(id); create index idx_id_t4 on t4(id); create index idx_id_t5 on t5(id); create index idx_id_t6 on t6(id); create index idx_id_t7 on t7(id);
观察比较各个索引的大小: select segment_name,bytes/1024 from user_segments where segment_name in ('IDX_ID_T1','IDX_ID_T2','IDX_ID_T3','IDX_ID_T4','IDX_ID_T5','IDX_ID_T6','IDX_ID_T7');
观察比较各个索引的高度: select index_name,blevel,leaf_blocks,num_rows,distinct_keys,clustering_factor from user_ind_statistics where table_name in ('T1','T2','T3','T4','T5','T6','T7');
由于一个区含有8个块,一个块8KB,即便是一个空表,大小也是64KB。
如果删除了索引,再执行查看查询性能。
5.2.1.5.1分区索引设计误区 建立分区表part_tab ,插入数据,并分别在col2上建局部索引,在col3上建立全局索引: drop table part_tab purge; create table part_tab (id int,col2 int,col3 int) partition by range(id) ( partition p1 values less than (10000), partition p2 values less than (20000), partition p3 values less than (30000), partition p4 values less than (40000), partition p5 values less than (50000), partition p6 values less than (60000), partition p7 values less than (70000), partition p8 values less than (80000), partition p9 values less than (90000), partition p10 values less than (100000), partition p11 values less than (maxvalue) ) ; insert into part_tab select rownum,rownum+1,rownum+2 from dual connect by rownum<=110000; commit; create index idx_par_tab_col2 on part_tab(col2) local; create index idx_par_tab_col3 on part_tab(col3);
查看分区表各个分区 及其类型: col segment_name format a20 select segment_name,partition_name,segment_type from user_segments where segment_name='PART_TAB'; select segment_name,partition_name,segment_type from user_segments where segment_name='IDX_PAR_TAB_COL2'; select segment_name,partition_name,segment_type from user_segments where segment_name='IDX_PAR_TAB_COL3';
建立一个普通表norm_tab,与part_tab类似: drop table norm_tab purge; create table norm_tab(id int,col2 int,col3 int); insert into norm_tab select rownum,rownum+1,rownum+2 from dual connect by rownum<=110000; commit; create index idx_nor_tab_col2 on norm_tab(col2); create index idx_nor_tab_col3 on norm_tab(col3);
查询性能分析: set autotrace traceonly set linesize 1000 set timing on select * from part_tab where col2=8; select * from norm_tab where col3=8; 针对分区表查询逻辑读为普通表的6倍,cost也为6倍。 分区表索引IO个数要乘以分区个数,分区查询,导致查询变慢。
分别观察分区表和普通表的索引高度: select index_name, blevel, leaf_blocks, num_rows, distinct_keys, clustering_factor from user_ind_partitions where index_name='IDX_PAR_TAB_COL2'; select index_name, blevel, leaf_blocks, num_rows, distinct_keys, clustering_factor from user_ind_statistics where index_name='IDX_PAR_TAB_COL2';
分区索引扫描落在某一个分区,性能大幅提升: select * from part_tab where col2=8 and id=7; 这里pstart 与pstop 只遍历一个分区。
[此贴子已经被作者于2015-06-28 01:06:17编辑过] |