【INDEX】Oracle分区索引技术详解
2021/9/4 19:07:32
本文主要是介绍【INDEX】Oracle分区索引技术详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
环境准备
创建分区表
--创建表结构
CREATE TABLE sf2021
(
sf_id int NOT NULL,
sf_name varchar2(40) NOT NULL,
sf_address VARCHAR2(20) NOT NULL,
insert_time DATE DEFAULT SYSDATE NOT NULL
)
PARTITION BY RANGE (insert_time)
(
PARTITION p202102 VALUES LESS THAN (to_date('202102','yyyymm')),
PARTITION p202103 VALUES LESS THAN (to_date('202103','yyyymm')),
PARTITION p202104 VALUES LESS THAN (to_date('202104','yyyymm')),
PARTITION p202105 VALUES LESS THAN (to_date('202105','yyyymm')),
PARTITION p202106 VALUES LESS THAN (to_date('202106','yyyymm')),
PARTITION p202107 VALUES LESS THAN (to_date('202107','yyyymm')),
PARTITION p202108 VALUES LESS THAN (to_date('202108','yyyymm')),
PARTITION pmax VALUES LESS THAN (maxvalue));
--创建插入程序
create or replace procedure proc_sf2021_insert(tablecount int)
as
begin
for i in 1..tablecount loop
insert into mytest.sf2021
select
trunc(DBMS_RANDOM.VALUE(100000,10000000000)),
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','爱国','中华'),
'山东省'|| DECODE(DBMS_RANDOM.STRING(0, 1),'A','济南市','B','德州市','C','青岛市','D','潍坊市','E','淄博市','F','滨州市','G','临沂市','H','烟台市','L','日照市','M','东营市','N','菏泽市','聊城市'),
to_date('2021/'||trunc(dbms_random.value(1,8))||'/'||trunc(dbms_random.value(1,28)),'yyyy/mm/dd') from dual;
end loop;
commit;
end;
/
--插入一万条数据
exec proc_sf2021_insert(10000);
--查看分区分布
col insert_time for a20
select to_char(insert_time,'yyyy/mm') insert_time,count(*)
from sf2021 group by to_char(insert_time,'yyyy/mm');
本地分区索引
本地分区索引,只适用于分区表分区键
--分区键,创建本地分区索引
create index sf2021_idx_id on sf2021(insert_time) local;
--查看分区状态 --索引及分区类型可参考 dba_part_indexes
SQL> select index_name,PARTITION_NAME,STATUS from DBA_IND_PARTITIONS where index_name='SF2021_IDX_ID';
INDEX_NAME PARTITION_NAME STATUS
------------------------------ ------------------------------ --------
SF2021_IDX_ID P202102 USABLE
SF2021_IDX_ID P202103 USABLE
SF2021_IDX_ID P202104 USABLE
SF2021_IDX_ID P202105 USABLE
SF2021_IDX_ID P202106 USABLE
SF2021_IDX_ID P202107 USABLE
SF2021_IDX_ID P202108 USABLE
SF2021_IDX_ID PMAX USABLE
8 rows selected.
--创建全局hash分区
create index sf2021_idx_hash on sf2021(sf_id) global partition by hash(sf_id) partitions 6;
全局分区索引
全局分区索引 可分为范围和散列.
--创建全局hash分区
create index sf2021_idx_hash on sf2021(sf_id) global partition by hash(sf_id) partitions 6;
--查看
SQL> select index_name,PARTITION_NAME,STATUS from DBA_IND_PARTITIONS where index_name='SF2021_IDX_HASH';
INDEX_NAME PARTITION_NAME STATUS
------------------------------ ------------------------------ --------
SF2021_IDX_HASH SYS_P61 USABLE
SF2021_IDX_HASH SYS_P62 USABLE
SF2021_IDX_HASH SYS_P63 USABLE
SF2021_IDX_HASH SYS_P64 USABLE
SF2021_IDX_HASH SYS_P65 USABLE
SF2021_IDX_HASH SYS_P66 USABLE
6 rows selected.
注意,全局分区索引,在维护分区时,可能造成索引无法使用,需要检查分区索引状态及重建无效索引.
alter index sf2021_idx_hash rebuild partition sys_p66;
前缀分前缀索引
--在本地分区创建唯一索引时,索引必须包括分区列,例如
create unique index sf2021_idx_pk on sf2021(sf_id,insert_time) local;
--当需要定义主键约束时,首先创建一个唯一索引,再添加约束(分步执行,可以单独管理),如
create unique index sf2021_idx_pk on sf2021(sf_id,insert_time) local;
alter table sf2021 add constraint sf2021_idx_pk primary key(sf_id,insert_time);
维护分区表及索引
--添加分区
alter table sf2021 add partition p202109 values less than (to_date('202108','yyyymm')) tablespace users;
--截断分区
alter table sf2021 truncate partition p202101;
--删除分区
alter table sf2021 drop partition p202101;
--移动分区
alter table sf2021 move partition p202106 tablespace mytest;
--拆分分区
alter table sf2021 split partition pmax at('2021/09') into (partition p202109 tablespace mytest,
partition pmax tablespace mytest);
--合并分区
alter table sf2021 merge partitions p202101,pmax into partition pmax;
--查看分区相关索引情况
set lines 200 pages 999
select index_name,null partition_name,status from dba_indexes where table_name='SF2021' and partitioned='NO'
union all
select index_name,partition_name,status from dba_ind_partitions
where index_name in (select index_name from dba_indexes where table_name='SF2021') order by 1,2,3;
表级分区操作对分区索引的影响
这篇关于【INDEX】Oracle分区索引技术详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-10Rakuten 乐天积分系统从 Cassandra 到 TiDB 的选型与实战
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用