2.1.1.2_11 Oracle数据字典 142-43 SYS.DBMS_STATS.gather_schema_stats


相关链接


一、Summary of GATHER_SCHEMA_STATS Procedure

Gathers statistics for all objects in a schema
收集方案所有对象的统计信息


二、GATHER_SCHEMA_STATS Procedure

This procedure gathers statistics for all objects in a schema.
此过程收集方案中所有对象的统计信息。


三、Constants 常量

The DBMS_STATS package uses the constants shown in Table 142-1:
Table 142-1 DBMS_STATS Constants

Name
名称
Type
类型
Value
Description
描述
AUTO_CASCADEBOOLEANNULLLets Oracle decide whether to collect statistics for indexes or not
让Oracle决定是否收集索引的统计信息
AUTO_DEGREENUMBER32768Lets Oracle select the degree of parallelism based on size of the object, number of CPUs and initialization parameters. For definition of default parallel degree, see “Degree of Parallelism” in Oracle Database VLDB and Partitioning Guide.
让Oracle根据对象的大小、cpu的数量和初始化参数选择并行度。有关默认并行度的定义,请参阅Oracle数据库VLDB和分区指南中的"并行度"。
AUTO_INVALIDATEBOOLEANNULLLets Oracle decide when to invalidate dependent cursors
让Oracle决定何时使依赖游标失效
AUTO_SIMPLESIZENUMBER0Indicates that auto-sample size algorithms should be used
表示自动样本大小应该使用的算法

四、GATHER_SCHEMA_STATS

4.1 Syntax 语法

参数有default值:可以不传参,使用默认值
参数无default值:必须传参才可调用

DBMS_STATS.GATHER_SCHEMA_STATS ( 
   ownname          VARCHAR2, 
   estimate_percent NUMBER   DEFAULT to_estimate_percent_type 
                                                (get_param('ESTIMATE_PERCENT')), 
   block_sample     BOOLEAN  DEFAULT FALSE, 
   method_opt       VARCHAR2 DEFAULT get_param('METHOD_OPT'),
   degree           NUMBER   DEFAULT to_degree_type(get_param('DEGREE')), 
   granularity      VARCHAR2 DEFAULT GET_PARAM('GRANULARITY'), 
   cascade          BOOLEAN  DEFAULT to_cascade_type(get_param('CASCADE')), 
   stattab          VARCHAR2 DEFAULT NULL, 
   statid           VARCHAR2 DEFAULT NULL, 
   options          VARCHAR2 DEFAULT 'GATHER', 
   objlist          OUT      ObjectTab,
   statown          VARCHAR2 DEFAULT NULL, 
   no_invalidate    BOOLEAN  DEFAULT to_no_invalidate_type (
                                     get_param('NO_INVALIDATE')),
  force             BOOLEAN DEFAULT FALSE,
  obj_filter_list  ObjectTab DEFAULT NULL);

or

DBMS_STATS.GATHER_SCHEMA_STATS ( 
   ownname          VARCHAR2, 
   estimate_percent NUMBER   DEFAULT to_estimate_percent_type 
                                                (get_param('ESTIMATE_PERCENT')), 
   block_sample     BOOLEAN  DEFAULT FALSE, 
   method_opt       VARCHAR2 DEFAULT get_param('METHOD_OPT'), 
   degree           NUMBER   DEFAULT to_degree_type(get_param('DEGREE')), 
   granularity      VARCHAR2 DEFAULT GET_PARAM('GRANULARITY'), 
   cascade          BOOLEAN  DEFAULT to_cascade_type(get_param('CASCADE')), 
   stattab          VARCHAR2 DEFAULT NULL, 
   statid           VARCHAR2 DEFAULT NULL, 
   options          VARCHAR2 DEFAULT 'GATHER', 
   statown          VARCHAR2 DEFAULT NULL, 
   no_invalidate    BOOLEAN  DEFAULT to_no_invalidate_type (
                                     get_param('NO_INVALIDATE'), 
   force            BOOLEAN DEFAULT FALSE,
   obj_filter_list  ObjectTab DEFAULT NULL);

4.2 执行存储过程

1.如果是命令窗口就用exec 存储过程名,举个例子:

EXEC  procedure;--procedure是存储过程名

2.如果是PL/SQL窗口就用 begin 存储过程名 end; 举个例子:

begin
  procedure;--procedure是存储过程名
end;

3.如果是程序中调用就用 call 存储过程名 ,举个例子:

hibernateDao.excuteSqlUpdate("{Call proc_stuInfo()}");//存储过程proc_stuInfo
  • plsql测试语句
declare
  -- Boolean parameters are translated from/to integers: 
  -- 0/1/null <--> false/true/null 
  block_sample boolean := sys.diutil.int_to_bool(:block_sample);
  cascade boolean := sys.diutil.int_to_bool(:cascade);
  no_invalidate boolean := sys.diutil.int_to_bool(:no_invalidate);
  gather_temp boolean := sys.diutil.int_to_bool(:gather_temp);
  gather_fixed boolean := sys.diutil.int_to_bool(:gather_fixed);
  force boolean := sys.diutil.int_to_bool(:force);
  -- Non-scalar parameters require additional processing 
  objlist sys.dbms_stats.objecttab;
  obj_filter_list sys.dbms_stats.objecttab;
begin
  -- Call the procedure
  sys.dbms_stats.gather_schema_stats(ownname => :ownname,
                                     estimate_percent => :estimate_percent,
                                     block_sample => block_sample,
                                     method_opt => :method_opt,
                                     degree => :degree,
                                     granularity => :granularity,
                                     cascade => cascade,
                                     stattab => :stattab,
                                     statid => :statid,
                                     options => :options,
                                     objlist => objlist,
                                     statown => :statown,
                                     no_invalidate => no_invalidate,
                                     gather_temp => gather_temp,
                                     gather_fixed => gather_fixed,
                                     stattype => :stattype,
                                     force => force,
                                     obj_filter_list => obj_filter_list);
end;

or

declare
  -- Boolean parameters are translated from/to integers: 
  -- 0/1/null <--> false/true/null 
  block_sample boolean := sys.diutil.int_to_bool(:block_sample);
  cascade boolean := sys.diutil.int_to_bool(:cascade);
  no_invalidate boolean := sys.diutil.int_to_bool(:no_invalidate);
  gather_temp boolean := sys.diutil.int_to_bool(:gather_temp);
  gather_fixed boolean := sys.diutil.int_to_bool(:gather_fixed);
  force boolean := sys.diutil.int_to_bool(:force);
  -- Non-scalar parameters require additional processing 
  obj_filter_list sys.dbms_stats.objecttab;
begin
  -- Call the procedure
  sys.dbms_stats.gather_schema_stats(ownname => :ownname,
                                     estimate_percent => :estimate_percent,
                                     block_sample => block_sample,
                                     method_opt => :method_opt,
                                     degree => :degree,
                                     granularity => :granularity,
                                     cascade => cascade,
                                     stattab => :stattab,
                                     statid => :statid,
                                     options => :options,
                                     statown => :statown,
                                     no_invalidate => no_invalidate,
                                     gather_temp => gather_temp,
                                     gather_fixed => gather_fixed,
                                     stattype => :stattype,
                                     force => force,
                                     obj_filter_list => obj_filter_list);
end;

4.3 Parameters 参数

Table 142-43 GATHER_SCHEMA_STATS Procedure Parameters

Ser
序号
Parameter
参数名称
Type
类型
DefaultIN / OUTNote
参数说明
Range
取值范围
1ownnameVARCHAR2INSchema to analyze (NULL means current schema)
要分析的案名(‘NULL’ 表示当前方案)
NULL
  当前方案。
[Any Schema]
  任意指定方案。
2estimate_percentNUMBERYINPercentage of rows to estimate (NULL means compute): The valid range is [0.000001,100]. Use the constant DBMS_STATS.AUTO_SAMPLE_SIZE to have Oracle determine the appropriate sample size for good statistics. This is the default.The default value can be changed using the SET_DATABASE_PREFS Procedure, SET_GLOBAL_PREFS Procedure, SET_SCHEMA_PREFS Procedure and SET_TABLE_PREFS Procedure.
这个参数是一个百分比值,它告诉分析包需要使用表中数据的多大比例来做分析。
理论上来讲,采样的数据越多,得到的信息就越接近于实际,CBO做出的执行计划就越优化,但是,采样越多,消耗的系统资源必然越多。 对系统的影响也越大。 所以对于这个值的设置,要根据业务情况来。 如果数据的直方图分布比较均匀,就可以使用默认值:AUTO_SAMPLE_SIZE,即让Oracle 自己来判断采样的比例。有时,特别是对于批量加载的表,我们可以预估表中的数据量,可以人工地设置一个合理的值。 一般,对于一个有1000万数据的表分区,可以把这个参数设置为0.000001。
DBMS_STATS.
AUTO_SAMPLE_SIZE
default
  默认值(取Oracle常量)。由oracle决定最佳取采样值,默认值可通过左侧介绍的四个存储过程进行修改。统计数据质量越好,CBO做出的决定越好。
NULL
  全部分析,不采样。
3block_sampleBOOLEANYINWhether or not to use random block sampling instead of random row sampling. Random block sampling is more efficient, but if the data is not randomly distributed on disk, then the sample values may be somewhat correlated. Only pertinent when doing an estimate statistics.
确定是否使用随机数据块采样代替随机行采样。随机数据块采样更快,但如果数据不是随机分布在磁盘上,那么样本值可能不准确。
《oracle的数据块》
TRUE
FALSEdefault
4method_opt
《统计量收集Method_Opt参数使用(上)》
《统计量收集Method_Opt参数使用(下)》
VARCHAR2YINAccepts:
 ● `FOR ALL [INDEXED
HIDDEN] COLUMNS [size_clause]</font><br> ● <font color=blue size=2>FOR COLUMNS [size clause] column [size_clause] [,column [size_clause]…]</font><br> <br>size_clauseis defined assize_clause := SIZE {integer
5degreeNUMBERYINDegree of parallelism. The default for degree is NULL. The default value can be changed using the SET_DATABASE_PREFS Procedure, SET_GLOBAL_PREFS Procedure, SET_SCHEMA_PREFS Procedure and SET_TABLE_PREFS Procedure. NULL means use the table default value specified by the DEGREE clause in the CREATE TABLE or ALTER TABLE statement. Use the constant DBMS_STATS.DEFAULT_DEGREE to specify the default value based on the initialization parameters. The AUTO_DEGREE value determines the degree of parallelism automatically. This is between 1 (serial execution) and DEFAULT_DEGREE (the system default value based on number of CPUs and initialization parameters) according to the size of the object. When using DEGREE=>NULL, DEGREE=>n, or DEGREE=>DBMS_STATS.DEFAULT_DEGREE, the current implementation of DBMS_STATS may use serial execution if the size of the object does not warrant parallel execution.
决定并行度(线程数),默认值为NULL
√ 根据业务情况选择,业务闲时可设置为-1
√ 一般来说,degree值越大执行越快,如果服务器主机只有一个CPU,并行进程可能会争用CPU,从而导致实际性能可能会下降。
√ degree一般不超过10就可以了,过大会导致任务卡死,根据实际情况选择
DBMS_STATS.
DEFAULT_DEGREE
default
  此值为默认值(取Oracle常量)。由oracle决定并行度,默认值可通过左侧介绍的四个存储过程进行修改。
  根据对象的大小,degree值介于1(串行)和DEFAULT_DEGREE(基于CPU数量和初始化参数系统默认值)之间。
NULL
  表示使用CREATE table或ALTER table语句中的DEGREE子句指定的表并行度默认值。《Oracle并行处理》
正整数
  容忍范围内,值越大运行时间越短。但值过大会抢占数据库资源,甚至任务卡死,根据实际情况选择。
6granularity
granularity的实验
range分区以及子分区
VARCHAR2YINGranularity of statistics to collect (only pertinent if the table is partitioned).
- ALL : Gathers all (subpartition, partition, and global) statistics
- AUTO :Determines the granularity based on the partitioning type. This is the default value.
- DEFAULT :Gathers global and partition-level statistics. This option is obsolete, and while currently supported, it is included in the documentation for legacy reasons only. You should use the ‘GLOBAL AND PARTITION’ for this functionality. Note that the default value is now ‘AUTO’.
- GLOBAL :Gathers global statistics
- GLOBAL AND PARTITION : Gathers the global and partition level statistics. No subpartition level statistics are gathered even if it is a composite partitioned object.
- PARTITION :Gathers partition-level statistics
- SUBPARTITION :Gathers subpartition-level statistics
决定统计数据的粒度,表分区时此参数才有效,默认值为AUTO
√ 理想情况下,对表的全局,分区,子分区都做分析,这样才能得到最充足的数据;
√ 通常来讲需要分区的表数据量较大,如果每增加一个分区都需要做一次全局分析的话,会消耗极大的系统资源;但是如果只对新加入的分区进行分析而不做全局分析,Oracle 在全局范围内的信息就会不准确,导致CBO 生成错误的执行计划
ALL
  收集统计数据范围有:全局级别(global),分区级别(partition),子分区级别(subpartition)
AUTOdefault
  根据分区类型确定粒度。这是默认值。
DEFAULT
  收集全局级别(global)和分区级别(partition)的统计信息。这个选项已经过时了,虽然目前受到支持,但仅由于遗留原因才将其包含在文档中。您应该使用GLOBAL AND PARTITION来实现此功能。注意,默认值现在是AUTO
GLOBAL
  收集全局级别(global)统计数据。
GLOBAL AND PARTITION
  收集全局级别(global)和分区级别(partition)的统计信息。即使是复合分区对象,也不会收集子分区级别(subpartition)的统计信息。
PARTITION
  收集分区级别(partition)的统计信息。
SUBPARTITION
  收集子分区级别(subpartition)的统计信息。
7cascadeBOOLEANYINGather statistics on the indexes as well. Using this option is equivalent to running the GATHER_INDEX_STATS Procedure on each of the indexes in the database in addition to gathering table and column statistics. Use the constant DBMS_STATS.AUTO_CASCADE to have Oracle determine whether index statistics to be collected or not. This is the default. The default value can be changed using the SET_DATABASE_PREFS Procedure, SET_GLOBAL_PREFS Procedure, SET_SCHEMA_PREFS Procedure and SET_TABLE_PREFS Procedure.
收集索引的统计信息
√ 默认值为Oracle常量 GATHER_INDEX_STATS => 让Oalce决定
√ 此选项为 TRUE 时,相当于在数据库中每个索引上运行 GATHER_INDEX_STATS 存储过程
DBMS_STATS.
AUTO_CASCADE
default
  默认值(取Oracle常量)。由oracle决定是否收集数据库中每个索引的统计信息,默认值可通过左侧介绍的四个存储过程进行修改。
TRUE
  收集索引的统计信息。
FALSE
  不收集索引的统计信息。
8stattabVARCHAR2YINUser statistics table identifier describing where to save the current statistics
存储统计信息的目标表名。
NULLdefault
  如果指定参数为NULL,统计信息将直接更新到数据字典。
[Any Stattab]
  指定要存储统计信息的表。
9statidVARCHAR2YINIdentifier (optional) to associate with these statistics within stattab
statid表明stattab这个数据集的主键,可以理解为stattab的一个分区。
NULLdefault
  不指定分区
[Any Statid]
  指定要存储统计信息的表的分区。
10optionsVARCHAR2YINFurther specification of which objects to gather statistics for:
- GATHER: Gathers statistics on all objects in the schema.
- GATHER AUTO: Gathers all necessary statistics automatically. Oracle implicitly determines which objects need new statistics, and determines how to gather those statistics. When GATHER AUTO is specified, the only additional valid parameters are stattab, statid, objlist and statown; all other parameter settings are ignored. Returns a list of processed objects.
- GATHER STALE: Gathers statistics on stale objects as determined by looking at the *_tab_modifications views. Also, return a list of objects found to be stale.
- GATHER EMPTY: Gathers statistics on objects which currently have no statistics. Return a list of objects found to have no statistics.
- LIST AUTO: Returns a list of objects to be processed with GATHER AUTO
- LIST STALE: Returns a list of stale objects as determined by looking at the *_tab_modifications views
- LIST EMPTY: Returns a list of objects which currently have no statistics
GATHERdefault
  收集方案下(schema)所有对象的统计信息。
GATHER AUTO
   由Oracle自动决定收集哪些必要的统计数据。当指定参数为GATHER AUTO时,附加有效参数只有stattab, statid, objliststatown;所有其他参数设置将被忽略。返回已处理对象的列表。
GATHER STALE
   收集通过查看* _tab_modify视图确定的陈旧对象的统计信息。返回值为已过期对象的列表。
GATHER EMPTY
   收集当前没有统计信息的对象的统计信息。返回没有统计信息对象的列表。
LIST AUTO
   返回要用 GATHER AUTO 处理对象的列表。
LIST STALE
   返回要用 GATHER STALE 处理对象的列表。
LIST EMPTY
   返回要用 GATHER EMPTY 处理对象的列表。

┄┄┄注意,无论gather stale还是gather auto,都要求进行监视。如果你执行一个alter table xxx monitoring命令,Oracle会用dba_tab_modifications视图来跟踪发生变动的表。
┄┄┄这样一来,你就确切地知道,自从上一次分析统计数据以来,发生了多少次插入、更新和删除操作。
11objlistObjectTabOUTList of objects found to be stale or empty
发现已过时或为空的对象的列表
对于options 选项 除了GATHER外,都会返回统计对象的列表
12statownVARCHAR2YINSchema containing stattab (if different than ownname)
statown表明stattab在哪个方案(schema)下,如果statown=ownname,则可以不指定此参数。
NULLdefault
  当前方案下。
[Any Schema]
  指定要存储统计信息的方案。
13no_invalidateBOOLEANYINDoes not invalidate the dependent cursors if set to TRUE. The procedure invalidates the dependent cursors immediately if set to FALSE. Use DBMS_STATS.AUTO_INVALIDATE. to have Oracle decide when to invalidate dependent cursors. This is the default. The default can be changed using the SET_DATABASE_PREFS Procedure, SET_GLOBAL_PREFS Procedure, SET_SCHEMA_PREFS Procedure and SET_TABLE_PREFS Procedure.DBMS_STATS.AUTO_INVALIDATEdefault
  默认值(取Oracle常量)。让Oracle决定何时使依赖游标失效。
TRUE
  依赖游标有效。
FALSE
  依赖游标失效。
14forceBOOLEANYINGather statistics on object even if it is locked
当这个参数的值为TRUE时,即使锁表也会强制收集索引统计信息。
TRUE
FALSE default
15obj_filter_listObjectTabYINA list of object filters. When provided, GATHER_DATABASE_STATS will gather statistics only on objects which satisfy at least one object filter in the list as needed. In a single object filter, we can specify the constraints on the object attributes. The attribute values specified in the object filter are case- insensitive unless double-quoted. Wildcard is allowed in the attribute values. Suppose non-NULL values s1, s2, … are specified for attributes a1, a2, … in one object filter. An object o is said to satisfy this object filter if (o.a1 like s1) and (o.a2 like s2) and … is true. See Applying an Object Filter List.
对统计对象进行筛选
√ 可以有多个条件,最终收集的对象至少满足其中一个条件
√ 单个过滤可以指定对象属性上的约束
√ 指定的的属性只不区分大小写,除非双引号
√ 属性值中允许使用通配符
具体使用语法见
附录1 Applying an Object Filter List

Most of the DBMS_STATS procedures include the three parameters statown, stattab, and statid. These parameters allow you to store statistics in your own tables (outside of the dictionary), which does not affect the optimizer. Therefore, you can maintain and experiment with sets of statistics.
大多数DBMS_STATS过程包括三个参数statown、stattab和statid。这些参数允许您将统计信息存储在自己的表中(在字典之外),这不会影响优化器。因此,您可以维护和试验统计数据集。
The stattab parameter specifies the name of a table in which to hold statistics, and it is assumed that it resides in the same schema as the object for which statistics are collected (unless the statown parameter is specified). You can create multiple tables with different stattab identifiers to hold separate sets of statistics.
stattab参数指定用于保存统计信息的表的名称,并且假设它与为其收集统计信息的对象驻留在同一个方案(用户)中(除非指定了statown参数)。您可以使用不同的stattab标识符创建多个表,以保存不同的统计数据集。
The statown, stattab, and statid parameters instruct the package to back up current statistics in the specified table before gathering new statistics.
statown、stattab和statid参数指示包在收集新的统计信息之前备份指定表中的当前统计信息。


4.4 Exceptions 异常

ORA-20000: Schema does not exist or insufficient privileges
      方案不存在或权限不足
ORA-20001: Bad input value
      输入参数错误


4.5 Usage Notes 使用方式

To invoke this procedure you must be owner of the table, or you need the ANALYZE ANY privilege. For objects owned by SYS, you need to be either the owner of the table, or you need the ANALYZE ANY DICTIONARY privilege or the SYSDBA privilege.    
调用这个过程,你必须是表的所有者,或 ANALYZE ANY 权限。
对于SYS拥有的对象,你需要是表的所有者,或 ANALYZE ANY DICTIONARY 权限或 SYSDBA 权限。

When you use a specific value for the sampling percentage, DBMS_STATS honors it except for when:
      ● The result is less than 2500 rows (too small a sample) and      
      ● The specified percentage is more than the certain percentage.
DBMS_STATS会按你指定的采样百分比值(estimate_percent参数)执行,除非:
      ●结果小于2500行(样本太小)  
      ●指定的百分比大于某个百分比。  

附录1 Applying an Object Filter List

The following example specifies that any table with a "T" prefix in the SAMPLE schema and any table in the SYS schema, if stale, will have statistics gathered upon it.
下面的示例指定,‘SAMPLE’模式中任何带‘“T”前缀的表以及‘SYS’模式中的任何表(如果过时)都将收集统计信息。

DECLARE
   filter_lst  DBMS_STATS.OBJECTTAB := DBMS_STATS.OBJECTTAB();
BEGIN
   filter_lst.extend(2);
   filter_lst(1).ownname := 'SH';
   filter_lst(1).objname := 'SALES';
   filter_lst(2).ownname := 'SH';
   filter_lst(2).objname := 'COSTS';
   DBMS_STATS.GATHER_SCHEMA_STATS(ownname=>'SH',obj_filter_list=>filter_lst);
 END;

20/10/27

M

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值