C/C++教程

【INDEX】Oracle分区索引技术详解

本文主要是介绍【INDEX】Oracle分区索引技术详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

环境准备

创建分区表

  1. --创建表结构
  2. CREATE TABLE sf2021
  3. (
  4. sf_id int NOT NULL,
  5. sf_name varchar2(40) NOT NULL,
  6. sf_address VARCHAR2(20) NOT NULL,
  7. insert_time DATE DEFAULT SYSDATE NOT NULL
  8. )
  9. PARTITION BY RANGE (insert_time)
  10. (
  11. PARTITION p202102 VALUES LESS THAN (to_date('202102','yyyymm')),
  12. PARTITION p202103 VALUES LESS THAN (to_date('202103','yyyymm')),
  13. PARTITION p202104 VALUES LESS THAN (to_date('202104','yyyymm')),
  14. PARTITION p202105 VALUES LESS THAN (to_date('202105','yyyymm')),
  15. PARTITION p202106 VALUES LESS THAN (to_date('202106','yyyymm')),
  16. PARTITION p202107 VALUES LESS THAN (to_date('202107','yyyymm')),
  17. PARTITION p202108 VALUES LESS THAN (to_date('202108','yyyymm')),
  18. PARTITION pmax VALUES LESS THAN (maxvalue));
  19. --创建插入程序
  20. create or replace procedure proc_sf2021_insert(tablecount int)
  21. as
  22. begin
  23. for i in 1..tablecount loop
  24. insert into mytest.sf2021
  25. select
  26. trunc(DBMS_RANDOM.VALUE(100000,10000000000)),
  27. DECODE(DBMS_RANDOM.STRING(0, 1),'A','张','B','周','C','李','D','赵','E','安','F','王小','G','杨','杨') || DECODE(DBMS_RANDOM.STRING(0, 1),'A','娜','B','娟','C','梅','D','美','E','鹏','F','爱国','中华'),
  28. '山东省'|| DECODE(DBMS_RANDOM.STRING(0, 1),'A','济南市','B','德州市','C','青岛市','D','潍坊市','E','淄博市','F','滨州市','G','临沂市','H','烟台市','L','日照市','M','东营市','N','菏泽市','聊城市'),
  29. to_date('2021/'||trunc(dbms_random.value(1,8))||'/'||trunc(dbms_random.value(1,28)),'yyyy/mm/dd') from dual;
  30. end loop;
  31. commit;
  32. end;
  33. /
  34. --插入一万条数据
  35. exec proc_sf2021_insert(10000);
  36. --查看分区分布
  37. col insert_time for a20
  38. select to_char(insert_time,'yyyy/mm') insert_time,count(*)
  39. from sf2021 group by to_char(insert_time,'yyyy/mm');

本地分区索引

本地分区索引,只适用于分区表分区键

  1. --分区键,创建本地分区索引
  2. create index sf2021_idx_id on sf2021(insert_time) local;
  3. --查看分区状态 --索引及分区类型可参考 dba_part_indexes
  4. SQL> select index_name,PARTITION_NAME,STATUS from DBA_IND_PARTITIONS where index_name='SF2021_IDX_ID';
  5. INDEX_NAME PARTITION_NAME STATUS
  6. ------------------------------ ------------------------------ --------
  7. SF2021_IDX_ID P202102 USABLE
  8. SF2021_IDX_ID P202103 USABLE
  9. SF2021_IDX_ID P202104 USABLE
  10. SF2021_IDX_ID P202105 USABLE
  11. SF2021_IDX_ID P202106 USABLE
  12. SF2021_IDX_ID P202107 USABLE
  13. SF2021_IDX_ID P202108 USABLE
  14. SF2021_IDX_ID PMAX USABLE
  15. 8 rows selected.
  16. --创建全局hash分区
  17. create index sf2021_idx_hash on sf2021(sf_id) global partition by hash(sf_id) partitions 6;

全局分区索引

全局分区索引 可分为范围和散列.

  1. --创建全局hash分区
  2. create index sf2021_idx_hash on sf2021(sf_id) global partition by hash(sf_id) partitions 6;
  3. --查看
  4. SQL> select index_name,PARTITION_NAME,STATUS from DBA_IND_PARTITIONS where index_name='SF2021_IDX_HASH';
  5. INDEX_NAME PARTITION_NAME STATUS
  6. ------------------------------ ------------------------------ --------
  7. SF2021_IDX_HASH SYS_P61 USABLE
  8. SF2021_IDX_HASH SYS_P62 USABLE
  9. SF2021_IDX_HASH SYS_P63 USABLE
  10. SF2021_IDX_HASH SYS_P64 USABLE
  11. SF2021_IDX_HASH SYS_P65 USABLE
  12. SF2021_IDX_HASH SYS_P66 USABLE
  13. 6 rows selected.

注意,全局分区索引,在维护分区时,可能造成索引无法使用,需要检查分区索引状态及重建无效索引.

  1. alter index sf2021_idx_hash rebuild partition sys_p66;

前缀分前缀索引

  1. --在本地分区创建唯一索引时,索引必须包括分区列,例如
  2. create unique index sf2021_idx_pk on sf2021(sf_id,insert_time) local;
  3. --当需要定义主键约束时,首先创建一个唯一索引,再添加约束(分步执行,可以单独管理),如
  4. create unique index sf2021_idx_pk on sf2021(sf_id,insert_time) local;
  5. alter table sf2021 add constraint sf2021_idx_pk primary key(sf_id,insert_time);

维护分区表及索引

  1. --添加分区
  2. alter table sf2021 add partition p202109 values less than (to_date('202108','yyyymm')) tablespace users;
  3. --截断分区
  4. alter table sf2021 truncate partition p202101;
  5. --删除分区
  6. alter table sf2021 drop partition p202101;
  7. --移动分区
  8. alter table sf2021 move partition p202106 tablespace mytest;
  9. --拆分分区
  10. alter table sf2021 split partition pmax at('2021/09') into (partition p202109 tablespace mytest,
  11. partition pmax tablespace mytest);
  12. --合并分区
  13. alter table sf2021 merge partitions p202101,pmax into partition pmax;
  14. --查看分区相关索引情况
  15. set lines 200 pages 999
  16. select index_name,null partition_name,status from dba_indexes where table_name='SF2021' and partitioned='NO'
  17. union all
  18. select index_name,partition_name,status from dba_ind_partitions
  19. where index_name in (select index_name from dba_indexes where table_name='SF2021') order by 1,2,3;

表级分区操作对分区索引的影响

这篇关于【INDEX】Oracle分区索引技术详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!