DBAtlas · Atlas de operaciones de bases de datos

Configurar estadísticas incrementales para una tabla particionada

OracleEstadísticas

Comando

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

Qué hace

En modo incremental se guarda una synopsis por partición; las estadísticas globales (a nivel de tabla), en particular el NDV, se calculan combinando synopses sin volver a recorrer toda la tabla. Solo se vuelven a leer las particiones modificadas.

Cuándo usarlo

En tablas grandes (escala de TB) particionadas por range/interval donde los datos nuevos solo llegan a las particiones recientes, cuando la recopilación de estadísticas globales excede la ventana de mantenimiento.

Precaución

No tiene licencia propia, pero ya se requiere la opción Partitioning (EE con costo adicional). Las synopses consumen espacio en SYSAUX (el formato HyperLogLog de 12.2+ es mucho más pequeño). La primera recopilación es un full scan; iníciela en una ventana de mantenimiento. Poner ESTIMATE_PERCENT en algo distinto de AUTO_SAMPLE_SIZE deshabilita el modo incremental.

Abrir en DBAtlas →

Relacionado