统计信息的备份恢复导入导出

Question:  I want to understand when to export and import by dbms_stats statistics and learn when it is a good idea to export and import statistics.

Answer:  Importing and exporting statistics for the CBO and the systems stats (external system statistics for CPU, I/O. etc) and useful in a variety of areas:

  • Export production into test to make test systems “look like” large systems for execution plan generation”.
  • Export/imports can be used to control execution plans by “freezing execution plans”.
  • Statistics are used as a backup before re-analyzing a schema.
  • System stats can be moved to a smaller server to make it appear as if Oracle is executing on a large fast server.

You can batch together entire import export job under these scenarios:

-System stats:  When migrating to a new server, you can export the old system statistics to ensure consistent execution plans until you are ready to use the "real" system stats.

- Systems reverse:  Conversely, you can migrate system stats from production to test to make a tiny server appear to be a larger server.  This will not improve SQL execution speed, but developers will see the same execution plans that they would see in production:

- Backup stats:  Before making any production change to the CBO stats with dbms_stats, take a full schema backup and an backup of your dbms_stats system stats.  Remember, the primary reason for re-analyzing stats is to change SQL execution plans.

For example, here we export production table stats and backport them to the test database to make it appear to be a larger table:

exec dbms_stats.create_stat_table ( ownname => user , stattab => ‘temp_stat‘ ) ;

exec dbms_stats.export_table_stats ( ownname => user , stattab => ‘temp_stat‘, tabname => ‘mytable‘, statid => ‘stats03252011‘) ;

FTP stats to to new database by exporting the table temp_stat table

exec dbms_stats.import_table_stats ( ownname => user , stattab => ‘temp_stat‘, tabname => ‘customer_fact‘ )

Oracle recommends that you collect and save CBO statistics and swap them whenever you see a major change in workloads, such as a database that runs in OLTP mode during the day and processes batch jobs at night.  The dbms_stats export and import utilities are perfect for matching your statistics to a changing workload.

You can use the Oracle dbms_stats and export utilities to migrate schema statistics from your PROD instance to your TEST instance, so that your developers will be able to do more-realistic execution-plan tuning of new SQL before it‘s migrated into PROD.

Here are the steps:

Step 1: Create the stats_table:

exec dbms_stats.create_stat_table(
   ownname => ‘SYS‘, 
   stattab => ‘prod_stats‘, -
   tblspace => ‘SYSTEM‘); 
Step 2: Gather the statistics with gather_system_stats.  In this dbms_stats example, we compute histograms on all indexed columns:

DBMS_STATS.gather_schema_stats(
   ownname=>’<schema>’,
   estimate_percent=>dbms_stats.auto_sample_size
   cascade=>TRUE,
   method_opt=>’FOR ALL COLUMNS SIZE AUTO’)

Step 3: Export the stats to the prod_stats table using export_system_stats::

exec dbms_stats.export_schema_stats(
   ownname => ‘SYS‘, 
   stattab => ‘prod_stats‘);

Step 4: Export the stats to the prod_stats table using exp:

exp scott/tiger file=prod_stats.dmp log=stats.log tables=prod_stats rows=yes

Step 5: copy the export file (e.g. FTP) over to the production server:

ftp -i prodserv . . .

Step 6: Backup the existing production statistics:

exec dbms_stats.create_stat_table(
   ownname => ‘SYS‘, 
   stattab => ‘test_stats‘, -
   tblspace => ‘SYSTEM‘);

DBMS_STATS.gather_schema_stats(
   ownname=>’<schema>’,
   estimate_percent=>dbms_stats.auto_sample_size
   cascade=>TRUE,
   method_opt=>’FOR ALL COLUMNS SIZE AUTO’)

Step 7: Delete the existing production schema stats

exec dbms_stats.delete_schema_stats(ownname=>‘<schema>’);

Step 8:  Import the stats:

EXEC DBMS_STATS.import_schema_stats(‘TEST’,‘STATS_TABLE’,NULL,’TEST’);

Step 9: We can now use the import_system_stats procedure in Oracle dbms_stats to overlay the existing CBO statistics from the smaller TEST instance:

dbms_stats.import_system_stats(‘STATS_TO_MOVE‘);

 Simple Usage for DBMS_STATS:

Using procedures in DBMS_STATS package one can backup statistics and restore them.Assumes the user scott already has access to execute DBMS_STATS and using “GRANT EXECUTE ON dbms_stats TO scott;” as sysdba one can grant execute access to DBMS_STATS.

– create table to backup statistics, ownname is statistics table owner and stattab is statistics table name
SQL> execute dbms_stats.create_stat_table(ownname= ‘scott‘, stattab= ‘backup_stats‘);

– procedure to export statistics, exports statistics scott.test into scott.backup_stats, cascade=>true means it will export index statistics too
SQL> exec dbms_stats.export_table_stats(ownname=>‘scott‘, tabname=>‘test‘, statown=>‘scott‘, stattab=>‘backup_stats‘, cascade=>true);

– import table stats
SQL> exec dbms_stats.import_table_stats(ownname=>‘scott‘, tabname=>‘test1‘, statown=>‘scott‘, stattab=>‘backup_stats‘, cascade=>true);

– drop statistics table
SQL> execute dbms_stats.drop_stat_table(ownname= ‘scott‘, stattab= ‘backup_stats‘);

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

10G中可以使用dbms_stats.restore_table_stats恢复表老的统计信息,这功能还是挺不错的。

因为统计信息的变化有时会引起某些语句执行计划变差,这时恢复原有统计信息,常常会有效果。当然分析前备份老的统计信息,始终是个好习惯。

查询统计信息保存时间,当然这个时间不一定能保证:

SQL> select DBMS_STATS.GET_STATS_HISTORY_RETENTION from dual;
 
GET_STATS_HISTORY_RETENTION
---------------------------
                         31

也可以通过execute DBMS_STATS.ALTER_STATS_HISTORY_RETENTION (45) 来修改这个保存时间。

查询统计信息能恢复到哪个时间点:

SQL> select DBMS_STATS.GET_STATS_HISTORY_AVAILABILITY from dual;
 
GET_STATS_HISTORY_AVAILABILITY
----------------------------------------------------------------------
20-NOV-11 10.08.13.843271000 PM +08:00

小测试:
当前统计信息:

SQL> SELECT COLUMN_NAME,NUM_DISTINCT,NUM_BUCKETS,
  2         HISTOGRAM,DENSITY,LOW_VALUE,
  3         HIGH_VALUE,LAST_ANALYZED
  4    FROM DBA_TAB_COL_STATISTICS a
  5   WHERE a.TABLE_NAME = ‘T1‘
  6     and a.owner = ‘SYS‘;
 
COLUMN_NAM NUM_DISTINCT NUM_BUCKETS HISTOGRAM     DENSITY LOW_VALUE       HIGH_VALUE      LAST_ANALYZED
---------- ------------ ----------- ---------- ---------- --------------- --------------- ----------------
ID               108046         100 HEIGHT BAL .000015395 C3020104        C30C512D        2011-12-22 14:53
                                    ANCED
 
NAME               7775         100 HEIGHT BAL .001328021 2F3130303065386 73756E2F7574696 2011-12-22 14:53
                                    ANCED                 4315F4C696E6B65 C2F427564646869
                                                          64486173684D617 737443616C656E6
                                                          056616C75654974 46172

恢复到分析之前:

SQL> select to_timestamp (‘2011-12-22 14:52‘,‘yyyy-mm-dd hh24:mi‘) from dual;
 
TO_TIMESTAMP(‘2011-12-2214:52‘,‘YYYY-MM-DDHH24:MI‘)
---------------------------------------------------------------------------
22-DEC-11 02.52.00.000000000 PM
 
SQL> BEGIN
  2  DBMS_STATS.RESTORE_TABLE_STATS(
  3  ownname => ‘SYS‘,
  4  tabname => ‘T1‘,
  5  as_of_timestamp => ‘22-DEC-11 02.52.00.000000000 PM‘
  6  );
  7  END;
  8  /
 
PL/SQL procedure successfully completed.
 
SQL> SELECT COLUMN_NAME,NUM_DISTINCT,NUM_BUCKETS,
  2         HISTOGRAM,DENSITY,LOW_VALUE,
  3         HIGH_VALUE,LAST_ANALYZED
  4    FROM DBA_TAB_COL_STATISTICS a
  5   WHERE a.TABLE_NAME = ‘T1‘
  6     and a.owner = ‘SYS‘;
 
COLUMN_NAM NUM_DISTINCT NUM_BUCKETS HISTOGRAM     DENSITY LOW_VALUE       HIGH_VALUE      LAST_ANALYZED
---------- ------------ ----------- ---------- ---------- --------------- --------------- ----------------
ID               118045         100 HEIGHT BAL .000013915 C102            C30C512E        2011-12-22 14:45
                                    ANCED
 
NAME              11656         100 HEIGHT BAL  .00104712 2F3130343866633 73756E2F746F6F6 2011-12-22 14:45
                                    ANCED                 9355F5772617054 C732F747265652F
                                                          6F6B656E5772617 5768696C6553746
                                                          0546F6B656E496E 174656D656E74

http://www.dba-oracle.com/t_export_import_cbo_optimizer_statistics_dbms_stats.htm

http://blog.csdn.net/tianlesoftware/article/details/4668723

http://www.dba-oracle.com/oracle_tips_dbms_stats1.htm

http://www.dba-oracle.com/art_builder_histo.htm

http://www.datadisk.co.uk/html_docs/oracle/sql_optimization.htm

http://www.dba-oracle.com/t_dbms_stats_gather_fixed_object_stats.htm

统计信息的备份恢复导入导出,布布扣,bubuko.com

时间: 2024-10-19 15:32:56

统计信息的备份恢复导入导出的相关文章

5.非关系型数据库(Nosql)之mongodb:创建集合,备份与导入导出, 数据还原,导入导出

 1 固定集合 固定集合值得是事先创建而且大小固定的集合 2 固定集合的特征:固定集合很像环形队列,如果空间不足,最早文档就会被删除,为新的文档腾出空间.一般来说,固定集合适用于任何想要自动淘汰过期属性的场景,没有太多的操作限制. 3 创建固定集合使用命令: db.createCollection("collectionName",{capped:true,size:100000,max:100}); size:指定集合大小,单位为KB,max指定文档的数量 当指定文档数量上限时,

HBase备份之导入导出

我们在上一篇文章<HBase复制>中讲述了如何建立主/从集群,实现数据的实时备份.但是,HBase复制只对设置好复制以后的数据生效,也即,配置好复制之后插入HBase主集群的数据才能同步复制到HBase从集群中,而对之前的历史数据,采用HBase复制这种办法是无能为力的.本文介绍如何使用HBase的导入导出功能来实现历史数据的备份. 1)将HBase表数据导出到hdfs的一个指定目录中,具体命令如下: $ cd $HBASE_HOME/ $ bin/hbase org.apache.hadoo

mongodb 的备份恢复导入与导出

导入导出 use hndb; db.s.save({name:'李四',age:18,score:80,address:'郑州'}); db.s.save({name:'李三',age:8,score:50,address:'郑州'}); db.s.save({name:'张三',age:38,score:20,address:'天津'}); db.s.save({name:'赵六',age:28,score:70,address:'郑州'}); db.s.save({name:'王五五',ag

PostgreSQL逻辑备份恢复--pg_dump导出及psql导入案例

数据库导入导出是最常用的功能之一.PostgreSQL的备份工具可以使用pg_dump及pg_dumpall.可以通过pg_dump --help获取其使用方法.这里不对其做过多介绍.主要介绍在使用pg_dump及恢复过程中遇到的一个问题. 1.问题使用pg_dump -c导出后,通过psql导入时报下面的错误: ERROR: relation "t1" already exists ERROR: duplicate key value violates unique constrai

Aruba AC如何通过CLI备份及导入导出

通过CLI登录Aruba AC.Aruba AC是通过SSH加密的,因此建议使用PUTTY或者Xshell等支持SSH的终端工具.导入导出需要使用到tftp server,请先与本地建立tftp server. 1.配置文档备份 备份之前,先通过show boot查看默认运行文档,通过dir查看默认文档大小,如下图为12718 (Aruba3600) #copy startup-config tftp:192.168.0.1 startup-config20170418 此命令,即可将配置导出至

python | MongoDB备份恢复与导出导入

说明:本文所有操作均在win7下的MongoDB3.4.4版本中进行. 一.备份与恢复 1. 备份: mongodump -h IP --port 端口 -u 用户名 -p 密码 -d数据库 -o 文件路径 (将使用某个用户账号将某个mongodb服务器上的某个数据库备份到某个路径下) 如果无需指定用户,可以去掉-u和-p:如果导出本机的数据库,可以去掉-h:如果是默认端口,可以去掉--port: 如果想导出所有数据库,可以去掉-d.如果只想备份某个集合,应在”-d 数据库”后添加”-c 集合名

MongoDB备份恢复与导出导入

说明:本文所有操作均在win7下的MongoDB3.4.4版本中进行. 一.备份与恢复 1. 备份: mongodump -h IP --port 端口 -u 用户名 -p 密码 -d数据库 -o 文件路径 (将使用某个用户账号将某个mongodb服务器上的某个数据库备份到某个路径下) 如果无需指定用户,可以去掉-u和-p:如果导出本机的数据库,可以去掉-h:如果是默认端口,可以去掉--port: 如果想导出所有数据库,可以去掉-d.如果只想备份某个集合,应在"-d 数据库"后添加&q

Linux系统Mysql备份的导入导出

Mysql 备份 如果您使用的是帮助中心的一键环境配置,mysql的安装目录是/alidata/server/mysql,如果您将mysql安装到其他目录,您需要输入您mysql完整的安装路径. 单库备份您可以在服务器上执行/alidata/server/mysql/bin/mysqldump -uroot -p密码 数据库名 > 备份名称.sql mysqldump默认不会导出事件表,执行此命令会出现警告-- Warning: Skipping the data of table mysql.

数据库备份,导入导出操作

1.导出数据库:mysqldump -uroot -pdbpassword dbname>test.sql,需要注意的是不需要进入mysql再使用该命令. 例如:mysqldump -uroot -p*** servletdemo>test.sql 2.导出数据库中的某张表的结构:mysqldump -uroot -pdbpasswd -d dbname 表名>test.sql; 例如:mysqldump -uroot -p*** -d servletdemo task>db.sq