DBAtlas · Database operations atlas

Gather statistics as pending, test them and publish

OracleStatistics

Command

-- 1) Disable publishing for the table (new statistics go to pending):
EXEC DBMS_STATS.SET_TABLE_PREFS('CUSTOMER_APP', 'ORDERS', 'PUBLISH', 'FALSE');
EXEC DBMS_STATS.GATHER_TABLE_STATS('CUSTOMER_APP', 'ORDERS');

-- 2) Compare pending and published values:
SELECT owner, table_name, partition_name, num_rows,
       TO_CHAR(last_analyzed,'YYYY-MM-DD HH24:MI') AS last_analyzed
FROM   dba_tab_pending_stats
WHERE  owner = 'CUSTOMER_APP';
SELECT num_rows, last_analyzed FROM dba_tables
WHERE  owner = 'CUSTOMER_APP' AND table_name = 'ORDERS';

-- 3) Try plans with pending statistics in this session only:
ALTER SESSION SET optimizer_use_pending_statistics = TRUE;
EXPLAIN PLAN FOR SELECT COUNT(*) FROM customer_app.orders WHERE status = 7;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'BASIC +ROWS'));
ALTER SESSION SET optimizer_use_pending_statistics = FALSE;

-- 4a) If good, publish:
EXEC DBMS_STATS.PUBLISH_PENDING_STATS('CUSTOMER_APP', 'ORDERS', no_invalidate => FALSE);
-- 4b) If bad, discard:
-- EXEC DBMS_STATS.DELETE_PENDING_STATS('CUSTOMER_APP', 'ORDERS');

-- 5) Return to normal behavior:
EXEC DBMS_STATS.SET_TABLE_PREFS('CUSTOMER_APP', 'ORDERS', 'PUBLISH', 'TRUE');

What it does

With the PUBLISH=FALSE preference, gathered statistics are written to a pending area instead of the dictionary, so other sessions are unaffected. Plans are validated in a test session with optimizer_use_pending_statistics, then published or discarded with one command.

When to use it

To test the plan impact of refreshed statistics on critical OLTP tables in production without risk, and for a controlled cutover after a large data load.

Caution

No license required. While PUBLISH=FALSE remains, statistics gathered by the automatic task also pile up as pending and are never published; remember to set the preference back to TRUE. EXPLAIN PLAN is an estimate; where possible run the query and check DISPLAY_CURSOR.

Open in DBAtlas →

Related