Oracle
 sql >> Database >  >> RDS >> Oracle

Come trovare la tabella in cui le statistiche sono bloccate

Le statistiche hanno giocato un ruolo chiave per l'ottimizzazione delle prestazioni di Oracle. Oracle Optimizer crea il piano di esecuzione in base alle statistiche della tabella Oracle coinvolta nelle query SQL.

Potresti voler bloccare le statistiche in una tabella Oracle in alcuni casi, ad esempio

  • non vuoi che una tabella venga analizzata in base al lavoro di pianificazione statistica, ma vuoi analizzarla in un secondo momento o con una stima più alta
  • non vuoi generare le statistiche per la tabella per motivi di prestazioni
  • non vuoi che il server perda tempo generare statistiche quando i dati della tabella non cambiano

Potrebbero esserci molti altri casi in cui vogliamo bloccare le statistiche

Sommario

Come bloccare le statistiche sul tavolo

Puoi utilizzare il pacchetto Oracle standard DBMS_STATS per bloccare le statistiche sulla tabella

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

Come trovare la tabella in cui le statistiche sono bloccate

Puoi utilizzare la query di seguito per trovare tutte le tabelle in cui le statistiche sono bloccate

select owner, table_name, stattype_locked
from dba_tab_statistics
where stattype_locked is not null;

Esecuzione dell'attività di generazione delle statistiche sul tavolo in cui le statistiche sono bloccate 

Se proviamo a eseguire la raccolta delle statistiche sulle tabelle in cui le statistiche sono bloccate, otteniamo Le statistiche degli oggetti ORA-20005 sono bloccate (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

Possiamo eseguire i passaggi seguenti per sbloccare le statistiche e generare le statistiche e bloccare nuovamente

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

Come sbloccare le statistiche per la tabella e lo schema/sbloccare le statistiche della tabella per lo schema

Ora, una volta individuati gli oggetti, possiamo utilizzare le query seguenti per sbloccarli

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

Creazione dell'indice con statistiche bloccate sulla tabella

10g in poi, ogni volta che creiamo l'indice, le statistiche vengono generate automaticamente. Ora questa equazione cambia Quando la tabella è bloccata, le statistiche non verranno generate durante la creazione dell'indice. Dobbiamo usare l'opzione FORZA per raccogliere le statistiche durante la creazione dell'indice per gli oggetti bloccati. Capiamolo in dettaglio vedendo l'esempio

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.

Spero che ti piacciano le informazioni su come bloccare/sbloccare le statistiche delle tabelle in Oracle. Inoltre ora devi sapere cosa fare quando ORA-20005:le statistiche degli oggetti sono bloccate e ORA-38029:statistiche sugli oggetti sono bloccati accade

Articoli correlati
Raccolta delle statistiche nella versione 11i e R12
Raccolta delle statistiche incrementali in 11g
ora-20001 in Raccogli le statistiche dello schema su 11g(FND_HISTOGRAM_COLS)
Come impostare il monitoraggio della tabella in Oracle e la relazione con STATISTICS_LEVEL
Esercitazione Oracle:come controllare le statistiche obsolete
Modalità Oracle Optimizer
Documentazione Oracle sulle statistiche