1. 程式人生 > >三十二、索引導致分割槽表變慢的解析

三十二、索引導致分割槽表變慢的解析

1、索引導致分割槽表變慢

分割槽表上建索引,相當於每個分割槽建了一個小索引。

drop table part_tab purge;
--建立分割槽表,id列分割槽
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)
        )
        ;
--插入11萬資料
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) ;

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 statistics
set linesize 1000
set timing on 

--分割槽表如果有索引,查詢條件中沒有分割槽條件,索引將拖慢查詢
select * from part_tab where col2=8 ;
select * from norm_tab where col2=8 ;

--查詢條件中帶了分割槽條件,索引將提升查詢速度
select * from part_tab where col2=8 and id=2;
select * from norm_tab where col2=8 and id=2;

--檢視索引高度等資訊
select index_name,
          blevel,
          leaf_blocks,
          num_rows,
          distinct_keys,
          clustering_factor
     from user_ind_statistics
    where table_name in( 'NORM_TAB');
    
select index_name,
          blevel,
          leaf_blocks,
          num_rows,
          distinct_keys,
          clustering_factor FROM USER_IND_PARTITIONS where index_name like 'IDX_PAR_TAB%';


select * from part_tab where col3=8 ;

在這裡插入圖片描述

在這裡插入圖片描述