Oracle 12c 新特性 --- 分区表的局部索引

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leo__1990/article/details/90051050

概念

 

Local and global indexes can be created on a subset of the partitions of a table.
Partial indexes provide more flexibility in index creation for partitioned tables. For example, index segments can be omitted for the most recent partitions to ensure maximum data ingest rates without impacting the overall data model and access for the partitioned object.

可以在表的分区的一个子集上创建本地和全局索引。部分索引为分区表的索引创建提供了更多的灵活性。例如,在最近的分区中,索引段可以省略,以确保最大的数据摄取速率,而不影响整个数据模型和对分区对象的访问

A partial index is an index that is correlated with the indexing properties of an associated partitioned table. The correlation enables you to specify which table partitions are indexed.
部分索引是与关联的分区表的索引属性相关的索引。关联使您能够指定哪个表分区被索引。

Partial indexes provide the following advantages:
Table partitions that are not indexed avoid consuming unnecessary index storage space.
没有索引的表分区避免使用不必要的索引存储空间
Performance of loads and queries can improve.
	负载和查询的性能可以提高。
Before Oracle Database 12c, an exchange partition operation required a physical update of an associated global index to retain it as usable. Starting with Oracle Database 12c, if the partitions involved in a partition maintenance operation are not part of a partial global index, then the index remains usable without requiring any global index maintenance.
在Oracle数据库12c之前,交换分区操作需要一个相关的全局索引的物理更新以保持其可用性。从Oracle Database 12c开始,如果分区维护操作中涉及的分区不是部分全局索引的一部分,那么该索引在不需要任何全局索引维护的情况下仍然可用。
If you index only some table partitions at index creation, and if you later index other partitions, then you can reduce the sort space required by index creation.
如果只在索引创建时索引一些表分区,如果您稍后索引其他分区,则可以减少索引创建所需的排序空间。
You can turn indexing on or off for the individual partitions of a table. A partial local index does not have usable index partitions for all table partitions that have indexing turned off. A global index, whether partitioned or not, excludes the data from all partitions that have indexing turned off. The database does not support partial indexes for indexes that enforce unique constraints.
您可以为表的各个分区打开或关闭索引。局部本地索引对于索引关闭的所有表分区没有可用的索引分区。全局索引(无论分区与否)排除了所有索引关闭的分区的数据。数据库不支持执行唯一约束的索引的部分索引。

实验

关于哪个分区被索引的决定是使用与每个分区关联的 INDEXING [ON | OFF]子句来决定的,默认的是索引。
1) 创建并插入分区表。
[[email protected] ~]$ sqlplus "test/test@pdbcndba"

SQL*Plus: Release 12.1.0.2.0 Production on Wed Aug 9 22:52:07 2017

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Last Successful login time: Wed Aug 09 2017 15:17:09 +08:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> DROP TABLE t1 PURGE;

Table dropped.

SQL> CREATE TABLE t1
(id            NUMBER,
 description   VARCHAR2(50),
 created_date  DATE)
PARTITION BY RANGE (created_date)
(PARTITION part_2014 VALUES LESS THAN (TO_DATE('01/01/2015', 'DD/MM/YYYY')),
 PARTITION part_2015 VALUES LESS THAN (TO_DATE('01/01/2016', 'DD/MM/YYYY')) INDEXING ON,
 PARTITION part_2016 VALUES LESS THAN (TO_DATE('01/01/2017', 'DD/MM/YYYY')) INDEXING OFF);

Table created.

SQL> INSERT INTO t1 VALUES (1, 't1 ONE', TO_DATE('01/07/2014', 'DD/MM/YYYY'));

1 row created.

SQL> INSERT INTO t1 VALUES (2, 't1 TWO', TO_DATE('01/07/2015', 'DD/MM/YYYY'));

1 row created.

SQL> INSERT INTO t1 VALUES (3, 't1 THREE', TO_DATE('01/07/2016', 'DD/MM/YYYY'));

1 row created.

SQL> commit;

Commit complete.
2)检查每个分区的索引状态
SQL> COLUMN table_name FORMAT A20
SQL> COLUMN partition_name FORMAT A20
SQL> SELECT table_name,
       partition_name,
       indexing
FROM   user_tab_partitions
ORDER BY 1,2;

TABLE_NAME	     PARTITION_NAME	  INDE
-------------------- -------------------- ----
T1		     PART_2014		  ON
T1		     PART_2015		  ON
T1		     PART_2016		  OFF
3)可以使用ALTER TABLE修改分区是否被可以被索引
SQL> ALTER TABLE t1 MODIFY PARTITION part_2014 INDEXING OFF;

Table altered.

SQL> SELECT table_name,
       partition_name,
       indexing
FROM   user_tab_partitions
ORDER BY 1,2;  2    3    4    5  

TABLE_NAME	     PARTITION_NAME	  INDE
-------------------- -------------------- ----
T1		     PART_2014		  OFF
T1		     PART_2015		  ON
T1		     PART_2016		  OFF

SQL> ALTER TABLE t1 MODIFY PARTITION part_2014 INDEXING ON;

Table altered.

SQL> SELECT table_name,
       partition_name,
       indexing
FROM   user_tab_partitions
ORDER BY 1,2;

TABLE_NAME	     PARTITION_NAME	  INDE
-------------------- -------------------- ----
T1		     PART_2014		  ON
T1		     PART_2015		  ON
T1		     PART_2016		  OFF


即使使用分区索引设置,默认的索引也会被创建为 INDEXING FULL,所以分区设置被忽略。
1)创建本地分区索引默认 INDEXING 默认FULL
SQL> DROP INDEX t1_local_partial_idx;
DROP INDEX t1_local_partial_idx
           *
ERROR at line 1:
ORA-01418: specified index does not exist

SQL> CREATE INDEX t1_local_partial_idx ON t1(created_date) LOCAL;

Index created.
2) 检查索引分区状态
SQL> COLUMN index_name FORMAT A25
SQL> SELECT index_name,
       partition_name,
       status
FROM   user_ind_partitions
ORDER BY 1,2;  2    3    4    5  

INDEX_NAME		  PARTITION_NAME       STATUS
------------------------- -------------------- --------
T1_LOCAL_PARTIAL_IDX	  PART_2014	       USABLE
T1_LOCAL_PARTIAL_IDX	  PART_2015	       USABLE
T1_LOCAL_PARTIAL_IDX	  PART_2016	       USABLE
3) 检查索引状态
SQL> SELECT index_name,
       indexing
FROM   user_indexes
ORDER BY 1;  2    3    4  

INDEX_NAME		  INDEXIN
------------------------- -------
T1_LOCAL_PARTIAL_IDX	  FULL
添加索引部分子句可以将索引创建为部分索引。非索引的分区被标记为不可用。
1) 创建局部分区索引
SQL> DROP INDEX t1_local_partial_idx;

Index dropped.

SQL> CREATE INDEX t1_local_partial_idx ON t1(created_date) LOCAL INDEXING PARTIAL;

Index created.
2)检查索引分区的状态
SQL> SELECT index_name,
       partition_name,
       status
FROM   user_ind_partitions
ORDER BY 1,2;  2    3    4    5  

INDEX_NAME		  PARTITION_NAME       STATUS
------------------------- -------------------- --------
T1_LOCAL_PARTIAL_IDX	  PART_2014	       USABLE
T1_LOCAL_PARTIAL_IDX	  PART_2015	       USABLE
T1_LOCAL_PARTIAL_IDX	  PART_2016	       UNUSABLE

SQL> SELECT index_name,
       indexing
FROM   user_indexes
ORDER BY 1;  2    3    4  
3)检查索引状态
INDEX_NAME		  INDEXIN
------------------------- -------
T1_LOCAL_PARTIAL_IDX	  PARTIAL
还可以创建全局索引作为部分索引,只包含索引中包含的标记分区。
SQL> CREATE INDEX t1_global_partial_idx ON t1(description) GLOBAL INDEXING PARTIAL;

Index created.

SQL> SELECT index_name,
       indexing
FROM   user_indexes
ORDER BY 1; 

INDEX_NAME		  INDEXIN
------------------------- -------
T1_GLOBAL_PARTIAL_IDX	  PARTIAL
T1_LOCAL_PARTIAL_IDX	  PARTIAL

参考文档 

http://docs.oracle.com/database/121/NEWFT/chapter12101.htm#NEWFT205

http://docs.oracle.com/database/121/VLDBG/GUID-569F94D0-E6E5-45BB-9626-5506DE18FF00.htm#VLDBG00403

https://oracle-base.com/articles/12c/partial-indexes-for-partitioned-tables-12cr1

http://docs.oracle.com/database/121/CNCPT/schemaob.htm#CNCPT89298

猜你喜欢

转载自blog.csdn.net/leo__1990/article/details/90051050
今日推荐