The COPY_TABLE_STATS procedure copies statistics from a source partition (or subpartition) to a destination partition (or subpartition).
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition does not support this feature.
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 | specifies the schema (user) name where the source and destination partitions (or subpartitions) are located. |
| tabname | The name of the table that corresponds to the source and target partition (or subpartition). |
| srcpartname | Specifies the source partition (or subpartition) from which to copy statistics. |
| dtspartname | The target partition (or subpartition) where the statistics are received. |
| scale_factor | The value by which statistics attributes (for example, nblks and nrows) are proportionally scaled when data is copied to a destination partition (or subpartition). |
| flags | Defaults to NULL. Regular users usually do not need to set it. |
| force | Default is FALSE. If this parameter is set to TRUE, the statistical data replication operation continues even if the target partition (or subpartition) is in a locked state. |
Exceptions
| Error code | Description |
|---|---|
| OBE-20000 | The partition name is invalid. |
| OBE-20001 | Indicates that the source partition and target partition have different types. |
Considerations
The procedure updates the minimum and maximum values in the target partition as follows:
For a partitioned table that uses a HASH partitioning method, the minimum and maximum values of the target partition are the same as those of the source partition.
If the partitioning type is LIST, behavior depends on the settings for the target partition.
If the target partition is the 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 not a DEFAULT partition, then:
- The minimum value of the target partition is set to the minimum value of the values describing the target partition.
- The maximum value for the target partition is set to the maximum value among the values in the list that describe the target partition.
For RANGE partitions, you can specify:
If the target partition is the first partition, then:
If the upper bound of the target partition is
MAXVALUE, then:- The target partition and the source partition are the same. In this case, you do not need to copy or update the partition's min or max values.
If the upper bound of the target partition is not
MAXVALUE, then:- The maximum and minimum values of the target partition are set to the upper bounds of the target partition.
If the destination partition is not the first partition:
If the target partition is a single-column partition key, then:
The minimum and maximum values of the destination partition are both set to the lower bound of the destination partition if the source partition has only one value, and this value is equal to the lower bound of the source partition. In addition, the upper bound of the destination partition is not
MAXVALUE.Otherwise:
- If the upper boundary of the target partition is
MAXVALUE, the maximum and minimum 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, 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 boundary of the target partition is
If the target partition is a composite partitioned column, then:
If the source partition contains only one value, which 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 adopts the previous rule, and for subsequent columns Cx, it would be:
- Cx-1 If the value specified in the
MAXVALUEclause is the same as the upper bound of the partition before the target partition, the upper bound of the target partition is set to the value specified in theMAXVALUEclause. - Otherwise, the maximum value of the target partition column is set to the larger of the maximum value of the partition column of the source data and the upper bound of the target partition.
- Cx-1 If the value specified in the
Examples
Create a hash-partitioned table named test_copy_tbl1 and perform related statistical 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 the table 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, where 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 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 that the histogram size for all columns is 32 buckets.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 with a scaling factor of 2 to theP2partition.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 obtain the 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 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 obtain the 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 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 and obtain the 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 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.
