Oracle 11g 增加了系统
dbms_stats.export_(database/schema/table)_stats 导出统计信息到创建的表中
dbms_stats.import_(database/schema/table)_stats 导入统计信息到系统中
来进行恢复统计信息!
对统计信息的备份和恢复包括如下等级
1)数据库的统计信息备份恢复(只能是sys用户下)
exec dbms_stats.create_stat_table('sys','stat_sys');
exec dbms_stats.export_database_stats('stat_sys');
exec dbms_stats.import_database_stats('stat_sys');
2)方案的统计信息备份恢复(只能在方案拥有者的用户下,yang为用户名)
exec dbms_stats.create_stat_table('yang','stat_2');
exec dbms_stats.export_schema_stats('yang','stat_2');
exec dbms_stats.import_schema_stats('yang','stat_2');
3)表的统计信息备份恢复(只能在表拥有者的用户下)
exec dbms_stats.create_stat_table('yang','stat_3');
exec dbms_stats.export_table_stats('yang','T1',null,'stat_3');
exec dbms_stats.import_table_stats('yang','T1',null,'stat_3');
下面针对用户级别做一个测试!
1 创建统计信息备份存放的表
www.linuxidc.com@linuxidc> exec dbms_stats.create_stat_table('yang','stat_4');
PL/SQL procedure successfully completed.
2 对用户yang 进行统计,并将统计信息导出到表里面!
www.linuxidc.com@linuxidc> begin
2 dbms_stats.gather_schema_stats(
3 ownname => 'yang',
4 estimate_percent => 100,
5 method_opt => 'for all columns size auto',
6 degree => 2);
7 end;
8 /
PL/SQL procedure successfully completed.
www.linuxidc.com@linuxidc> select table_name,last_analyzed from user_tables where table_name='T2';
TABLE_NAME LAST_ANALYZED
------------------------------ ------------------
T2 27-FEB-12
www.linuxidc.com@linuxidc> exec dbms_stats.export_schema_stats('yang','stat_4');
PL/SQL procedure successfully completed.
3 删除之前的统计信息并查询验证
www.linuxidc.com@linuxidc> exec dbms_stats.delete_schema_stats('yang');
PL/SQL procedure successfully completed.
www.linuxidc.com@linuxidc> select table_name,last_analyzed from user_tables where table_name='T2';
TABLE_NAME LAST_ANALYZED
------------------------------ ------------------
T2
4 将之前备份的统计信息从新导入
www.linuxidc.com@linuxidc> exec dbms_stats.import_schema_stats('yang','stat_4');
PL/SQL procedure successfully completed.
查询yang 用户下的t2表的统计分析时间!
www.linuxidc.com@linuxidc> select table_name,last_analyzed from user_tables where table_name='T2';
TABLE_NAME LAST_ANALYZED
------------------------------ ------------------
T2 27-FEB-12