分区表优点:
可以提高可用性、管理性、性能和扩展型。比如当表的数据量很大时,可以通过分区来对表进行分而治之。当某个表有热块时,可以通过分区打散热点。当需要进行数据生命周期管理时,可以使用分区表的特性switch和truncate分区表。
那么分区表的索引设计有哪些注意点呢?
首先看看分区表有哪些索引:
本地分区索引 -
一个索引分区对应一个表分区。一个本地分区索引可以使唯一的,但是表的分区键必须是索引字段的一部分。这种索引通常在dss类型数据库中使用。
全局分区索引 -
在全局分区索引中,一个索引分区和表分区不是一一对应。索引的分区是按自己的方式进行。如果对表分区进行ddl操作,会导致全局分区索引失效。必须将上update indexes子句。
全局非分区索引 -
和普通索引一样。
本地分区索引的优点是方便管理,在进行分区表维护操作时,不需要对索引进行额外的维护操作。缺点是查询效率要比全局索引慢。
全局索引的优点是查询效率高,但是在分区维护时,需要对索引进行额外的维护操作。
下面是测试案例:
可维护性:
--创建生产表
-
CREATE TABLE p1
-
(
-
p1_id number,
-
p1_name varchar2(20),
-
create_date date
-
)
-
PARTITION BY RANGE (create_date)
-
INTERVAL(NUMTOYMINTERVAL(1, 'MONTH'))
-
(
-
PARTITION P1 VALUES LESS THAN(TO_DATE('2016-12-01', 'YYYY-MM-DD'))
-
)
-
;
--添加本地索引,并将其设置为主键
-
create unique index p1_pk on p1(p1_id,create_date) local;
-
alter table p1 add primary key (p1_id,create_date) using index p1_pk;
-
-
DECLARE
-
i NUMBER;
-
BEGIN
-
FOR i IN 1..1000 LOOP
-
INSERT INTO P1 VALUES (i, to_char(round(dbms_random.value*100000000,0)), TO_DATE('2016-12-01','yyyy-mm-hh')+MOD(i,30));
-
END LOOP;
-
-
FOR i IN 1001..2000 LOOP
-
INSERT INTO P1 VALUES (i, to_char(round(dbms_random.value*100000000,0)), TO_DATE('2017-01-01','yyyy-mm-hh')+MOD(i,30));
-
END LOOP;
-
-
FOR i IN 2001..3000 LOOP
-
INSERT INTO P1 VALUES (i, to_char(round(dbms_random.value*100000000,0)), TO_DATE('2017-02-01','yyyy-mm-hh')+MOD(i,30));
-
END LOOP;
-
END;
-
/
--创建中间的交换表
-
CREATE TABLE p1_ex
-
(
-
p1_id number,
-
p1_name varchar2(20),
-
create_date date
-
)
-
;
-
-
create unique index p1_ex_pk on p1_ex(p1_id,create_date) ;
-
alter table p1_ex add primary key (p1_id,create_date) using index
-
p1_ex_pk;
--创建历史表
-
CREATE TABLE p1_his
-
(
-
p1_id number,
-
p1_name varchar2(20),
-
create_date date
-
)
-
PARTITION BY RANGE (create_date)
-
INTERVAL(NUMTOYMINTERVAL(1, 'MONTH'))
-
(
-
PARTITION P1 VALUES LESS THAN(TO_DATE('2016-12-01', 'YYYY-MM-DD'))
-
)
-
;
-
-
create unique index p1_his_pk on p1_his(p1_id,create_date)
-
local;
-
alter table p1_his add primary key (p1_id,create_date) using index
-
p1_his_pk;
--在历史表添加一条记录用来自动扩展分区
-
DECLARE
-
i NUMBER;
-
BEGIN
-
FOR i IN 1000001..1000001 LOOP
-
INSERT INTO P1_HIS VALUES (i, to_char(round(dbms_random.value*100000000,0)), TO_DATE('2016-12-01','yyyy-mm-hh')+MOD(i,28));
-
END LOOP;
-
DELETE FROM P1_HIS WHERE P1_ID = i;
-
COMMIT;
-
END;
-
/
--进行表分区交换
--查到需要交换的分区
-
select * from dba_tab_partitions where table_name = 'P1';
--将2016年12月份数据通过交换分区交换到临时表。
-
alter table p1 exchange partition SYS_P1341 with table p1_ex INCLUDING
-
INDEXES;
--P1表的分区SYS_P1341数据为0
-
select * from p1 partition(SYS_P1341)
--查看P1表的分区索引,状态时正常的。
-
select * from dba_ind_partitions where index_name = 'P1_PK'
--P1_EX表有1000条数据。
-
select count(*) from p1_ex;
--将临时表数据通过交换分区交换到历史表。
-
alter table p1_his exchange partition SYS_P1345 with table p1_ex INCLUDING
-
INDEXES WITHOUT VALIDATION ;
--P1_EX表有0条数据。
-
select count(*) from p1_ex;
--历史表的分区SYS_P1345数据为1000条
-
select count(*) from p1 partition(SYS_P1345);
--查看历史表的分区索引,状态时正常的。
-
select * from dba_ind_partitions where index_name = 'P1_HIS_PK'
性能对比:
-
select * from pttest where ptest_id = 20;
--创建生产表
-
CREATE TABLE p2
-
(
-
p2_id number,
-
p2_name varchar2(20),
-
create_date date
-
)
-
PARTITION BY RANGE (create_date)
-
INTERVAL(NUMTOYMINTERVAL(1, 'MONTH'))
-
(
-
PARTITION p2 VALUES LESS THAN(TO_DATE('2016-12-01', 'YYYY-MM-DD'))
-
)
-
;
--添加本地索引,并将其设置为主键
-
create unique index p2_pk on p2(p2_id) ;
-
alter table p2 add primary key (p2_id) using index p2_pk;
-
DECLARE
-
i NUMBER;
-
BEGIN
-
FOR i IN 1..1000 LOOP
-
INSERT INTO p2 VALUES (i, to_char(round(dbms_random.value*100000000,0)), TO_DATE('2016-12-01','yyyy-mm-hh')+MOD(i,30));
-
END LOOP;
-
-
FOR i IN 1001..2000 LOOP
-
INSERT INTO p2 VALUES (i, to_char(round(dbms_random.value*100000000,0)), TO_DATE('2017-01-01','yyyy-mm-hh')+MOD(i,30));
-
END LOOP;
-
-
FOR i IN 2001..3000 LOOP
-
INSERT INTO p2 VALUES (i, to_char(round(dbms_random.value*100000000,0)), TO_DATE('2017-02-01','yyyy-mm-hh')+MOD(i,30));
-
END LOOP;
-
END;
-
/
--创建中间的交换表
-
CREATE TABLE p2_ex
-
(
-
p2_id number,
-
p2_name varchar2(20),
-
create_date date
-
)
-
;
-
-
create unique index p2_ex_pk on p2_ex(p2_id) ;
-
alter table p2_ex add primary key (p2_id) using index
-
p2_ex_pk;
--进行表分区交换
--查到需要交换的分区
-
select * from dba_tab_partitions where table_name = 'P2';
--将2016年12月份数据通过交换分区交换到临时表。
-
alter table p2 exchange partition SYS_P1346 with table p2_ex INCLUDING
-
INDEXES;
--报错
--ORA-14098: ALTER TABLE EXCHANGE PARTITION 中的表索引不匹配
--不使用include indexes
-
alter table p2 exchange partition SYS_P1346 with table p2_ex ;
--p2表的分区SYS_p2341数据为0
-
select * from p2 partition(SYS_P1346)
--查看p2表的索引,状态是UNUSABLE的。
-
select index_name,status from dba_indexes where index_name = 'P2_PK';
-
-
INDEX_NAME STATUS
-
------------------------------ --------
-
P2_PK UNUSABLE
--p2_EX表有1000条数据。
-
select count(*) from p2_ex;
--将临时表重新通过交换分区交换到原来的分区表。
-
alter table p2 exchange partition SYS_P1346 with table p2_ex ;
--重新rebuild index
-
alter table p2 rebuild index p2_pk;
-
11:24:01 AM> alter index p2_pk rebuild;
-
Index altered
-
Executed in 0.078 seconds
-
11:24:08 AM> select index_name,status from dba_indexes where index_name = 'P2_PK';
-
INDEX_NAME STATUS
-
------------------------------ --------
-
P2_PK VALID
--再次使用update indexes交换分区。
-
alter table p2 exchange partition SYS_P1346 with table p2_ex update indexes;
--查看p2表的索引,状态是正常的。
-
11:24:56 AM> select index_name,status from dba_indexes where index_name = 'P2_PK';
-
INDEX_NAME STATUS
-
------------------------------ --------
-
P2_PK VALID
如果分区表的索引不是本地索引,必须使用update indexes来更新索引。但是当数据量很大时,这会导致维护时间很长,生成的undo很大,从而导致生产库问题。
性能对比:
-
select * from p1 where p1_id = 20;
-
SQL> select * from p1 where p1_id = 2002;
-
-
执行计划
-
----------------------------------------------------------
-
Plan hash value: 3533025020
-
------------------------------------------------------------------------------------------------------------
-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
-
------------------------------------------------------------------------------------------------------------
-
| 0 | SELECT STATEMENT | | 1 | 34 | 1 (0)| 00:00:01 | | |
-
| 1 | PARTITION RANGE ALL | | 1 | 34 | 1 (0)| 00:00:01 | 1 |1048575|
-
| 2 | TABLE ACCESS BY LOCAL INDEX ROWID| P1 | 1 | 34 | 1 (0)| 00:00:01 | 1 |1048575|
-
|* 3 | INDEX RANGE SCAN | P1_PK | 1 | | 1 (0)| 00:00:01 | 1 |1048575|
-
------------------------------------------------------------------------------------------------------------
-
Predicate Information (identified by operation id):
-
---------------------------------------------------
-
3 - access("P1_ID"=2002)
-
Note
-
-----
-
- dynamic sampling used for this statement (level=2)
-
-
统计信息
-
----------------------------------------------------------
-
0 recursive calls
-
0 db block gets
-
9 consistent gets
-
0 physical reads
-
0 redo size
-
487 bytes sent via SQL*Net to client
-
476 bytes received via SQL*Net from client
-
2 SQL*Net roundtrips to/from client
-
0 sorts (memory)
-
0 sorts (disk)
-
1 rows processed
-
-
select * from p2 where p2_id = 20;
-
SQL> select * from p2 where p2_id = 2002;
-
-
执行计划
-
----------------------------------------------------------
-
Plan hash value: 3582785584
-
------------------------------------------------------------------------------------------------------------
-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
-
------------------------------------------------------------------------------------------------------------
-
| 0 | SELECT STATEMENT | | 1 | 34 | 2 (0)| 00:00:01 | | |
-
| 1 | TABLE ACCESS BY GLOBAL INDEX ROWID| P2 | 1 | 34 | 2 (0)| 00:00:01 | ROWID | ROWID |
-
|* 2 | INDEX UNIQUE SCAN | P2_PK | 1 | | 1 (0)| 00:00:01 | | |
-
------------------------------------------------------------------------------------------------------------
-
Predicate Information (identified by operation id):
-
---------------------------------------------------
-
2 - access("P2_ID"=2002)
-
-
统计信息
-
----------------------------------------------------------
-
0 recursive calls
-
0 db block gets
-
3 consistent gets
-
0 physical reads
-
0 redo size
-
487 bytes sent via SQL*Net to client
-
476 bytes received via SQL*Net from client
-
2 SQL*Net roundtrips to/from client
-
0 sorts (memory)
-
0 sorts (disk)
-
1 rows processed
从上面的例子看到,当使用本地分区索引时,consistent read 是9,而使用全局索引时,consistent read只有2个。效率下降了。
结论:
本地分区索引方便管理,在进行分区表维护操作时,不需要对索引进行额外的维护操作,从而降低维护带来的生产库问题。缺点是查询效率要比全局索引慢。
全局索引查询效率高,但是在分区维护时,需要对索引进行额外的维护操作。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25105315/viewspace-2131989/
,如需转载,请注明出处,否则将追究法律责任。