Configurar estatísticas incrementais para uma tabela particionada
OracleEstatísticasComando
-- 1) Enable incremental mode (global statistics are derived from partition synopses):
EXEC DBMS_STATS.SET_TABLE_PREFS('CUSTOMER_APP', 'SALE_PART', 'INCREMENTAL', 'TRUE');
EXEC DBMS_STATS.SET_TABLE_PREFS('CUSTOMER_APP', 'SALE_PART', 'INCREMENTAL_STALENESS', 'USE_STALE_PERCENT');
EXEC DBMS_STATS.SET_TABLE_PREFS('CUSTOMER_APP', 'SALE_PART', 'GRANULARITY', 'AUTO');
-- 2) The first gather scans everything and builds synopses; later ones read only changed partitions:
EXEC DBMS_STATS.GATHER_TABLE_STATS(ownname => 'CUSTOMER_APP', tabname => 'SALE_PART');
-- 3) Verify:
SELECT DBMS_STATS.GET_PREFS('INCREMENTAL', 'CUSTOMER_APP', 'SALE_PART') AS incremental FROM dual;
SELECT partition_name, num_rows,
TO_CHAR(last_analyzed,'YYYY-MM-DD HH24:MI:SS') AS last_analyzed, stale_stats
FROM dba_tab_statistics
WHERE owner = 'CUSTOMER_APP' AND table_name = 'SALE_PART'
ORDER BY partition_position NULLS FIRST;
-- 4) Pre-build synopses on a staging table to be loaded via exchange (12c+):
-- EXEC DBMS_STATS.SET_TABLE_PREFS('CUSTOMER_APP', 'SALE_STAGE', 'INCREMENTAL', 'TRUE');
-- EXEC DBMS_STATS.SET_TABLE_PREFS('CUSTOMER_APP', 'SALE_STAGE', 'INCREMENTAL_LEVEL', 'TABLE');
-- EXEC DBMS_STATS.GATHER_TABLE_STATS('CUSTOMER_APP', 'SALE_STAGE');O que faz
No modo incremental um synopsis é armazenado por partição; as estatísticas globais (em nível de tabela), em especial o NDV, são calculadas mesclando os synopses sem reler a tabela inteira. Apenas as partições alteradas são lidas novamente.
Quando usar
Em tabelas grandes (escala de TB) particionadas por range/interval em que os dados novos caem apenas nas partições recentes, quando a coleta de estatísticas globais excede a janela de manutenção.
Atenção
Não tem licença própria, mas a opção Partitioning (EE com custo extra) já é necessária. Os synopses consomem espaço no SYSAUX (o formato HyperLogLog do 12.2+ é bem menor). A primeira coleta é um full scan; inicie-a em uma janela de manutenção. Definir ESTIMATE_PERCENT com qualquer valor diferente de AUTO_SAMPLE_SIZE desabilita o incremental.