Bloquear y desbloquear las estadísticas de una tabla/esquema
OracleEstadísticasComando
-- Lock (the auto stats job and normal GATHER calls no longer touch them):
EXEC DBMS_STATS.LOCK_TABLE_STATS(ownname => 'CUSTOMER_APP', tabname => 'ORDERS');
EXEC DBMS_STATS.LOCK_SCHEMA_STATS(ownname => 'CUSTOMER_APP');
-- A single partition only: DBMS_STATS.LOCK_PARTITION_STATS('CUSTOMER_APP', 'SALE_PART', 'P2026_H1');
-- Locked objects:
SELECT owner, table_name, partition_name, stattype_locked
FROM dba_tab_statistics
WHERE owner = 'CUSTOMER_APP' AND stattype_locked IS NOT NULL;
-- Deliberate gathering while locked (instead of ORA-20005):
EXEC DBMS_STATS.GATHER_TABLE_STATS('CUSTOMER_APP', 'ORDERS', force => TRUE);
-- Unlock:
EXEC DBMS_STATS.UNLOCK_SCHEMA_STATS(ownname => 'CUSTOMER_APP');
EXEC DBMS_STATS.UNLOCK_TABLE_STATS(ownname => 'CUSTOMER_APP', tabname => 'ORDERS');Qué hace
Bloquea las estadísticas frente a cambios; la tarea automática omite los objetos bloqueados y una llamada GATHER a nivel de tabla lanza ORA-20005. force=>TRUE recopila sin quitar el bloqueo.
Cuándo usarlo
Para mantener estadísticas representativas en tablas de staging/volátiles que se vacían y rellenan durante el día, proteger los planes de una tabla crítica frente a cambios de estadísticas, o conservar valores definidos con DBMS_STATS.SET_TABLE_STATS.
Precaución
No requiere licencia. Las estadísticas bloqueadas se alejan de la realidad con el tiempo y causan errores de cardinalidad; informe regularmente de los objetos bloqueados. Un bloqueo de esquema no cubre las tablas añadidas después al esquema. En una tabla bloqueada, CREATE/REBUILD INDEX tampoco recopila automáticamente estadísticas del índice.