重庆思庄Oracle、Redhat认证学习论坛

标题: 【统计信息】如何备份和还原统计信息 [打印本页]

作者: jiawang    时间: 2021-1-7 11:29
标题: 【统计信息】如何备份和还原统计信息
本帖最后由 jiawang 于 2021-1-7 18:30 编辑

一、介绍
备份和还原统计信息,是指利用存储过程创建一个模板表,将存储信息存储在模板表内。如果需要还原,则将模板表内的统计信息导出还原即可。
二、备份统计信息1.用户创建一个“统计信息表”

SQL> conn test_user/oracle
SQL> exec dbms_stats.create_stat_table('TEST_USER','STAT_TIMESTAMP');


2.传输统计信息到“统计信息表”
1)传输多表的统计信息到“统计信息表”
SQL> exec dbms_stats.export_table_stats(user,'TAB1',NULL,'STAT_TIMESTAMP');
SQL> exec dbms_stats.export_table_stats(user,'TAB2',NULL,'STAT_TIMESTAMP');
SQL> exec dbms_stats.export_table_stats(user,'TAB3',NULL,'STAT_TIMESTAMP');


2)传输整个schemas的统计信息
SQL> exec dbms_stats.export_schema_stats(user,'STAT_TIMESTAMP');

3)传输system统计信息到“统计信息表”
-- 检查system状态
SQL> select sname,pname,pval1 from sys.aux_stats$ where pval1 is not null;
-- 创建统计信息表
SQL> exec dbms_stats.create_stat_table(user,'STAT_TIMESTAMP');
-- 导出
SQL> exec dbms_stats.export_system_stats('STAT_TIMESTAMP');
-- 导入
SQL> exec dbms_stats.import_system_stats('STAT_TIMESTAMP');


3.还原统计信息

1)还原某个表的统计信息
SQL> exec dbms_stats.import_table_stats(NULL,'TAB1', NULL,'STAT_TIMESTAMP');
SQL> exec dbms_stats.import_table_stats(NULL,'TAB2', NULL,'STAT_TIMESTAMP');
SQL> exec dbms_stats.import_table_stats(NULL,'TAB3', NULL,'STAT_TIMESTAMP');

4.查询存储在STAT_TIMESTAMP表中的统计信息
SQL> select distinct c1 from STAT_TIMESTAMP where type ='T';
C1
------------------------------
TAB2
TAB1
TAB3
上面结果显示,在表STAT_TIMESTAMP中,存着TAB1,TAB2,TAB3三个表的统计信息。


5.还原所有表的统计信息
SQL> exec dbms_stats.import_schema_stats(user,'STAT_TIMESTAMP');

---- end ----






欢迎光临 重庆思庄Oracle、Redhat认证学习论坛 (http://bbs.cqsztech.com/) Powered by Discuz! X3.2