Las estadísticas juegan un papel clave en el ajuste de rendimiento de Oracle. El Optimizador de Oracle crea el plan de ejecución basándose en las estadísticas de las tablas de Oracle implicadas en las consultas SQL.
En algunos casos puede que quieras bloquear las estadísticas de una tabla de Oracle, por ejemplo
- no quieres que un job de estadísticas programado analice una tabla, pero quieres analizarla más tarde o con un estimate mayor
- no quieres generar las estadísticas de la tabla por motivos de rendimiento
- no quieres que el servidor dedique tiempo a generar estadísticas cuando los datos de la tabla no cambian
Podría haber muchos más casos en los que queramos bloquear las estadísticas.
Cómo bloquear las estadísticas de la tabla
Puedes usar el paquete estándar de Oracle DBMS_STATS para bloquear las estadísticas de la tabla
exec dbms_stats.lock_table_stats('table_owner','table_name');
Example SELECT stattype_locked FROM dba_tab_statistics WHERE table_name = 'TEST' and owner = 'TECH'; STATTYPE_LOCKED ------------------- exec dbms_stats.lock_table_stats('TEST','TECH'); SELECT stattype_locked FROM dba_tab_statistics WHERE table_name = 'TEST' and owner = 'TECH'; STATTYPE_LOCKED --------------- ALL
Cómo encontrar las tablas donde las estadísticas están bloqueadas
Puedes usar la siguiente consulta para encontrar todas las tablas donde las estadísticas están bloqueadas
select owner, table_name, stattype_locked from dba_tab_statistics where stattype_locked is not null;
Ejecutar el job de generación de estadísticas en una tabla donde las estadísticas están bloqueadas
Si intentamos ejecutar la recopilación de estadísticas en las tablas donde están bloqueadas, obtenemos ORA-20005 object statistics are locked (stattype = all)
SQL> exec dbms_stats.gather_table_stats('TECH', 'TEST'); BEGIN dbms_stats.gather_table_stats('TECH', 'TEST'); END; * ERROR at line 1: ORA-20005: object statistics are locked (stattype = ALL) ORA-06512: at "SYS.DBMS_STATS", line 10640 ORA-06512: at "SYS.DBMS_STATS", line 10664 ORA-06512: at line 1
Podemos realizar los siguientes pasos para desbloquear las estadísticas, generarlas y volver a bloquearlas
exec dbms_stats.unlock_table_stats('TECH','TEST'); exec dbms_stats.gather_table_stats('TECH', 'TEST'); exec dbms_stats.lock_table_stats('TECH','TEST'); or exec dbms_stats.gather_table_stats('TECH', 'TEST',force=>true);
Cómo desbloquear las estadísticas de una tabla y un esquema / desbloquear estadísticas de tablas de un esquema
Ahora que hemos encontrado los objetos, podemos usar las siguientes consultas para desbloquearlos
unlock table stats for schema exec dbms_stats.unlock_schema_stats('schema_owner'); exec dbms_stats.unlock_table_stats('table_owner','table_name'); Example exec dbms_stats.unlock_schema_stats('TECH'); exec dbms_stats.unlock_table_stats('TECH','TEST');
Creación de índices con las estadísticas bloqueadas en la tabla
A partir de 10g, siempre que creamos un índice, las estadísticas se generan automáticamente. Ahora esta ecuación cambia cuando la tabla está bloqueada: las estadísticas no se generarán al crear el índice. Necesitamos usar la opción FORCE para recopilar las estadísticas al crear un índice para objetos bloqueados. Entendámoslo en detalle con un ejemplo
Example Lets first create the dummy table and lock the statistics on that table SQL> create table test as select a.* ,rownum id from all_objects a where rownum <1001; SQL> exec dbms_stats.lock_table_stats('TECH','TEST'); Now we will try to create index SQL> create index test_idx on test(id); Index created. SQL> select num_rows, last_analyzed from user_ind_statistics where index_name ='TEST_IDX'; NUM_ROWS LAST_ANAL ---------- --------- So statistics on index is not generated automatically for the locked statistics table Lets try to generate the statistics using DBMS_STATS SQL> exec dbms_stats.gather_index_stats(null, 'TEST_IDX'); BEGIN dbms_stats.gather_index_stats(null, 'TEST_IDX'); END; * ERROR at line 1: ORA-20005: object statistics are locked (stattype = ALL) ORA-06512: at "SYS.DBMS_STATS", line 10640 ORA-06512: at "SYS.DBMS_STATS", line 10664 ORA-06512: at line 1 So statistics generation failed. In order to generate stats on the index, We can use force option in dbms_stats to override this SQL> exec dbms_stats.gather_index_stats(null, 'TEST_IDX',force=>true); PL/SQL procedure successfully completed. SQL> select num_rows, last_analyzed from user_ind_statistics where index_name ='IDX'; NUM_ROWS LAST_ANAL ---------- --------- 1000 01-SEP-17 Lets try to create a new index with compute statistics clause SQL> create index TEST_IDX1 on test(object_name) compute statistics; create index idx on test(object_name) compute statistics * ERROR at line 1: ORA-38029: object statistics are locked So ORA-38029 error happens, So we need to create index with out the compute statistics clause and then generate stats using force option SQL> create index TEST_IDX1 on test(object_name); SQL> exec dbms_stats.gather_index_stats(null, 'TEST_IDX1',force=>true); Same things happens if we rebuild the index with compute statistics option SQL> alter index TEST_IDX1 rebuild compute statistics; alter index TEST_IDX1 rebuild compute statistics * ERROR at line 1: ORA-38029: object statistics are locked SQL> alter index TEST_IDX1 rebuild; Index altered. SQL> exec dbms_stats.gather_index_stats(null, 'TEST_IDX1',force=>true); PL/SQL procedure successfully completed.
Espero que te guste la información sobre cómo bloquear/desbloquear las estadísticas de tablas en Oracle. Ahora también deberías saber qué hacer cuando ocurren ORA-20005: object statistics are locked y ORA-38029: object statistics are locked
Artículos relacionados
Recopilación de estadísticas en Release 11i y R12
Recopilación incremental de estadísticas en 11g
ora-20001 in Gather schema stats on 11g (FND_HISTOGRAM_COLS)
Cómo configurar el Table Monitoring en Oracle y su relación con STATISTICS_LEVEL
Oracle Tutorial: Cómo comprobar estadísticas obsoletas
Oracle Optimizer Mode
Documentación de Oracle sobre estadísticas