ITPub博客

首页 > 数据库 > Oracle > 【实验】【analyze】分析特定用户的表和索引

【实验】【analyze】分析特定用户的表和索引

原创 Oracle 作者:OCMU联盟 时间:2009-08-29 20:35:23 0 删除 编辑
1.使用analyze方法对特定用户的表和索引进行分析脚本如下
ora10g@testdb183 /home/oracle$ cat analyze_schema.sql
set pagesize 6000
set heading off
spool analytab.sql
select 'analyze table '||table_name||' estimate statistics sample 20 percent;' from user_tables;
spool off
spool analyind.sql
select 'analyze table '||table_name||' estimate statistics sample 20 percent for all indexes;' from user_tables;
spool off
spool analyze.log
@analytab.sql
@analyind.sql
spool off

2.使用方法,进入到sqlplus界面
ora10g@testdb183 /home/oracle$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.3.0 - Production on Sat Aug 29 20:16:29 2009

Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP and Data Mining Scoring Engine options

3.连接到待分析的用户,这里是sec用户
sys@ora10g> conn sec/sec
Connected.

4.使用“@”符号调用上面的analyze_schema.sql脚本,执行过程如下(这个用户下只有两张表)
sec@ora10g> @analyze_schema.sql

analyze table T_PARENT estimate statistics sample 20 percent;
analyze table T_CHILD estimate statistics sample 20 percent;


analyze table T_PARENT estimate statistics sample 20 percent for all indexes;
analyze table T_CHILD estimate statistics sample 20 percent for all indexes;


Table analyzed.


Table analyzed.


Table analyzed.


Table analyzed.

sec@ora10g>

5.上面的脚本使用的是analyze方法进行分析的,analyze方法不支持“并行”操作,下面演示一下使用dbms_stats.gather_schema_stats方法使用4并行对sec用户的10%数据进行分析的过程。
sec@ora10g> exec dbms_stats.gather_schema_stats(OWNNAME=>'SEC',ESTIMATE_PERCENT=>10,DEGREE=>4,cascade=>true);

PL/SQL procedure successfully completed.

如果想对SEC用户中所有表使用DBMS_STATS.GATHER_TABLE_STATS方法进行分析,可以参考如下我写的一个语句(所有能节省DBA宝贵时间的尝试都应该被鼓励):
SET lin 300
SET head off
SELECT    'execute DBMS_STATS.GATHER_TABLE_STATS(ownname=>'
       || ''''
       || owner
       || ''''
       || ',tabname=>'
       || ''''
       || table_name
       || ''''
       || ',method_opt=>'
       || ''''
       || 'FOR ALL COLUMNS SIZE 254'
       || ''''
       || ',DEGREE=>4,cascade=>true);'
  FROM dba_tables
 WHERE dba_tables.owner = 'SEC';


6.小结
对表和索引的定期分析有利于Oracle的CBO得到正确的执行计划,提高系统的性能。是DBA日常维护中必做的内容。
analyze和dbms_stats两种方法都可以完成分析的任务,如果在资源可用,并且待分析数据较大的情况下可以考虑使用dbms_stats加并行的方法进行处理。

上面只是演示了一种非常简单的用法,更多的analyze方法请参考Oracle官方文档《ANALYZE》


更多的DBMS_STATS方法,请参考Oracle官方文档《103  DBMS_STATS》http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_stats.htm#CIHBIEII


-- The End --

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70034820/viewspace-2993981/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
OCM联盟( Oracle Certified Master Union)是由侯圣文(ACE总监)发起的一个公益组织,旨在为每一个OCM大师提供一个可以继续展示自己才华,技术和实力的平台。

注册时间:2007-05-27

  • 博文量
    799
  • 访问量
    8407002