Pages

Wednesday, January 18, 2023

How to copy stats from existing partition to a new partition

How to copy stats from existing partition to a new partition

--Unlock stats. to avoid reset from scheduled stats collection
BEGIN
 DBMS_STATS.UNLOCK_TABLE_STATS('MY_OWNER','MY_TABLE');
END;
/

--Gather table stats - on the table
BEGIN
 DBMS_STATS.gather_table_stats('MY_OWNER','MY_TABLE',CASCADE => TRUE);
END;
/

--See current status
SELECT table_owner, table_name, partition_name, num_rows, blocks, last_analyzed 
FROM DBA_TAB_PARTITIONS 
WHERE table_name = 'XXXXXXXX'

--Generate COPY_TABLE_STATS SQLs, from P23 partition to zero stats partitions.
SELECT 'BEGIN  DBMS_STATS.COPY_TABLE_STATS ('''||table_owner||''', '''||table_name||''', ''P_23'', '''||partition_name||''', FORCE=>TRUE);  END;'||CHR(10)||'/'
  FROM DBA_TAB_PARTITIONS 
 WHERE table_name = 'XXXXXXXX' AND blocks = 0;

--Copy Start from Old Partition to New partition, Example
BEGIN
 DBMS_STATS.COPY_TABLE_STATS ('MY_OWNER', 'MY_TABLE', 'P_SOURCE', 'P_TARGET', FORCE=>TRUE);
END;
/

--Lock stats. to avoid reset from scheduled stats collection
BEGIN
 DBMS_STATS.LOCK_TABLE_STATS('MY_OWNER','MY_TABLE');
END;
/

--Make stats incremental, when collecting stats. 
For big table, partitions were data was not changed, will not be involved again and again in stats collection, only partitions were data was changed, will be worked upon.
BEGIN
 DBMS_STATS.SET_TABLE_PREFS('MY_OWNER','MY_TABLE','INCREMENTAL','TRUE');
END;
/


No comments:

Post a Comment