The COPY_TABLE_STATS procedure copies statistics from a source partition (or subpartition) to a destination partition (or subpartition).
Syntax
DBMS_STATS.COPY_TABLE_STATS (
ownname VARCHAR2,
tabname VARCHAR2,
srcpartname VARCHAR2,
dstpartname VARCHAR2,
scale_factor VARCHAR2,
flags NUMBER DEFAULT NULL,
force BOOLEAN DEFAULT FALSE);
Parameters
| Parameter | Description |
|---|---|
| ownname | The schema name of the table that contains the source and destination partitions (or subpartitions). |
| tabname | The name of the table that corresponds to the source and destination partitions (or subpartitions). |
| srcpartname | The name of the source partition (or subpartition) from which statistics are to be copied. |
| dstpartname | The name of the destination partition (or subpartition) that receives the statistics. |
| scale_factor | The value used to proportionally adjust statistics attributes such as nblks (number of blocks) and nrows (number of rows) when copying to the destination partition (or subpartition). |
| flags | Default value is NULL. General users do not need to set this parameter. |
| force | Default value is FALSE. If this parameter is set to TRUE, statistics copying will proceed even if the destination partition (or subpartition) is currently locked. |
Exceptions
| Error Code | Description |
|---|---|
| HY000 | Indicates an invalid partition name. |
| HY000 | Indicates that the types of the source and destination partitions are different. |
Considerations
This procedure updates the minimum and maximum values in the destination partition as follows:
If the partition type is HASH, the minimum and maximum values of the destination partition are the same as those of the source partition.
If the partition type is LIST, the behavior depends on the destination partition's settings:
If the destination partition is a DEFAULT partition, then:
- The minimum value of the destination partition is set to the minimum value of the source partition.
- The maximum value of the destination partition is set to the maximum value of the source partition.
If the destination partition is a NOT DEFAULT partition, then:
- The minimum value of the destination partition is set to the minimum value in the list of values that describe the destination partition.
- The maximum value of the destination partition is set to the maximum value in the list of values that describe the destination partition.
If the partition type is RANGE, then:
If the destination partition is the first partition, then:
If the upper bound of the destination partition is
MAXVALUE, then:- The destination partition and source partition are the same, so no need to copy or update the minimum and maximum values, which do not need to be set.
If the upper bound of the destination partition is not
MAXVALUE, then:- The minimum and maximum values of the destination partition are both set to the upper bound of the destination partition.
If the destination partition is not the first partition, then:
If the destination partition is a single-column partition key, then:
If the source partition has only one value equal to the lower bound of the source partition, and the upper bound of the destination partition is not
MAXVALUE, then the minimum and maximum values of the destination partition are both set to the lower bound of the destination partition.Otherwise:
- If the upper bound of the destination partition is
MAXVALUE, then the minimum and maximum values are both set to the lower bound of the destination partition (which is the upper bound of the previous partition). - If the upper bound of the destination partition is not
MAXVALUE, then the maximum value is set to the upper bound of the destination partition, and the minimum value is set to the lower bound of the destination partition.
- If the upper bound of the destination partition is
If the destination partition is a multi-column partition key, then:
If the source partition has only one value equal to the lower bound of the source partition, then the minimum and maximum values of the destination partition are both set to the lower bound of the destination partition.
Otherwise, the current column C follows the aforementioned rules, and for subsequent columns Cx, then:
- If Cx-1 in the destination partition is the same as
MAXVALUEin the destination partition and the previous partition, then the maximum value of the destination partition is set to the upper bound of the destination partition. - Otherwise, the maximum value of the destination partition is set to the larger of the upper bound of the destination partition and the maximum value of the source partition for column Cx.
- If Cx-1 in the destination partition is the same as
To call this procedure, you must be the owner of the table. For objects owned by SYS, you must be the owner of the table or have the SYSDBA privilege.
Examples
Create a HASH partitioned table named test_copy_tbl1 and perform related statistics operations.
(Optional) If a table named
test_copy_tbl1exists, execute the following statement to drop it.DROP TABLE IF EXISTS test_copy_tbl1;Execute the following statement to create a table named
test_copy_tbl1with two integer columnscol1andcol2, and partition the table by thecol1column using the HASH function into four partitions:p0,p1,p2, andp3.CREATE TABLE test_copy_tbl1(col1 INT, col2 INT) PARTITION BY HASH(col1) PARTITIONS 4;Execute the following statement to insert data into the
test_copy_tbl1table.INSERT INTO test_copy_tbl1 VALUES (1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,9),(9,10),(10,11);Execute the following statement to call the
gather_table_statsprocedure in theDBMS_STATSpackage to collect statistics for thep0partition of thetest_copy_tbl1table.CALL dbms_stats.gather_table_stats(NULL, 'test_copy_tbl1', 'p0', method_opt=>'for all columns size 32');Execute the following statement to call the
copy_table_statsprocedure in theDBMS_STATSpackage to copy statistics for thep0partition of thetest_copy_tbl1table to thep1partition with a scale factor of 2.CALL dbms_stats.copy_table_stats(NULL, 'test_copy_tbl1', 'p0', 'p1', '2');Alternatively, use named parameters:
CALL dbms_stats.copy_table_stats(NULL, 'test_copy_tbl1', 'p0', 'p1', scale_factor=>'2');
