DBAtlas · Atlas de operações de banco de dados

Coletar estatísticas como pending, testá-las e publicá-las

OracleEstatísticas

Comando

-- 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');

O que faz

Com a preferência PUBLISH=FALSE, as estatísticas coletadas são gravadas em uma área pending em vez do dicionário, então as outras sessões não são afetadas. Os planos são validados em uma sessão de teste com optimizer_use_pending_statistics e depois publicados ou descartados com um único comando.

Quando usar

Para testar sem risco, em produção, o impacto no plano de estatísticas atualizadas em tabelas OLTP críticas e para uma virada controlada após uma grande carga de dados.

Atenção

Não exige licença. Enquanto PUBLISH=FALSE permanecer, as estatísticas coletadas pela tarefa automática também se acumulam como pending e nunca são publicadas; lembre-se de voltar a preferência para TRUE. EXPLAIN PLAN é uma estimativa; quando possível execute a consulta e verifique o DISPLAY_CURSOR.

Abrir no DBAtlas →

Relacionados