The COPY_TABLE_STATS procedure copies statistics from a source partition (or subpartition) to a target partition (or subpartition).
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only 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 (user) name of the source and target partitions (or subpartitions) in the table. |
| tabname | The name of the table corresponding to the source and target partitions (or subpartitions). |
| srcpartname | The source partition (or subpartition) from which to copy statistics. |
| dtspartname | The target partition (or subpartition) that receives the statistics. |
| scale_factor | The value used to scale statistics attributes such as nblks (number of blocks) and nrows (number of rows) when copying to the target 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 continues even if the target partition (or subpartition) is currently locked. |
Exceptions
| Error Code | Description |
|---|---|
| ORA-20000 | Indicates an invalid partition name. |
| ORA-20001 | Indicates that the source and target partitions have different types. |
Considerations
This procedure updates the minimum and maximum values in the target partition as follows:
If the partition type is HASH, the minimum and maximum values of the target partition are the same as those of the source partition.
If the partition type is LIST, the behavior depends on the settings of the target partition:
If the target partition is a DEFAULT partition, then:
- The minimum value of the target partition is set to the minimum value of the source partition.
- The maximum value of the target partition is set to the maximum value of the source partition.
If the target partition is a NOT DEFAULT partition, then:
- The minimum value of the target partition is set to the minimum value in the list of values that describe the target partition.
- The maximum value of the target partition is set to the maximum value in the list of values that describe the target partition.
If the partition type is RANGE, then:
If the target partition is the first partition, then:
If the upper bound of the target partition is
MAXVALUE, then:- The target partition and source partition are the same partition, so no need to copy or update the minimum and maximum values. The minimum and maximum values do not need to be set.
If the upper bound of the target partition is not
MAXVALUE, then:- The minimum and maximum values of the target partition are set to the upper bound of the target partition.
If the target partition is not the first partition, then:
If the target partition is a single-column partition key, then:
If the source partition has only one value that is equal to the lower bound of the source partition, and the upper bound of the target partition is not
MAXVALUE, then the minimum and maximum values of the target partition are set to the lower bound of the target partition.Otherwise:
- If the upper bound of the target partition is
MAXVALUE, then both the minimum and maximum values are set to the lower bound of the target partition (which is also the upper bound of the previous partition). - If the upper bound of the target partition is not
MAXVALUE, then the maximum value is set to the upper bound of the target partition, and the minimum value is set to the lower bound of the target partition.
- If the upper bound of the target partition is
If the target partition is a multi-column partition key, then:
If the source partition has only one value that is equal to the lower bound of the source partition, then the minimum and maximum values of the target partition are set to the lower bound of the target partition.
Otherwise, the current column C follows the rules mentioned above, and for subsequent columns Cx, then:
- If Cx-1 in the target partition is the same as
MAXVALUEin the target partition before, then the maximum value of the target partition is set to the upper bound of the target partition. - Otherwise, the maximum value of the target partition column is set to the larger of the upper bound of the target partition and the maximum value of the source partition column.
- If Cx-1 in the target partition is the same as
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 test_copy_tbl1;Execute the following statement to create a table named
test_copy_tbl1with two integer columnscol1andcol2, and partition it by thecol1column using the hash function 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 of data into the
test_copy_tbl1table. Thecol1column contains values from 1 to 10, and thecol2column contains values from 2 to 11.INSERT INTO test_copy_tbl1 SELECT level, level + 1 FROM dual CONNECT BY LEVEL <= 10;The returned 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 statistics for theP1partition of thetest_copy_tbl1table. Themethod_optparameter specifies a histogram size of 32 buckets for all columns.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 for theP1partition of thetest_copy_tbl1table to theP2partition with a scale factor of 2.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 and retrieve statistics for all partitions of theTEST_COPY_TBL1table owned bySYS, including the table name, partition name, number of rows, number of blocks, average row length, whether the statistics type is locked, whether the statistics are outdated, and the 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 returned 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 and retrieve partition column statistics for theTEST_COPY_TBL1table owned bySYS, sorted 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 returned 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 setExecute the following statement to query the
all_part_histogramsview and retrieve partition column histogram information for theTEST_COPY_TBL1table owned bySYS, sorted 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 returned 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 set