相关链接
Gathers statistics for all objects in the database
收集数据库中所有对象的统计信息
This procedure gathers statistics for all objects in the database.
此过程收集数据库中所有对象的统计信息。
The DBMS_STATS
package uses the constants shown in Table 142-1:
Table 142-1 DBMS_STATS Constants
Name 名称 | Type 类型 | Value 值 | Description 描述 |
---|---|---|---|
AUTO_CASCADE | BOOLEAN | NULL | Lets Oracle decide whether to collect statistics for indexes or not 让Oracle决定是否收集索引的统计信息 |
AUTO_DEGREE | NUMBER | 32768 | Lets 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_INVALIDATE | BOOLEAN | NULL | Lets Oracle decide when to invalidate dependent cursors 让Oracle决定何时使依赖游标失效 |
AUTO_SIMPLESIZE | NUMBER | 0 | Indicates that auto-sample size algorithms should be used 表示自动样本大小应该使用的算法 |
参数有default值:可以不传参,使用默认值
参数无default值:必须传参才可调用
DBMS_STATS.GATHER_DATABASE_STATS (
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,
gather_sys BOOLEAN DEFAULT TRUE,
no_invalidate BOOLEAN DEFAULT to_no_invalidate_type (
get_param('NO_INVALIDATE')),
obj_filter_list ObjectTab DEFAULT NULL);
DBMS_STATS.GATHER_DATABASE_STATS (
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,
gather_sys BOOLEAN DEFAULT TRUE,
no_invalidate BOOLEAN DEFAULT to_no_invalidate_type (
get_param('NO_INVALIDATE')),
obj_filter_list ObjectTab DEFAULT NULL);
1.如果是命令窗口就用exec 存储过程名,举个例子:
EXEC procedure;--procedure是存储过程名
2.如果是PL/SQL窗口就用 begin 存储过程名 end; 举个例子:
begin
procedure;--procedure是存储过程名
end;
3.如果是程序中调用就用 call 存储过程名 ,举个例子:
hibernateDao.excuteSqlUpdate("{Call proc_stuInfo()}");//存储过程proc_stuInfo
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);
gather_sys boolean := sys.diutil.int_to_bool(:gather_sys);
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);
-- 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_database_stats(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,
gather_sys => gather_sys,
no_invalidate => no_invalidate,
obj_filter_list => obj_filter_list);
end;
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);
gather_sys boolean := sys.diutil.int_to_bool(:gather_sys);
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);
-- Non-scalar parameters require additional processing
obj_filter_list sys.dbms_stats.objecttab;
begin
-- Call the procedure
sys.dbms_stats.gather_database_stats(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,
gather_sys => gather_sys,
no_invalidate => no_invalidate,
obj_filter_list => obj_filter_list);
end;
Table 142-39 GATHER_DATABASE_STATS Procedure Parameters
Ser 序号 | Parameter 参数名称 | Type 类型 | Default | IN / OUT | Note 参数说明 | Range 取值范围 |
---|---|---|---|---|---|---|
1 | estimate_percent | NUMBER | Y | IN | Percentage 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_SIZEdefault 默认值(取Oracle常量)。由oracle决定最佳取采样值,默认值可通过左侧介绍的四个存储过程进行修改。统计数据质量越好,CBO做出的决定越好。 NULL 全部分析,不采样。 |
2 | block_sample | BOOLEAN | Y | IN | Whether 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 |
3 | method_opt 《统计量收集Method_Opt参数使用(上)》 《统计量收集Method_Opt参数使用(下)》 | VARCHAR2 | Y | IN | Accepts: ● FOR ALL [INDEXED | HIDDEN] COLUMNS [size_clause] ● FOR COLUMNS [size clause] column [size_clause] [,column [size_clause]...] size_clause is defined as size_clause := SIZE {integer | REPEAT | AUTO | SKEWONLY} column is defined as column := column_name | extension name | extension - integer : Number of histogram buckets. Must be in the range [1,254]. - REPEAT : Collects histograms only on the columns that already have histograms. - AUTO : Oracle determines the columns on which to collect histograms based on data distribution and the workload of the columns.- SKEWONLY : Oracle determines the columns on which to collect histograms based on the data distribution of the columns.The default is FOR ALL COLUMNS SIZE AUTO .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.决定histograms信息是怎样被统计的 √ 决定哪些columns统计被收集 √ 决定哪些columns直方图被收集以及直方图包含多少个buckets | get_param(‘METHOD_OPT’)default = DEFAULT_METHOD_OPT = FOR ALL COLUMNS SIZE AUTO 由Oralce决定N的大小 for all columns 统计所有列的histograms。 for all indexed columns 统计所有有索引列的histograms。 for all hidden columns 统计你看不到列的histograms。 for all columns SIZE 10 所有列buckt数量为10, buck∈【1,254】 for all columns SIZE REPEAT 上次统计过的histograms。 for all columns SIZE AUTO 【默认】由oracle决定N的大小。 for all columns SIZE SKEWONLY 根据数据列的分布情况来判断直方图的生成。如果数据呈现出偏移倾斜的情况,会去生成直方图。 for columns size 10 owner owner列的NUM_BUCKET=10 for columns size 10 owner for columns size 5 object_name owner列的NUM_BUCKET=10 object_name列的NUM_BUCKET=5 for columns size 10 owner object_name object_id object_name 列的NUM_BUCKET=10 object_id列的NUM_BUCKET=10 … |
4 | degree | NUMBER | Y | IN | Degree 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并行处理》 正整数 容忍范围内,值越大运行时间越短。但值过大会抢占数据库资源,甚至任务卡死,根据实际情况选择。 |
5 | granularity 《granularity的实验》 《range分区以及子分区》 | VARCHAR2 | Y | IN | Granularity 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)的统计信息。 |
6 | cascade | BOOLEAN | Y | IN | Gather 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 不收集索引的统计信息。 |
7 | stattab | VARCHAR2 | Y | IN | User statistics table identifier describing where to save the current statistics. The statistics table is assumed to reside in the same schema as the object being analyzed, so there must be one such table in each schema to use this option. 存储统计信息的目标表名。 统计表被假定与被分析对象在相同的方案(schema)中,因此每个模式中必须有一个这样的表才能使用此选项。 | NULLdefault 如果指定参数为NULL,统计信息将直接更新到数据字典。 [Any Stattab] 指定要存储统计信息的表。 |
8 | statid | VARCHAR2 | Y | IN | Identifier (optional) to associate with these statistics within stattab statid表明stattab这个数据集的主键,可以理解为stattab的一个分区。 | NULLdefault 不指定分区。 [Any Statid] 指定要存储统计信息的表的分区。 |
9 | options | VARCHAR2 | Y | IN | Further 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 , objlist 和statown ;所有其他参数设置将被忽略。返回已处理对象的列表。GATHER STALE 收集通过查看* _tab_modify 视图确定的陈旧对象的统计信息。返回值为已过期对象的列表。GATHER EMPTY 收集当前没有统计信息的对象的统计信息。返回没有统计信息对象的列表。 LIST AUTO 返回要用 GATHER AUTO 处理对象的列表。 LIST STALE 返回要用 GATHER STALE 处理对象的列表。 LIST EMPTY 返回要用 GATHER EMPTY 处理对象的列表。 |
10 | objlist | OUT | IN | List of objects found to be stale or empty 发现已过时或为空的对象的列表 | 对于options 选项 除了GATHER 外,都会返回统计对象的列表 | |
11 | statown | VARCHAR2 | Y | IN | Schema containing stattab (if different from current schema)statown表明stattab在哪个方案(schema)下,如果statown=当前schema,则可以不指定此参数。 | NULLdefault [Any Schema] 指定要存储统计信息的方案。 |
12 | gather_sys | BOOLEAN | Y | IN | Gathers statistics on the objects owned by the 'SYS' user 收集关于“SYS”用户所拥有对象的统计信息 | TRUEdefault FALSE |
13 | no_invalidate | BOOLEAN | Y | IN | Does 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 依赖游标失效。 |
14 | obj_filter_list | ObjectTab | Y | IN | A 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.对统计对象进行筛选 √ 可以有多个条件,最终收集的对象至少满足其中一个条件 √ 单个过滤可以指定对象属性上的约束 √ 指定的的属性只不区分大小写,除非双引号 √ 属性值中允许使用通配符 | 具体使用语法不详 |
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参数指示包在收集新的统计信息之前备份指定表中的当前统计信息。
ORA-20000
: Insufficient privileges
权限不足
ORA-20001
: Bad input value
输入参数错误
To run this procedure, you need to have the SYSDBA
role or both ANALYZE ANY DICTIONARY
and ANALYZE ANY
system privileges.
调用这个过程,你有SYSDBA角色,或 ANALYZE ANY DICTIONARY + ANALYZE ANY SYSTEM 权限。
20/10/26
M
信息加载中,请等待
微信客服(速回)
微信客服(慢回)