如何在Oracle 12C中Drop/Truncate多个分区 (Doc ID 1482264.1)
How to Drop/Truncate Multiple Partitions in Oracle 12C (Doc ID 1482264.1)
APPLIES TO:
Oracle Database - Enterprise Edition - Version 12.1.0.1 and later
Oracle Database Cloud Schema Service - Version N/A and later
Oracle Database Exadata Cloud Machine - Version N/A and later
Oracle Cloud Infrastructure - Database Service - Version N/A and later
Oracle Database Cloud Exadata Service - Version N/A and later
Information in this document applies to any platform.
Oracle 12C
GOAL
To be able to drop or truncate multiple partitions or subpartitions with a single statement 为了能够使用单个语句删除或截断多个分区或子分区
SOLUTION
You can use multi-partition maintenance operations which enable dropping multiple partitions and truncating multiple partitions using a single SQL data definition language (DDL) statement.
您可以使用多分区维护操作,这些操作允许使用单个SQL数据定义语言(DDL)语句删除多个分区并截断多个分区
table
, then you can drop/truncate only one partition at a time. 如果在上定义了 domain index table
,则一次只能删除/截断一个分区。Rows inside the partition(s) being dropped are eliminated, so if you want to keep the rows you must use the MERGE PARTITIONS instead. 删除了要删除的分区内的行,因此,如果要保留行,则必须使用MERGE PARTITIONS代替。
In addition, Global index maintenance can now be delayed and decoupled using DROP and TRUNCATE partition without making a global index unusable.
此外,现在可以使用DROP和TRUNCATE分区来延迟和处理全局索引维护,而不会使全局索引不可用。
This new feature provides faster DROP and TRUNCATE partition operations. Index maintenance can be delayed to off-peak time.
这项新功能提供了更快的DROP和TRUNCATE分区操作。索引维护可以延迟到非高峰时间。
Dropping Partitions 删除分区
You can remove multiple partitions or subpartitions from a range or list partitioned table with the DROP PARTITIONS and DROP SUBPARTITIONS clauses of the ALTER TABLE statement. 您可以使用 ALTER TABLE 语句的 DROP PARTITIONS 和 DROP SUBPARTITIONS 子句从范围或列表分区表中删除多个分区或子分区。
For example, the example below drops multiple partitions from the Range-partitioned table, XYZ. 例如,下面的示例从范围分区表XYZ中删除多个分区。
CREATE TABLE XYZ ( prod_id NUMBER(6) , cust_id NUMBER , time_id DATE , quantity_sold NUMBER(3) , amount_sold NUMBER(10,2) ) PARTITION BY RANGE (time_id) (PARTITION SALES_Q2_2011 VALUES LESS THAN (TO_DATE('01-APR-2011','DD-MON-YYYY')), PARTITION SALES_Q3_2011 VALUES LESS THAN (TO_DATE('01-OCT-2011','DD-MON-YYYY')), PARTITION SALES_Q4_2011 VALUES LESS THAN (TO_DATE('01-JAN-2012','DD-MON-YYYY')), PARTITION SALES_Q1_2012 VALUES LESS THAN (TO_DATE('01-APR-2012','DD-MON-YYYY')), PARTITION SALES_Q2_2012 VALUES LESS THAN (TO_DATE('01-JUL-2012','DD-MON-YYYY')), PARTITION SALES_Q3_2012 VALUES LESS THAN (TO_DATE('01-OCT-2012','DD-MON-YYYY')), PARTITION SALES_Q4_2012 VALUES LESS THAN (MAXVALUE)); ALTER TABLE XYZ DROP PARTITIONS SALES_Q2_2011, SALES_Q3_2011, SALES_Q4_2011;
Note: You cannot drop all the partitions of a table. If table contains only one partition, then you cannot drop the partition. You must drop the table. 注意:您不能删除表的所有分区。如果表仅包含一个分区,则无法删除该分区。您必须drop table。
When you drop multiple partitions, local and global index operations are the same as when you drop a single partition. That is the corresponding partitions of local indexes are also dropped in the operation, and the Global indexes must be rebuilt unless the UPDATE INDEXES or UPDATE GLOBAL INDEXES clause is specified.
删除多个分区时,本地索引和全局索引操作与删除单个分区时相同。 也就是说,在操作中也将删除本地索引的相应分区,并且除非指定了UPDATE INDEXES或UPDATE GLOBAL INDEXES子句,否则必须重建全局索引。
Truncating Partitions 截断分区
Use the ALTER TABLE ... TRUNCATE PARTITION statement to remove all rows from a table partition. The ALTER TABLE ... TRUNCATE PARTITIONS statement does this for multiple partitions. 使用 ALTER TABLE ... TRUNCATE PARTITION 语句从表分区中删除所有行。ALTER TABLE ... TRUNCATE PARTITIONS语句对多个分区执行此操作。
Truncating a partition is similar to dropping a partition, except that the partition is emptied of its data, but not physically dropped. 截断分区类似于删除分区,不同之处在于该分区清空了其数据,但并未物理删除
Using the same example as before: 使用与之前相同的示例
CREATE TABLE XYZ ( prod_id NUMBER(6) , cust_id NUMBER , time_id DATE , quantity_sold NUMBER(3) , amount_sold NUMBER(10,2) ) PARTITION BY RANGE (time_id) (PARTITION SALES_Q2_2011 VALUES LESS THAN (TO_DATE('01-APR-2011','DD-MON-YYYY')), PARTITION SALES_Q3_2011 VALUES LESS THAN (TO_DATE('01-OCT-2011','DD-MON-YYYY')), PARTITION SALES_Q4_2011 VALUES LESS THAN (TO_DATE('01-JAN-2012','DD-MON-YYYY')), PARTITION SALES_Q1_2012 VALUES LESS THAN (TO_DATE('01-APR-2012','DD-MON-YYYY')), PARTITION SALES_Q2_2012 VALUES LESS THAN (TO_DATE('01-JUL-2012','DD-MON-YYYY')), PARTITION SALES_Q3_2012 VALUES LESS THAN (TO_DATE('01-OCT-2012','DD-MON-YYYY')), PARTITION SALES_Q4_2012 VALUES LESS THAN (MAXVALUE)); ALTER TABLE XYZ TRUNCATE PARTITIONS SALES_Q2_2011, SALES_Q3_2011, SALES_Q4_2011;
The corresponding partitions of local indexes are truncated in the operation. 本地索引的相应分区在操作中被截断。
Global indexes must be rebuilt unless the UPDATE INDEXES or UPDATE GLOBAL INDEXES clause is specified. 除非指定了UPDATE INDEXES或UPDATE GLOBAL INDEXES子句,否则必须重建全局索引。
Index maintenance 索引维护
Asynchronous global index maintenance for DROP
and TRUNCATE
is performed by default; however, the UPDATE
INDEXES
clause is still required for backward compatibility.
默认情况下,对DROP
和TRUNCATE
进行异步全局索引维护;但是,该UPDATE
INDEXES
子句对于向后兼容仍然是必需的。
Global index maintenance is be decoupled from the DROP and TRUNCATE partition maintenance operation without rendering a global index unusable. Index maintenance is done asynchronously and can be delayed to a later point-in-time. Delaying the global index maintenance to off-peak times without impacting the index availability makes DROP and TRUNCATE partition and subpartition maintenance operations faster and less resource intensive at the point-in-time of the partition maintenance operation.
全局索引维护与DROP和TRUNCATE分区维护操作分离,而不会导致全局索引不可用。 索引维护是异步完成的,并且可以延迟到稍后的时间点。 将全局索引维护延迟到非高峰时间而不影响索引可用性,可以在分区维护操作的时间点上更快地进行DROP和TRUNCATE分区以及子分区维护操作,并减少资源消耗。
The partition maintenance operations DROP PARTITION and TRUNCATE PARTITION that support the updating of global indexes are optimized by making the index maintenance for metadata only. This functionality enables maintenance of a list of data object numbers in metadata, where index entries corresponding to the drop and truncated objects that are invalid are ignored.
通过仅对元数据进行索引维护,可以优化支持全局索引更新的分区维护操作DROP PARTITION和TRUNCATE PARTITION。 通过此功能,可以维护元数据中的数据对象编号列表,其中将忽略与无效的丢弃对象和截断对象相对应的索引条目。
Maintenance operations on indexes can be performed with the automatic scheduler job SYS.PMO_DEFERRED_GIDX_MAINT_JOB to clean up all global indexes. This job is scheduled to run at 2:00 A.M. on a daily basis by default. You can run this job at any time using DBMS_SCHEDULER.RUN_JOB if you want to proactively clean up the indexes. You can also modify the job to run with a different schedule based on your specific requirements. However, Oracle recommends that you do not drop the job.
可以使用自动调度程序作业SYS.PMO_DEFERRED_GIDX_MAINT_JOB对索引执行维护操作,以清理所有全局索引。 该作业计划于凌晨2:00运行。 默认情况下每天。 如果要主动清理索引,可以随时使用DBMS_SCHEDULER.RUN_JOB运行此作业。 您还可以根据您的特定要求修改作业,使其以不同的时间表运行。 但是,Oracle建议您不要放弃该作业。
The DBMS_PART package provides an interface for maintenance and management operations on partitioned objects. As a consequence of prior partition maintenance operations with asynchronous global index maintenance, global indexes can contain entries pointing to data segments that no longer exist. These stale index rows will not cause any correctness issues or corruptions during any operation on the table or index, whether these are queries, DMLs, DDLs or analyze.
DBMS_PART包提供了一个接口,用于对分区对象进行维护和管理。 由于先前的分区维护操作具有异步全局索引维护,因此全局索引可以包含指向不再存在的数据段的条目。 这些陈旧的索引行在对表或索引的任何操作期间都不会引起任何正确性问题或损坏,无论它们是查询,DML,DDL还是分析。
The DBMS_PART.CLEANUP_GIDX procedure will identify and cleanup these global indexes to ensure efficiency in terms of storage and performance.
DBMS_PART.CLEANUP_GIDX过程将识别并清除这些全局索引,以确保存储和性能方面的效率。
Partial Global Index Optimization 局部全局索引优化
The column ORPHANED_ENTRIES is added to the dictionary views USER_INDEXES and USER_IND_PARTITIONS. This column specifies whether or not a global index (partition) contains stale entries due to deferred index maintenance during DROP/TRUNCATE PARTITION, or MODIFY PARTITION operations.
列ORPHANED_ENTRIES已添加到词典视图USER_INDEXES和USER_IND_PARTITIONS。 此列指定由于在DROP / TRUNCATE PARTITION或MODIFY PARTITION操作期间推迟进行索引维护,全局索引(分区)是否包含过时的条目。
The column can have one of three values: 该列可以具有以下三个值之一
• YES: The index (partition) contains orphaned entries
• NO: The index (partition) does not contain any orphaned entries
• N/A: The property is not applicable – this is the case for local indexes, or indexes
on non-partitioned tables.
You can also force a cleanup of an index needing maintenance using one of the following options: 您还可以使用以下选项之一强制清除需要维护的索引:
- DBMS_PART.CLEANUP_GIDX: This PL/SQL package procedure gathers the list of global indexes in the system that may require cleanup and runs the operations
necessary to restore the indexes to a clean state.
- ALTER INDEX REBUILD [PARTITION]: This SQL statement rebuilds the entire
index or index partition as was done prior to Oracle Database 12.1 releases; the resulting index (partition) does not contain any stale entries.
- ALTER INDEX COALESCE [PARTITION] CLEANUP: This SQL statement cleans up any orphaned entries in index blocks.
REFERENCES
BUG:26332943 - ASYNCHRONOUS GLOBAL INDEX MAINTENANCE DOES NOT WORK AFTER UPGRADE