The COPY_TABLE_STATS procedure copies the statistics from the source partition (or subpartition) to the destination partition (or subpartition).
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only the MySQL mode.
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 or username of the table to which the source and destination partitions (or subpartitions) belong. |
| tabname | The name of the table to which the source and destination partitions (or subpartitions) belong. |
| srcpartname | The source partition (or subpartition) whose statistics are to be copied. |
| dtspartname | The destination partition (or subpartition) that receives the copied statistics. |
| scale_factor | The scale for adjusting values of statistical attributes such as nblks (number of blocks) and nrows (number of rows) when you copy the statistics to the destination partition (or subpartition). |
| flags | Default value: NULL. Generally, this parameter is not required. |
| force | Default value: FALSE. If you set the value to TRUE, statistics are still copied even if the destination partition (or subpartition) is locked. |
Exceptions
| Error code | Description |
|---|---|
| ORA-20000 | The partition name is invalid. |
| ORA-20001 | The source and destination partitions are of different types. |
Considerations
This procedure updates the minimum and maximum values of the destination partition based on the following rules:
If the partitioning type is HASH, the minimum and maximum values of the destination partition are the same as those of the source partition.
If the partitioning type is LIST, the minimum and maximum values of the destination partition depend on the setting of the destination partition.
If the destination partition is a DEFAULT partition:
- The minimum value of the destination partition is set to that of the source partition.
- The maximum value of the destination partition is set to that of the source partition.
If the destination partition is a NOT DEFAULT partition:
- The minimum value of the destination partition is set to the minimum value in the value list that describes the destination partition.
- The maximum value of the destination partition is set to the maximum value in the value list that describes the destination partition.
If the partitioning type is RANGE:
When the destination partition is the first partition:
If the upper bound of the destination partition is
MAXVALUE:- The destination partition and source partition are the same, and you do not need to copy or update the minimum and maximum values. In other words, you do not need to set its minimum or maximum value.
If the upper bound of the destination partition is not
MAXVALUE:- Both the maximum and minimum values of the destination partition are set to the upper bound of the destination partition.
When the destination partition is not the first partition:
If the partitioning key of the destination partition is a single column:
In the case that the source partition has a single value and the value is equal to the lower bound of the source partition, and that the upper bound of the destination partition is not
MAXVALUE, both the maximum and minimum values of the destination partition are set to the lower bound of the destination partition.Otherwise:
- If the upper bound of the destination partition is
MAXVALUE, both the maximum and minimum values are set to the lower bound of the destination partition, namely, the upper bound of the previous partition. - If the upper bound of the destination partition is not
MAXVALUE, the maximum value is set to its upper bound and the minimum value is set to its lower bound.
- If the upper bound of the destination partition is
If the partitioning key of the destination partition contains multiple columns:
If the source partition has only one value that is equal to its lower bound, the maximum and minimum values of the destination partition are both set to the lower bound of the destination partition.
Otherwise, the foregoing rule is used for the current column C. For a subsequent column Cx:
- If
MAXVALUEof Cx-1 is the same in the destination partition and the previous partition, the maximum value of the destination partition is set to its upper bound. - Otherwise, the maximum value of the destination partition column is set to the larger one of the upper bound of the destination partition and the maximum value of the source partition column.
- If
Examples
Create a HASH-partitioned table named test_copy_tbl1 and perform operations related to statistics collection.
(Optional) If a table named
test_copy_tbl1already exists, execute the following statement to drop it:DROP TABLE test_copy_tbl1;Execute the following statement to create a table named
test_copy_tbl1, which contains two integer columnscol1andcol2, and use the HASH function to partition the table by thecol1column into four partitions:p1,p2,p3, andp4.CREATE TABLE test_copy_tbl1(col1 INT, col2 INT) PARTITION BY HASH(col1) (PARTITION p1, PARTITION p2, PARTITION p3, PARTITION p4);Execute the following statement to insert 10 rows into the
test_copy_tbl1table. To be specific,1to10are inserted into thecol1column and2to11are inserted into thecol2column.INSERT INTO test_copy_tbl1 SELECT level, level + 1 FROM dual CONNECT BY LEVEL <= 10;The return result is as follows:
Query OK, 10 rows affected (0.060 sec) Records: 10 Duplicates: 0 Warnings: 0Execute the following statement to call the
gather_table_statsprocedure in theDBMS_STATSpackage to collect the statistics of theP1partition in thetest_copy_tbl1table. Themethod_optparameter specifies the histogram size for all columns, which is 32 buckets here.CALL dbms_stats.gather_table_stats('SYS','TEST_COPY_TBL1', 'P1', method_opt=>'for all columns size 32');Execute the following statement to call the
copy_table_statsprocedure in theDBMS_STATSpackage to copy the statistics of theP1partition in thetest_copy_tbl1table to theP2partition based on the scale factor2.CALL dbms_stats.copy_table_stats('SYS','TEST_COPY_TBL1','P1','P2', scale_factor=>2);Execute the following statement to query the
all_tab_statisticsview for the statistics of all partitions in theTEST_COPY_TBL1table owned by theSYSuser, including the table name, partition names, number of rows, number of blocks, average row length, whether the statistical type is locked, whether the statistics are outdated, and last analysis time.SELECT table_name, partition_name, num_rows, blocks, avg_row_len, stattype_locked locked, stale_stats, last_analyzed FROM all_tab_statistics WHERE table_name = 'TEST_COPY_TBL1' AND owner = 'SYS';The return result is as follows:
+----------------+----------------+----------+--------+-------------+--------+-------------+---------------+ | TABLE_NAME | PARTITION_NAME | NUM_ROWS | BLOCKS | AVG_ROW_LEN | LOCKED | STALE_STATS | LAST_ANALYZED | +----------------+----------------+----------+--------+-------------+--------+-------------+---------------+ | TEST_COPY_TBL1 | NULL | 10 | NULL | 40 | NULL | NO | 08-JAN-24 | | TEST_COPY_TBL1 | P1 | 2 | NULL | 40 | NULL | NO | 08-JAN-24 | | TEST_COPY_TBL1 | P2 | 4 | NULL | 40 | NULL | NO | 08-JAN-24 | | TEST_COPY_TBL1 | P3 | NULL | NULL | NULL | NULL | NULL | NULL | | TEST_COPY_TBL1 | P4 | NULL | NULL | NULL | NULL | NULL | NULL | +----------------+----------------+----------+--------+-------------+--------+-------------+---------------+ 5 rows in setFor more information about the
all_tab_statisticsview, see ALL_TAB_STATISTICS.Execute the following statement to query the
all_part_col_statisticsview for the statistics of the partition columns of theTEST_COPY_TBL1table owned by theSYSuser, and sort the statistics by partition name and column name.SELECT * FROM all_part_col_statistics WHERE table_name = 'TEST_COPY_TBL1' AND owner = 'SYS' ORDER BY partition_name, column_name;The return result is as follows:
+-------+----------------+----------------+-------------+--------------+-----------+------------+---------+-----------+-------------+-------------+---------------+--------------+------------+-------+-------------+-----------+ | OWNER | TABLE_NAME | PARTITION_NAME | COLUMN_NAME | NUM_DISTINCT | LOW_VALUE | HIGH_VALUE | DENSITY | NUM_NULLS | NUM_BUCKETS | SAMPLE_SIZE | LAST_ANALYZED | GLOBAL_STATS | USER_STATS | NOTES | AVG_COL_LEN | HISTOGRAM | +-------+----------------+----------------+-------------+--------------+-----------+------------+---------+-----------+-------------+-------------+---------------+--------------+------------+-------+-------------+-----------+ | SYS | TEST_COPY_TBL1 | P1 | COL1 | 2 | 2 | 7 | .25 | 0 | 2 | 2 | 08-JAN-24 | NO | NO | NULL | 20 | FREQUENCY | | SYS | TEST_COPY_TBL1 | P1 | COL2 | 2 | 3 | 8 | .25 | 0 | 2 | 2 | 08-JAN-24 | NO | NO | NULL | 20 | FREQUENCY | | SYS | TEST_COPY_TBL1 | P2 | COL1 | 2 | 2 | 7 | .25 | 0 | 2 | 2 | 08-JAN-24 | NO | NO | NULL | 20 | FREQUENCY | | SYS | TEST_COPY_TBL1 | P2 | COL2 | 2 | 3 | 8 | .25 | 0 | 2 | 2 | 08-JAN-24 | NO | NO | NULL | 20 | FREQUENCY | | SYS | TEST_COPY_TBL1 | P3 | COL1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | | SYS | TEST_COPY_TBL1 | P3 | COL2 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | | SYS | TEST_COPY_TBL1 | P4 | COL1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | | SYS | TEST_COPY_TBL1 | P4 | COL2 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | +-------+----------------+----------------+-------------+--------------+-----------+------------+---------+-----------+-------------+-------------+---------------+--------------+------------+-------+-------------+-----------+ 8 rows in setFor more information about the
all_part_col_statisticsview, see ALL_PART_COL_STATISTICS.Execute the following statement to query the
all_part_histogramsview for the histogram statistics of the partition columns of theTEST_COPY_TBL1table owned by theSYSuser, and sort the statistics by partition name and column name.SELECT * FROM all_part_histograms WHERE table_name = 'TEST_COPY_TBL1' AND owner = 'SYS' ORDER BY partition_name, column_name;The return result is as follows:
+-------+----------------+----------------+-------------+-----------------+----------------+-----------------------+---------------------------+-----------------------+ | OWNER | TABLE_NAME | PARTITION_NAME | COLUMN_NAME | ENDPOINT_NUMBER | ENDPOINT_VALUE | ENDPOINT_ACTUAL_VALUE | ENDPOINT_ACTUAL_VALUE_RAW | ENDPOINT_REPEAT_COUNT | +-------+----------------+----------------+-------------+-----------------+----------------+-----------------------+---------------------------+-----------------------+ | SYS | TEST_COPY_TBL1 | P1 | COL1 | 2 | NULL | 7 | 7 | 1 | | SYS | TEST_COPY_TBL1 | P1 | COL1 | 1 | NULL | 2 | 2 | 1 | | SYS | TEST_COPY_TBL1 | P1 | COL2 | 2 | NULL | 8 | 8 | 1 | | SYS | TEST_COPY_TBL1 | P1 | COL2 | 1 | NULL | 3 | 3 | 1 | | SYS | TEST_COPY_TBL1 | P2 | COL1 | 2 | NULL | 7 | 7 | 1 | | SYS | TEST_COPY_TBL1 | P2 | COL1 | 1 | NULL | 2 | 2 | 1 | | SYS | TEST_COPY_TBL1 | P2 | COL2 | 2 | NULL | 8 | 8 | 1 | | SYS | TEST_COPY_TBL1 | P2 | COL2 | 1 | NULL | 3 | 3 | 1 | +-------+----------------+----------------+-------------+-----------------+----------------+-----------------------+---------------------------+-----------------------+ 8 rows in setFor more information about the
all_part_histogramsview, see ALL_PART_HISTOGRAMS.