Backup e restore de estatísticas
Autor: Mario Barduchi | Publicado: 2016-10-18T13:16:00-02:00


Hola que tal?



Eita, ahahahaha não ficou bom isso...



Opa, pessoal.. Como estão?&nbsp;



Agora sim.



Em primeiro lugar, fique claro que esse texto é somente uma pequena introdução sobre o assunto. Alias nem entramos no assunto das estatísticas.



Aqui estamos abordando somente uma ou duas formas de fazer backup's de estatísticas.



Se você quer conhecer melhor sobre a coleta de estatísticas, aqui vão alguns links excelentes:



Apresentação do Portilho no GUOB&nbsp;

Best Practices for Gathering Optimizer Statistics with Oracle Database 12c

Optimizer with Oracle Database 12c



Só alguns exemplos dos excelentes textos e documentos obrigatórios da Oracle que nós achamos sobre o assunto.&nbsp;



Este note do MOS também é ótimo: "FAQ: Automatic Statistics Collection (Doc ID 1233203.1)"



Dito isso, então deixemos de mimimi e vamos ao que interessa.



Esses dias me aconteceu um pequeno problema com estatísticas de um banco. Depois da coleta, a performance de várias tabelas ficou pior do que anteriormente.&nbsp;



Com isso e com aquela falta de paciência normal dos usuários, veio a pergunta premiada. Tem como voltar essas tais estatísticas?



Claro, pequeno gafanhoto. Nós somos DBA's e DBA que se preze não faz nada sem backup (e testa o restore disso também, é claro).



Então, é isso que eu quero mostrar. Como fazer o "backup das estatísticas" de um schema e depois retornar isso em caso de necessidade.&nbsp;



Coisa simples mas que muitos esquecem antes de fazer uma intervenção dessas.



Coloquei o "backup" entre aspas pois na verdade faremos um EXPORT/IMPORT das estatísticas no nosso primeiro exemplo. Já no caso do backup feito pelo próprio Oracle, que é o nosso segundo exemplo, trataremos de um restore propriamente dito.



Vamos ver primeiro as tabelas e suas estatísticas de um usuário qualquer. Vou usar o SQL abaixo (e deixar a parte da tabela comentada pois vamos usar mais tarde).



COL OWNER FOR A10;

COL TABLE_NAME FOR A15;



SELECT &nbsp;

 owner,&nbsp;

 table_name,&nbsp;

 avg_row_len,&nbsp;

 blocks,&nbsp;

 empty_blocks,&nbsp;

 num_rows,&nbsp;

 TO_CHAR(last_analyzed, 'DD/MM/YYYY HH24:MI:SS') as last_analyzed

FROM &nbsp; &nbsp;

 DBA_TABLES

WHERE &nbsp;&nbsp;

 OWNER = UPPER(NVL('&amp;OWNER',OWNER));



OWNER &nbsp; &nbsp; &nbsp;TABLE_NAME &nbsp; &nbsp; &nbsp;AVG_ROW_LEN &nbsp; &nbsp; BLOCKS EMPTY_BLOCKS &nbsp; NUM_ROWS LAST_ANALYZED

---------- --------------- ----------- ---------- ------------ ---------- -------------------

EXEMPLO &nbsp; &nbsp;EXTCONFIG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;44 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 11 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;SERVER_STAT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;TMP_TOP_SESSION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;TOP_SESSION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;TOP_SQL_TEXT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;TOP_SQL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;LOG_TEMPO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;33 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 13/10/2016 23:00:13

EXEMPLO &nbsp; &nbsp;ESTATISTICA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;75 &nbsp; &nbsp; &nbsp; &nbsp;244 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;19539 12/10/2016 23:00:04

EXEMPLO &nbsp; &nbsp;DISPONIBILIDADE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;19 &nbsp; &nbsp; &nbsp; &nbsp; 20 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 26 13/10/2016 23:00:11

EXEMPLO &nbsp; &nbsp;BACKUP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 86 &nbsp; &nbsp; &nbsp; &nbsp;496 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;37974 10/10/2016 23:00:06

EXEMPLO &nbsp; &nbsp;INSTANCIA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; 28 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 13/10/2016 23:00:13

EXEMPLO &nbsp; &nbsp;ESQUEMA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;18 &nbsp; &nbsp; &nbsp; &nbsp; 13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; 1641 10/10/2016 23:00:05

EXEMPLO &nbsp; &nbsp;TABLESPACE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 24 &nbsp; &nbsp; &nbsp; &nbsp; 13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; 2550 28/09/2016 23:00:03

EXEMPLO &nbsp; &nbsp;HIST_ARMAZENAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;ARMAZENAMENTO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;SERVIDOR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 28 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 13/10/2016 23:00:13

EXEMPLO &nbsp; &nbsp;CONFIG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 27 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 10/09/2016 23:02:50



Ok, bem zuadas algumas estatísticas por sinal, bem antigas.



Vamos ver como forçar um "backup" das estatísticas antes da coleta. Depois coletamos as estatísticas e vamos retornar algumas.



Primeiro, faremos da forma manual. Vou rodar tudo como SYS:



1) Criar a tabela que irá armazenar a estatística

exec dbms_stats.create_stat_table(

&nbsp; &nbsp; &nbsp; ownname &nbsp;=&gt; 'EXEMPLO',

&nbsp; &nbsp; &nbsp; stattab &nbsp;=&gt; 'STAT_EXEMPLO',

&nbsp; &nbsp; &nbsp; tblspace =&gt; 'EXEMPLOTBS'

);



Onde:

ownname - Name of the schema.&nbsp;

stattab &nbsp; &nbsp; &nbsp;- Name of the table to create.&nbsp;

tblspace &nbsp; - Tablespace in which to create the stat tables. If none is specified, then they are created in the user's default tablespace.&nbsp;



2) Vamos agora exportar as estatísticas do schema

exec dbms_stats.export_schema_stats(

&nbsp; &nbsp; &nbsp; ownname =&gt; 'EXEMPLO',

&nbsp; &nbsp; &nbsp; stattab =&gt; 'STAT_EXEMPLO',

&nbsp; &nbsp; &nbsp; statown =&gt; 'EXEMPLO'

);



Onde:

ownname &nbsp;- Name of the schema.&nbsp;

stattab &nbsp; &nbsp; &nbsp;- User stat table identifier describing where to store the statistics.&nbsp;

statown &nbsp; &nbsp;- Schema containing stattab (if different than ownname).&nbsp;



-- Se quiser, pode verificar se todas as tabelas estão no backup das estatísticas.&nbsp;

select distinct c1 as tabelas from EXEMPLO.STAT_EXEMPLO where type ='T';



-- Como curiosidade, poderíamos salvar apenas a estatística de uma tabela:

exec dbms_stats.export_table_stats(

&nbsp; &nbsp; &nbsp; ownname =&gt; 'EXEMPLO',

&nbsp; &nbsp; &nbsp; tabname =&gt; 'MENSAGEM',

&nbsp; &nbsp; &nbsp; statown =&gt; 'EXEMPLO',

&nbsp; &nbsp; &nbsp; stattab =&gt; 'STAT_EXEMPLO',

&nbsp; &nbsp; &nbsp; cascade =&gt; true);



3) Vamos coletar as estatísticas de uma tabela qualquer

EXEC DBMS_STATS.GATHER_TABLE_STATS(

 OWNNAME=&gt;'EXEMPLO',

 TABNAME=&gt;'MENSAGEM',

 ESTIMATE_PERCENT=&gt;DBMS_STATS.AUTO_SAMPLE_SIZE,

 BLOCK_SAMPLE=&gt;FALSE,

 DEGREE=&gt;16,

 GRANULARITY=&gt;'AUTO',

 CASCADE=&gt;TRUE,

 NO_INVALIDATE=&gt;FALSE,

 METHOD_OPT=&gt;'FOR ALL COLUMNS SIZE AUTO'

);



4) Conferimos as estatísticas

OWNER &nbsp; &nbsp; &nbsp;TABLE_NAME &nbsp; &nbsp; &nbsp;AVG_ROW_LEN &nbsp; &nbsp; BLOCKS EMPTY_BLOCKS &nbsp; NUM_ROWS LAST_ANALYZED

---------- --------------- ----------- ---------- ------------ ---------- -------------------

EXEMPLO &nbsp; &nbsp; MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 86 &nbsp; &nbsp; &nbsp; &nbsp;622 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;40312 15/10/2016 00:31:00



Lembrando que a estatítica antigas estava com a data do dia 10/10.



5) Vamos restaurar a posição anterior dessa estatística então

exec dbms_stats.import_table_stats(

 ownname =&gt; 'EXEMPLO',

 stattab =&gt; 'STAT_EXEMPLO',

 tabname =&gt; 'MENSAGEM'

);



E olha que beleza, tudo restaurado como antes.



OWNER &nbsp; &nbsp; &nbsp;TABLE_NAME &nbsp; &nbsp; &nbsp;AVG_ROW_LEN &nbsp; &nbsp; BLOCKS EMPTY_BLOCKS &nbsp; NUM_ROWS LAST_ANALYZED

---------- --------------- ----------- ---------- ------------ ---------- -------------------

EXEMPLO &nbsp; &nbsp; MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 86 &nbsp; &nbsp; &nbsp; &nbsp;496 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;37974 10/10/2016 23:00:06



Ok, mas e se eu tivesse coletado do schema todo? Sem problemas.



1) Vamos coletar do schema todo. Lembrando que já criamos o backup de nosso schema.

exec dbms_stats.gather_schema_stats(

 OWNNAME=&gt;'EXEMPLO',

 ESTIMATE_PERCENT=&gt;DBMS_STATS.AUTO_SAMPLE_SIZE,

 BLOCK_SAMPLE=&gt;FALSE,

 DEGREE=&gt;16,

 GRANULARITY=&gt;'AUTO',

 CASCADE=&gt;TRUE,

 NO_INVALIDATE=&gt;FALSE,

 OPTIONS=&gt;'GATHER',

 METHOD_OPT=&gt;'FOR ALL COLUMNS SIZE AUTO'

);



2) Conferimos as estatísticas atuais.

OWNER &nbsp; &nbsp; &nbsp;TABLE_NAME &nbsp; &nbsp; &nbsp;AVG_ROW_LEN &nbsp; &nbsp; BLOCKS EMPTY_BLOCKS &nbsp; NUM_ROWS LAST_ANALYZED

---------- --------------- ----------- ---------- ------------ ---------- -------------------

EXEMPLO &nbsp; &nbsp;EXTCONFIG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;44 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 11 15/10/2016 00:40:27

EXEMPLO &nbsp; &nbsp;STAT_EXEMPLO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 108 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp;304 15/10/2016 00:40:27

EXEMPLO &nbsp; &nbsp;SERVER_STAT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 15/10/2016 00:40:27

EXEMPLO &nbsp; &nbsp;TMP_TOP_SESSION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 15/10/2016 00:40:28

EXEMPLO &nbsp; &nbsp;TOP_SESSION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 15/10/2016 00:40:28

EXEMPLO &nbsp; &nbsp;TOP_SQL_TEXT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 15/10/2016 00:40:28

EXEMPLO &nbsp; &nbsp;TOP_SQL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 15/10/2016 00:40:28

EXEMPLO &nbsp; &nbsp;LOG_TEMPO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;33 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 15/10/2016 00:40:27

EXEMPLO &nbsp; &nbsp;ESTATISTICA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;75 &nbsp; &nbsp; &nbsp; &nbsp;244 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;20132 15/10/2016 00:40:27

EXEMPLO &nbsp; &nbsp;DISPONIBILIDADE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;19 &nbsp; &nbsp; &nbsp; &nbsp; 20 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 26 15/10/2016 00:40:25

EXEMPLO &nbsp; &nbsp;BACKUP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 15/10/2016 00:40:25

EXEMPLO &nbsp; &nbsp;MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 86 &nbsp; &nbsp; &nbsp; &nbsp;622 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;40316 15/10/2016 00:40:27

EXEMPLO &nbsp; &nbsp;INSTANCIA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; 28 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 15/10/2016 00:40:27

EXEMPLO &nbsp; &nbsp;ESQUEMA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;18 &nbsp; &nbsp; &nbsp; &nbsp; 13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; 1766 15/10/2016 00:40:26

EXEMPLO &nbsp; &nbsp;TABLESPACE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 24 &nbsp; &nbsp; &nbsp; &nbsp; 20 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; 3269 15/10/2016 00:40:28

EXEMPLO &nbsp; &nbsp;HIST_ARMAZENAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 15/10/2016 00:40:27

EXEMPLO &nbsp; &nbsp;ARMAZENAMENTO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 15/10/2016 00:40:25

EXEMPLO &nbsp; &nbsp;SERVIDOR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 28 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 15/10/2016 00:40:27

EXEMPLO &nbsp; &nbsp;CONFIG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 27 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 15/10/2016 00:40:25




3) Restauramos o schema todo. Antes, deletamos todas as estatísticas do schema.



-- Delete

exec DBMS_STATS.delete_schema_stats(ownname=&gt;'EXEMPLO');



-- Import

exec DBMS_STATS.import_schema_stats(ownname=&gt;'EXEMPLO',stattab=&gt;'STAT_EXEMPLO',statown=&gt;'EXEMPLO');



4) E conferimos de novo.

OWNER &nbsp; &nbsp; &nbsp;TABLE_NAME &nbsp; &nbsp; &nbsp;AVG_ROW_LEN &nbsp; &nbsp; BLOCKS EMPTY_BLOCKS &nbsp; NUM_ROWS LAST_ANALYZED

---------- --------------- ----------- ---------- ------------ ---------- -------------------

EXEMPLO &nbsp; &nbsp;EXTCONFIG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;44 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 11 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;STAT_EXEMPLO

EXEMPLO &nbsp; &nbsp;SERVER_STAT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;TMP_TOP_SESSION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;TOP_SESSION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;TOP_SQL_TEXT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;TOP_SQL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;LOG_TEMPO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;33 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 13/10/2016 23:00:13

EXEMPLO &nbsp; &nbsp;ESTATISTICA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;75 &nbsp; &nbsp; &nbsp; &nbsp;244 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;19539 12/10/2016 23:00:04

EXEMPLO &nbsp; &nbsp;DISPONIBILIDADE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;19 &nbsp; &nbsp; &nbsp; &nbsp; 20 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 26 13/10/2016 23:00:11

EXEMPLO &nbsp; &nbsp;BACKUP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 86 &nbsp; &nbsp; &nbsp; &nbsp;496 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;37974 10/10/2016 23:00:06

EXEMPLO &nbsp; &nbsp;INSTANCIA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; 28 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 13/10/2016 23:00:13

EXEMPLO &nbsp; &nbsp;ESQUEMA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;18 &nbsp; &nbsp; &nbsp; &nbsp; 13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; 1641 10/10/2016 23:00:05

EXEMPLO &nbsp; &nbsp;TABLESPACE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 24 &nbsp; &nbsp; &nbsp; &nbsp; 13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; 2550 28/09/2016 23:00:03

EXEMPLO &nbsp; &nbsp;HIST_ARMAZENAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;ARMAZENAMENTO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 10/09/2016 23:02:50

EXEMPLO &nbsp; &nbsp;SERVIDOR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; 28 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 13/10/2016 23:00:13

EXEMPLO &nbsp; &nbsp;CONFIG &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 27 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 10/09/2016 23:02:50



A tabela STAT_EXEMPLO está sem estatística pois quando criamos o backup ela ainda não existia.

Veja que são procedimentos extremamente simples e que, confesso, geralmente eu não executa. Até o dia que eu precisei e o backup automático não estava disponível.



Certo, tudo ok até aqui e tranquilo né. Esse é o método que eu gosto de utilizar pois tenho controle total na operação.



Ahhh, só lembrando. Se por acaso você tem as estatísticas em uma área de homologação que se mostra mais eficaz que as estatísticas na produção, como eu poderia migrar essas estatísticas?



Simples meu amigo, como o nosso querido amigo exp/imp (ou expdp/impdp se preferir).



exp \'/ as sysdba\' FILE=estat.dmp LOG=exp_estat.log TABLES="exemplo.STAT_EXEMPLO"


imp \'/ as sysdba\' FILE=estat.dmp LOG=imp_estat.log TABLES="exemplo.STAT_EXEMPLO" ignore=Y



E agora é só executar os procedimentos listados acima.



Beleza, e se eu não fiz o export das estatísticas. Não tem problema, o Oracle faz um backup dessas estatísticas e retém por um tempo.

Vamos utilizar o backup automático do Oracle então para ver como é. 



1) Vamos verificar a retenção da coleta (por default, 31 dias) e a data mais antiga disponível para restore.



-- Default da retenção

select dbms_stats.get_stats_history_retention from dual;


GET_STATS_HISTORY_RETENTION

---------------------------

    31



-- Coleta mais antiga disponível


select dbms_stats.get_stats_history_availability from dual;


GET_STATS_HISTORY_AVAILABILITY

----------------------------------------------------------------

13-SEP-16 11.18.16.343981000 PM -03:00



2) Vou coletar novamente as estatísticas do schema todo. E vou consultar a tabela DBA_TAB_STATS_HISTORY



select table_name, stats_update_time from DBA_TAB_STATS_HISTORY where table_name='MENSAGEM' order by 2;



TABLE_NAME &nbsp; &nbsp; &nbsp;STATS_UPDATE_TIME

--------------- ------------------------------------

MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp;25-SEP-16 11.00.05.468877 PM -03:00

MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp;10-OCT-16 11.00.06.423578 PM -03:00

MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp;15-OCT-16 12.31.00.454985 AM -03:00

MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp;15-OCT-16 12.34.24.597019 AM -03:00

MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp;15-OCT-16 12.39.30.145758 AM -03:00

MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp;15-OCT-16 12.40.27.476614 AM -03:00

MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp;15-OCT-16 12.42.53.868798 AM -03:00

MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp;15-OCT-16 12.42.58.278735 AM -03:00



Essas acima são as coletas disponíveis para restore. E lembre-se como está a tabela nesse momento:


OWNER &nbsp; &nbsp; &nbsp;TABLE_NAME &nbsp; &nbsp; &nbsp;AVG_ROW_LEN &nbsp; &nbsp; BLOCKS EMPTY_BLOCKS &nbsp; NUM_ROWS LAST_ANALYZED

---------- --------------- ----------- ---------- ------------ ---------- -------------------

EXEMPLO &nbsp; &nbsp; MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 86 &nbsp; &nbsp; &nbsp; &nbsp;622 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;40324 15/10/2016 01:01:38



3) Vamos restaurar a estatística da tabela MENSAGEM, de "25-SEP-16 11.00.05.468877 PM -03:00".



exec DBMS_STATS.delete_table_stats(ownname=&gt;'EXEMPLO',tabname=&gt;'MENSAGEM');


exec dbms_stats.restore_table_stats('EXEMPLO', 'MENSAGEM','25-SEP-16 11.00.05.468877 PM -03:00');



4) E está lá, a estatística do dia 25/09.



OWNER &nbsp; &nbsp; &nbsp;TABLE_NAME &nbsp; &nbsp; &nbsp;AVG_ROW_LEN &nbsp; &nbsp; BLOCKS EMPTY_BLOCKS &nbsp; NUM_ROWS LAST_ANALYZED

---------- --------------- ----------- ---------- ------------ ---------- -------------------

EXEMPLO &nbsp; &nbsp; MENSAGEM &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 86 &nbsp; &nbsp; &nbsp; &nbsp;496 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp;29338 25/09/2016 23:00:05





Para verificar todas as operações realizadas na tabela, basta consultar a DBA_OPTSTAT_OPERATIONS .










col OPERATION&nbsp; for a20

col TARGET&nbsp; &nbsp;&nbsp; for a20

col START_TIME&nbsp;for a40

col END_TIME&nbsp;  for a40

col STATUS     for a10

col JOB_NAME   for a10



select&nbsp;

&nbsp; &nbsp; ID,

&nbsp; &nbsp; OPERATION,

&nbsp; &nbsp; TARGET,

&nbsp; &nbsp; START_TIME,

&nbsp; &nbsp; END_TIME,

&nbsp; &nbsp; STATUS

from&nbsp;

&nbsp; &nbsp; DBA_OPTSTAT_OPERATIONS&nbsp;

where&nbsp;

&nbsp; &nbsp; target like '%MENSAGEM%'

ORDER BY&nbsp;

&nbsp; &nbsp; START_TIME;



ID OPERATION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TARGET &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; START_TIME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; END_TIME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; STATUS

---------- ---------------------- -------------------- ---------------------------------------- ---------------------------------------- ----------

2056 gather_table_stats &nbsp; &nbsp; SISMON.MENSAGEM &nbsp; &nbsp; &nbsp;15-OCT-16 12.30.58.665379 AM -03:00 &nbsp; &nbsp; &nbsp;15-OCT-16 12.31.02.537099 AM -03:00 &nbsp; &nbsp; &nbsp;COMPLETED

2057 import_table_stats &nbsp; &nbsp; SISMON.MENSAGEM &nbsp; &nbsp; &nbsp;15-OCT-16 12.34.23.087547 AM -03:00 &nbsp; &nbsp; &nbsp;15-OCT-16 12.34.25.265047 AM -03:00 &nbsp; &nbsp; &nbsp;COMPLETED

2069 delete_table_stats &nbsp; &nbsp; SISMON.MENSAGEM &nbsp; &nbsp; &nbsp;15-OCT-16 01.09.41.084232 AM -03:00 &nbsp; &nbsp; &nbsp;15-OCT-16 01.09.41.132820 AM -03:00 &nbsp; &nbsp; &nbsp;COMPLETED

2072 restore_table_stats &nbsp; &nbsp;SISMON.MENSAGEM &nbsp; &nbsp; &nbsp;15-OCT-16 01.13.00.186013 AM -03:00 &nbsp; &nbsp; &nbsp;15-OCT-16 01.13.00.219791 AM -03:00 &nbsp; &nbsp; &nbsp;COMPLETED

2073 restore_table_stats &nbsp; &nbsp;SISMON.MENSAGEM &nbsp; &nbsp; &nbsp;15-OCT-16 01.13.52.145885 AM -03:00 &nbsp; &nbsp; &nbsp;15-OCT-16 01.13.52.178049 AM -03:00 &nbsp; &nbsp; &nbsp;COMPLETED

É isso pessoal. Espero que ajude.

Abraço

Mario