优化器统计范围:
表统计; --行数,块数,行平均长度;all_tables:num_rows,blocks,avg_row_len;
列统计; --列中唯一值的数量(ndv),null值的数量,数据分布;
--dba_tab_columns:num_distinct,num_nulls,histogram;
索引统计;--叶块数量,等级,聚簇因子;
--dba_indexes:leaf_blocks,clustering_factor,blevel;
系统统计;--i/o性能与使用率;
--cpu性能与使用率;
--存储在aux_stats$中,需要使用dbms_stats收集,i/o统计在x$kcfio中;
-------------
analyze
-------------
需要使用analyze统计的统计:
使用list chained rows和validate子句;
收集空闲列表块的统计;
analyze table tablename compute statistics;
analyze index|cluster indexname estimate statistics;
analyze table tablename compute statistics
for table
for all [local] indexes
for all [indexed] columns;
analyze table tablename delete statistics
analyze table tablename validate ref update
analyze table tablename validate structure [cascade]|[into tablename]
analyze table tablename list chained rows [into tablename]
analyze 不适合做分区表的分析
----------------------
dbms_stats
----------------------
dbms_stats能良好地估计统计数据(尤其是针对较大的分区表),并能获得更好的统计结果,最终制定出速度更快的sql执行计划。
这个包的下面四个存储过程分别收集index、table、schema、database的统计信息:
dbms_stats.gather_table_stats 收集表、列和索引的统计信息;
dbms_stats.gather_schema_stats 收集schema下所有对象的统计信息;
dbms_stats.gather_index_stats 收集索引的统计信息;
dbms_stats.gather_system_stats 收集系统统计信息
dbms_stats.gather_dictionary_stats: 所有字典对象的统计;
dbms_stats.gather_dictionary_stats 其收集所有系统模式的统计
dbms_stats.delete_table_stats 删除表的统计信息
dbms_stats.delete_index_stats 删除索引的统计信息
dbms_stats.export_table_stats 输出表的统计信息
dbms_stats.create_state_table
dbms_stats.set_table_stats 设置表的统计
dbms_stats.auto_sample_size
统计收集的权限
==========================
必须授予普通用户权限
sys@oradb> grant execute_catalog_role to hr;
sys@oradb> grant connect,resource,analyze any to hr;
统计收集的时间考虑
==========================
当参数statistics_level设置为typical或者all,系统会在夜间自动收集统计信息。
查看系统自动收集统计信息的job:
select * from dba_scheduler_jobs where job_name = 'gather_stats_job';
也可以disable自动收集统计信息:
begin
dbms_scheduler.disable('gather_stats_job');
end;
使用手工统计
对所有更改活动中等的对象自动统计应该足够充分,由于自动统计收集在夜间进行,因此对于一些更新频繁的对象其统计可能已经过期。两种典型的对象:
高度变化的表在白天的活动期间被truncate/drop并重建;
块加载超过本身总大小10%的对象;
对于第一种对象可以使用以下两种方法:
1 将这些表上的统计设置为null,当oracle碰到没有统计的表时,将动态收集必要的统计作为查询优化的一部分;
动态收集特征由optimizer_dynamic_sampling控制,这个参数应该设置为大于等于2,默认为2。可以通过删除并锁住统计将统计设置为null:
dbms_stats.delete_table_stats('schema','table');
dbms_stats.lock_table_stats('schema','table');
2 将这些表上的统计设置为代表表典型状态的值。在表具有某个有代表性的值时收集统计,然后锁住统计;
由于夜间收集的统计未必适合于白天的负载,因此这些情况下使用手工收集比gather_stats_job更有效。
对于块加载,统计应该在加载后连忙收集,通常合并在加载语句的后面防止遗忘。
对于外部表,统计不能通过gather_database_stats,gather_schema_stats以及自动统计收集收集。因此需要使用gather_table_stats在单个表上收集统计,并且在外部表上不支持取样,estimate_percent应该被显示设置为null。
假如statistics_level设置为basic禁用了监控特征,自动统计收集将不会检测过期的统计,此时需要手工收集。
3 需要手工收集的另一个地方是系统统计,其不会自动收集。
对于固定表,如动态性能表,需要使用gather_fixed_objects_stats收集,这些表上的统计应该在数据库具有有代表性的活动后收集。
统计收集考虑
==========================
1 统计收集使用取样
不使用抽样的统计收集需要全表扫描并且排序整个表,抽样最小化收集统计的必要资源。
oracle推荐设置dbms_stats的estimate_percent参数为dbms_stats.auto_sample_size在达到必要的统计精确性的同时最大化性能。
2 并行统计收集
oracle推荐设置dbms_stats的degree参数为dbms_stats.auto_degree,该参数答应oracle根据对象的大小和并行性初始化参数的设置选择恰当的并行度。
聚簇索引,域索引,位图连接索引不能并行收集。
3 分区对象的统计收集
对于分区表和索引,dbms_stats可以收集单独分区的统计和全局分区,对于组合分区,可以收集子分区,分区,表/索引上的统计,分区统计的收集可以通过声明参数granularity。根据将优化的sql语句,优化器可以选择使用分区统计或全局统计,对于大多数系统这两种统计都是很重要的,oracle推荐将granularity设置为auto同时收集全部信息。
4 列统计和直方图
当在表上收集统计时,dbms_stats收集表中列的数据分布的信息,数据分布最基本的信息是最大值和最小值,但是假如数据分布是倾斜的,这种级别的统计对于优化器来说不够的,对于倾斜的数据分布,直方图通常用来作为列统计的一部分。
直方图通过method_opt参数声明,oracle推荐设置method_opt为for all columns size auto,使用该值时oracle自动决定需要直方图的列以及每个直方图的桶数。也可以手工设置需要直方图的列以及桶数。
假如在使用dbms_stats的时候需要删除表中的所有行,需要使用truncate代替drop/create,否则自动统计收集特征使用的负载信息以及restore_*_stats使用的保存的统计历史将丢失。这些特征将无法正常发挥作用。
5 确定过期的统计
对于那些随着时间更改的对象必须周期性收集统计,为了确定过期的统计,oracle提供了一个表监控这些更改,这些监控默认情况下在statistics_level为typical/all时启用,该表为user_tab_modifications。使用dbms_stats.flush_database _monitoring_info可以连忙反映内存中超过监控的信息。在options参数设置为gather stale or gather auto时,dbms_stats收集过期统计的对象的统计。
6 用户定义统计
在创建了基于索引的统计后,应该在表上收集新的列统计,这可以通过调用过程设置method_opt的for all hidden columns。
7 何时收集统计
对于增量更改的表,可能每个月/每周只需要收集一次,而对于加载后表,通常在加载脚本中增加收集统计的脚本。对于分区表,假如仅仅是一个分区有了较大改动,只需要收集一个分区的统计,但是收集整个表的分区也是必要的。
系统统计
==========================
系统统计描述系统硬件的特征,包括i/o和cpu。在选择执行计划时,优化器考虑查询所需的cpu和i/o代价。系统统计答应优化器更加精确的评价cpu和io代价,选择更好的查询计划。
使用dbms_stats.gather_system_stats收集系统统计,oracle推荐收集系统统计。收集系统统计需要dba权限。
收集的优化器系统统计包括:
cpuspeednw:代表无负载cpu速度,cpu速度为每秒钟cpu周期数;通过设置gathering_mode = noworkload或手工设置统计;单位millions/sec。
ioseektim:i/o查找时间=查找时间+延迟时间+os负载时间;通过设置gathering_mode = noworkload或手工设置统计;单位为ms。
iotfrspeed:i/o传输速度;通过设置gathering_mode = noworkload或手工设置统计;单位为bytes/ms.
cpuspeed:代表有负载cpu速度,cpu速度为每秒钟cpu周期数;通过设置gathering_mode =noworkload,interval, start|stop或手工设置统计;单位millions/sec。
maxthr:最大i/o吞吐量;通过设置gathering_mode =noworkload,interval, start|stop或手工设置统计;单位bytes/sec.
slavethr:服务i/o吞吐量是平均并行服务i/o吞吐量;通过设置gathering_mode = interval,start|stop或手工设置统计;bytes/sec.
sreadtim:随机读取单块的平均时间;通过设置gathering_mode =interval,start|stop或手工设置统计;单位为ms。
mreadtim:顺序读取多块的平均时间,通过设置通过设置gathering_mode = interval,start|stop或手工设置统计;单位为ms。
mbrc: 多块读平均每次读取的块数量;通过设置通过设置gathering_mode = interval,start|stop或手工设置统计;单位为blocks。
系统统计的重新收集不会导致当前的sql无效,只是所有的新sql语句使用新的统计。
oracle提供两个选项收集统计:负载统计;非负载统计。
负载统计
==========================
在负载窗口的开始运行dbms_stats.gather_system_stats(’start’),然后运行dbms_stats.gather_system_stats(’stop’)结束负载窗口。
运行dbms_stats.gather_system_stats(’interval’, interval=>n),n表示n分钟后系统统计收集结束。
运行dbms_stats.delete_system_stats()删除负载统计。
非负载统计
==========================
运行不带参数的dbms_stats.gather_system_stats()收集非负载统计,运行非负载统计时会有一定的i/o负载。在某些情况下,非负载统计的值可能会保持默认,此时需要使用dbms_stats.set_system_stats设置。
管理统计
==========================
转储先前版本的统计
使用restore过程转储先前版本的统计,这些过程使用一个时间戳作为参数,包含统计时间的视图包括:
1 dba_optstat_operations:其中包含了使用dbms_stats在模式/系统级别执行的统计操作;
2 *_tab_stats_history:包含了表统计更改的历史。
旧的统计定期刷新,根据dbms_stats的alter_stats_history_retention过程设置而定,默认为31天。
默认情况下,假如statistics_level为typical/all,自动刷新启用;否则需要使用purge_stat手工刷新。
其他转储与刷新相关的信息包括:
purge_stats: 手工刷新超过某个时间戳的旧统计;
get_stats_history_rentention: 得到当前历史统计保留值;
get_stats_history_availabilty: 得到可用的最旧的统计的时间戳。
转储的限制:
1 不能转储用户定义统计;
2 假如使用了analyze收集,旧的统计将无法转储。
导入/导出统计
==========================
导出统计前需要使用dbms_stats.create_stat_table创建一个统计表保留统计,在表创建后可以使用dbms_stats.export_*_stats导出统计到自定义表,这些统计可以使用dbms_stats.import_*_stats重新导入。
也可以使用imp/exp导到其他数据库。
转储统计与导入导出统计
使用转储的情况:
1 恢复旧版本的统计;
2 希望数据库管理统计历史的保留和刷新;
使用export/import_*_stats的情况:
1 实验各种值的不同情况;
2 移动统计到不同数据库;
3 保留统计数据更长的时间。
锁住表和模式的统计
==========================
一旦统计被锁住,将无法在更改这些统计直到被解锁。dbms_stat提供两个过程用于解锁,两个用于加锁:
1 lock_schema_stats;?¤lock_table_stats;
2 unlock_schema_stats;?¤unlock_table_stats;
设置统计
==========================
可以使用set_*_statistics设置表,索引,列,系统统计。
使用动态取样评价统计
==========================
动态取样的目的是通过为谓词选择性和表/索引统计确定更加精确的估计提高服务器性能,估计越精确产生的性能更好。
可以使用动态取样的情况:
1 在收集的统计不能使用或会导致严重的估计错误时估计单表的谓词选择性;
2 估计没有统计的表/索引的统计;
3 估计统计过期的表和索引的统计;
动态取样特征由参数optimizer_dynamic_sampling控制,默认级别为2。
动态取样的工作机制
主要的性能特征是编译时,oracle在编译时决定一个查询是否能通过取样获益,假如可以,将用递归sql随机扫描一小部分表块,然后应用相关的单表谓词评价谓词选择性。
使用动态取样的时间
使用动态取样将获益的情况:
1 可以发现更好的执行计划;
2 取样时间仅占总时间的一小部分;
3 查询将执行多次;
取样级别
==========================
范围从1..10
缺失统计处理
==========================
当oracle碰到丢失统计时,优化器动态必要的统计。在某些情况下,oracle无法执行动态取样,包括:远程表/外部表,此时将使用默认统计。
缺失统计时的表默认值:
1 cardinality:num_of_blocks * (block_size - cache_layer) / avg_row_len
2 average row length:100字节;
3 number of blocks:100或基于分区映射的实际值;
4 remote cardinality:2000行;
5 remote average row length:100字节;
缺失统计时的索引默认值:
levels:1
leaf blocks:25
leaf blocks/key:1
data blocks/key:1
distinct keys:100
clustering factor:800
gather_schema_stats
==========================
begin
dbms_stats.gather_schema_stats( wnname => 'scott',
ptions => 'gather auto',
estimate_percent => dbms_stats.auto_sample_size,
method_opt => 'for all columns size repeat',
degree => 15 );
end;
options参数使用4个预设的方法:
gather——重新分析整个架构(schema)。
gather empty——只分析目前还没有统计的表。
gather stale——只重新分析修改量超过10%的表(这些修改包括插入、更新和删除)。
gather auto——重新分析当前没有统计的对象,以及统计数据过期(变脏)的对象。类似于组合使用gather stale和gather empty。
注重,无论gather stale还是gather auto,都要求进行监视。
假如你执行一个alter table xxx monitoring命令,oracle会用dba_tab_modifications视图来跟踪发生变动的表。
这样一来,你就确切地知道,自从上一次分析统计数据以来,发生了多少次插入、更新和删除操作。
select * from sys.dba_tab_modifications where table_owner = 'scott';
使用alter table xxx monitoring命令来实现oracle表监视时,需要使用dbms_stats中的auto选项。
auto选项根据数据分布以及应用程序访问列的方式(例如通过监视而确定的一个列的工作量)
来创建直方图。使用method_opt=>’auto’类似于在dbms_stats的option参数中使用gather auto。
begin
dbms_stats.gather_schema_stats(ownname => 'scott',
estimate_percent => dbms_stats.auto_sample_size,
method_opt => 'for all columns size auto',
degree => 7);
end;
estimate_percent选项
以下estimate_percent参数是一种比较新的设计,它答应oracle的dbms_stats在收集统计数据时,自动估计要采样的一个segment的最佳百分比:
estimate_percent => dbms_stats.auto_sample_size
要验证自动统计采样的正确性,你可检视dba_tables sample_size列。一个有趣的地方是,在使用自动采样时,oracle会为一个样本尺寸选择5到20的百分比。记住,统计数据质量越好,cbo做出的决定越好。
method_opt选项
dbms_stats的method_opt参数尤其适合在表和索引数据发生变化时刷新统计数据。method_opt参数也适合用于判定哪些列需要直方图(histograms)。
某些情况下,索引内的各个值的分布会影响cbo是使用一个索引还是执行一次全表扫描的决策。例如,假如在where子句中指定的值的数量不对称,全表扫描就显得比索引访问更经济。
假如你有一个高度倾斜的索引(某些值的行数不对称),就可创建oracle直方图统计。但在现实世界中,出现这种情况的机率相当小。使用cbo时,最常见的错误之一就是在cbo统计中不必要地引入直方图。根据经验,只有在列值要求必须修改执行计划时,才应使用直方图。
为了智能地生成直方图,oracle为dbms_stats预备了method_opt参数。在method_opt子句中,还有一些重要的新选项,包括skewonly,repeat和auto:method_opt=>'for all columns size skewonly'
method_opt=>'for all columns size repeat'
method_opt=>'for all columns size auto'
skewonly选项会耗费大量处理时间,因为它要检查每个索引中的每个列的值的分布情况。
假如dbms_stat发现一个索引的各个列分布得不均匀,就会为那个索引创建直方图,帮助基于代价的sql优化器决定是进行索引访问,还是进行全表扫描访问。例如,在一个索引中,假定有一个列在50%的行中,那么为了检索这些行,全表扫描的速度会快于索引扫描。
--*************************************************************
-- skewonly option—detailed analysis
--
-- use this method for a first-time analysis for skewed indexes
-- this runs a long time because all indexes are examined
--*************************************************************
begin
dbms_stats.gather_schema_stats(ownname => 'scott',
estimate_percent => dbms_stats.auto_sample_size,
method_opt => 'for all columns size skewonly',
degree => 7);
end;
重新分析统计数据时,使用repeat选项,重新分析任务所消耗的资源就会少一些。使用repeat选项时,只会为现有的直方图重新分析索引,不再搜索其他直方图机会。定期重新分析统计数据时,你应该采取这种方式。
--**************************************************************
-- repeat option - only reanalyze histograms for indexes
-- that have histograms
--
-- following the initial analysis, the weekly analysis
-- job will use the “repeat” option. the repeat option
-- tells dbms_stats that no indexes have changed, and
-- it will only reanalyze histograms for
-- indexes that have histograms.
--**************************************************************
begin
dbms_stats.gather_schema_stats(ownname => 'scott',
estimate_percent => dbms_stats.auto_sample_size,
method_opt => 'for all columns size repeat',
degree => 7);
end;
oracle中关于表的统计信息是在数据字典中的,可以下sql查询到:
select table_name,num_rows,blocks,empty_blocks,avg_space,chain_cnt,avg_row_len,sample_size,last_analyzed
from dba_tables where wner = 'scott' ;
这是对命令与工具包的一些总结
1、对于分区表,建议使用dbms_stats,而不是使用analyze语句。
a) 可以并行进行,对多个用户,多个table
b) 可以得到整个分区表的数据和单个分区的数据。
c) 可以在不同级别上compute statistics:单个分区,子分区,全表,所有分区 ,但不收集聚簇统计
d) 可以倒出统计信息
e) 可以用户自动收集统计信息
2、dbms_stats的缺点
a) 不能validate structure
b) 不能收集chained rows, 不能收集cluster table的信息,这两个仍然需要使用analyze语句。
c) dbms_stats 默认不对索引进行analyze,因为默认cascade是false,需要手工指定为true
3、对于external table,analyze不能使用,只能使用dbms_stats来收集信息。
gather_table_stats
==========================
dbms_stats.gather_table_stats
(ownname varchar2,
tabname varchar2,
partname varchar2 default null,
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,
statown varchar2 default null,
no_invalidate boolean default to_no_invalidate_type(get_param('no_invalidate')),
stattype varchar2 default 'data',
force boolean default false);
参数说明:
ownname: 要分析表的拥有者
tabname: 要分析的表名.
partname: 分区的名字,只对分区表或分区索引有用.
estimate_percent:采样行的百分比,取值范围[0.000001,100],null为全部分析,不采样. 常量:dbms_stats.auto_sample_size是默认值,由oracle决定最佳取采样值.
block_sapmple:是否用块采样代替行采样.
method_opt: 决定histograms信息是怎样被统计的.method_opt的取值如下:
for all columns:统计所有列的histograms.
for all indexed columns:统计所有indexed列的histograms.
for all hidden columns:统计你看不到列的histograms
for columns size | repeat | auto | skewonly:
统计指定列的histograms.n的取值范围[1,254]; r
epeat上次统计过的histograms;
auto由oracle决定n的大小;
skewonly multiple end-points with the same value which is what we define by "there is skew in the data
degree: 设置收集统计信息的并行度.默认值为null.
granularity:granularity of statistics to collect ,only pertinent if the table is partitioned.
cascade: 是收集索引的信息.默认为falase.
stattab 指定要存储统计信息的表,statid假如多个表的统计信息存储在同一个stattab中用于进行区分.statown存储统计信息表的拥有者.以上三个参数若不指定,统计信息会直接更新到数据字典.
no_invalidate: does not invalidate the dependent cursors if set to true. the procedure invalidates the dependent cursors immediately if set to false.
force: 即使表锁住了也收集统计信息
例子:
execute dbms_stats.gather_table_stats(ownname => 'owner',
tabname => 'table_name' ,
estimate_percent => null ,
method_opt => 'for all indexed columns' ,
cascade => true);
gather_index_stats
==========================
begin
sys.dbms_stats.gather_index_stats (ownname => 'abc',
indname => 'idx_func_abc',
estimate_percent => 10,
degree => sys.dbms_stats.default_degree,
no_invalidate => false);
end;
---------------------------------------
10g自动收集统计信息
---------------------------------------
从10g开始,oracle在建库后就默认创建了一个名为gather_stats_job的定时任务,用于自动收集cbo的统计信息。
这个自动任务默认情况下在工作日晚上10:00-6:00和周末全天开启。
调用dbms_stats.gather_database_stats_job_proc收集统计信息。该过程首先检测统计信息缺失和陈旧的对象。然后确定优先级,再开始进行统计信息。
可以通过以下查询这个job的运行情况:
select * from dba_scheduler_jobs where job_name = 'gather_stats_job';
其实同在10点运行的job还有一个auto_space_advisor_job:
select job_name, last_start_date from dba_scheduler_jobs;
job_name last_start_date
------------------------------ ------------------------------------
auto_space_advisor_job 30-oct-08 10.00.01.463000 pm +08:00
gather_stats_job 30-oct-08 10.00.01.463000 pm +08:00
然而这个自动化功能已经影响了很多系统的正常运行,晚上10点对于大部分生产系统也并非空闲时段。
而自动分析可能导致极为严重的闩锁竞争,进而可能导致数据库hang或者crash。
所以建议最好关闭这个自动统计信息收集功能:
关闭及开启自动搜集功能,有两种方法,分别如下:
方法一:
exec dbms_scheduler.disable('sys.gather_stats_job');
exec dbms_scheduler.enable('sys.gather_stats_job');
方法二:
alter system set "_optimizer_autostats_job"=false scope=spfile;
alter system set "_optimizer_autostats_job"=true scope=spfile;
---------------------------------------
查看统计
---------------------------------------
表/索引/列上的统计
dba_tables
dba_object_tables
dba_tab_statistics
dba_tab_col_statistics
dba_tab_histograms
dba_indexes
dba_ind_statistics
dba_clusters
dba_tab_partitions
dba_tab_subpartitions
dba_ind_partitions
dba_ind_subpartitions
dba_part_col_statistics
dba_part_histograms
dba_subpart_col_statistics
dba_subpart_histograms
---------------------------------------
直方图统计
---------------------------------------
直方图的类型存储在*tab_col_statistics视图的histogram列上。
------------------------------------------------------------------------------
bde_last_analyzed.sql - verifies cbo statistics
------------------------------------------------------------------------------
bde_last_analyzed.sql verifies the cbo statistics in the data dictionary for all tables, indexes, and partitions. it also validates the statistics on tables and indexes owned by 'sys'.
the 5 generated reports bde_last_analyzed_xxx.html, present the total of tables and indexes analyzed per module and per date.
script. bde_last_analyzed.sql provided in this note can be used on any 8i, 9i, 10g, 11g or higher database, including oracle apps 11i and r12 instances
假如是erp数据库,则用apps连接,否则用其他任何sys权限用户连接都可以
#sqlplus /
sql> start bde_last_analyzed.sql
review spool output files bde_last_analyzed_xxx.html files. spool files get created on same directory from which this script. is executed. on nt, files may get created under $oracle_home/bin.
if some modules have not been analyzed, or they have but not recently, these apps objects must be analyzed using fnd_stats or coe_stats.sql if belonging to oracle apps. otherwise use dbms_stats.
if oracle apps, use corresponding concurrent program with an estimate of 10%, or execute equivalent fnd_stats procedure from sql*plus:
sql> exec fnd_stats.gather_schema_statistics('applsys'); where 'applsys' is the module (schema) that requires new statistics.
if only a few tables require to have their statistics gathered, use the corresponding concurrent program to gather stats by table, or execute equivalent fnd_stats procedure from sql*plus:
sql> exec fnd_stats.gather_table_stats('mrp','mrp_forecast_dates');
where 'mrp' is the schema owner, and 'mrp_forecast_dates' is the table name. this syntax is only for non-partitioned tables.
if any partitioned table requires its global stats being rebuilt, it is because at some point you gathered stats on the table using a granularity of partition. see second method below:
begin
dbms_stats.delete_table_stats(ownname => 'applsys', tabname => 'wf_item_activity_statuses');
fnd_stats.gather_table_stats (ownname => 'applsys', tabname => 'wf_item_activity_statuses',
granularity => 'default');
end;
/
once you fix your stats, be sure to always use the granularity of default for partitioned tables.
if you want to execute this bde_last_analyzed.sql script. against only one schema, modify def schema code line.
---------------------------------------
分区表的统计信息实例
---------------------------------------
oratea oracle的统计信息在执行sql的过程中扮演着非常重要的作用,而且oracle在表的各个层次都会有不同的统计信息,通过这些统计信息来描述表的,列的各种各样的统计信息。下面通过一个复合分区表来说明一些常见的和常见的统计信息。
sql>
create table test
partition by range(object_id)
subpartition by hash(object_type) subpartitions 4
(partition p1 values less than(10000),
partition p2 values less than(20000),
partition p3 values less than(30000),
partition p4 values less than(maxvalue))
as
select * from dba_objects;
表已创建。
sql>
begin
dbms_stats.gather_table_stats(ownname => 'scott',
tabname => 'test',
estimate_percent => 100,
block_sample => false,
method_opt => 'for all columns size 10',
granularity => 'all',
cascade => true);
end;
1,表级的统计信息
sql> select table_name,num_rows,blocks,empty_blocks,avg_space from user_tables where table_name = 'test';
table_name num_rows blocks empty_blocks avg_space
------------------------------ ---------- ---------- ------------ ----------
test 50705 788 0 0
2,表上列的统计信息
sql> select table_name,column_name,num_distinct,density from user_tab_columns where table_name = 'test';
table_name column_name num_distinct density
------------------------------ ------------------------------ ------------ ----------
test owner 25 .365014295
test object_name 30275 .000039205
test subobject_name 191 .015657993
test object_id 50705 .000019722
test data_object_id 4334 .000248075
test object_type 42 .271207855
test created 2305 .001608457
test last_ddl_time 2369 .001566737
test timestamp 2412 .001610251
test status 2 .000009861
test temporary 2 .000009861
test generated 2 .000009861
test secondary 2 .000009861
13 rows selected.
3,表上列的直方图信息
sql>
select table_name,column_name,endpoint_number,endpoint_value
from user_tab_histograms
where table_name = 'test'
and column_name = 'object_id';
table_name column_nam endpoint_number endpoint_value
---------- ---------- --------------- --------------
test object_id 0 2
test object_id 1 5160
test object_id 2 10587
test object_id 3 15658
test object_id 4 20729
test object_id 5 25800
test object_id 6 30870
test object_id 7 35940
test object_id 8 41089
test object_id 9 46821
test object_id 10 53497
4,分区的统计信息
sql>
select partition_name,num_rows,blocks,empty_blocks,avg_space
from user_tab_partitions
where table_name = 'test';
partition_name num_rows blocks empty_blocks avg_space
--------------- ---------- ---------- ------------ ----------
p1 9581 140 0 0
p2 9973 164 0 0
p3 10000 158 0 0
p4 21151 326 0 0
5,分区上列的统计信息
sql> select column_name,num_distinct,density,num_nulls
from user_part_col_statistics
where table_name = 'test'
and partition_name = 'p1';
column_name num_distinct density num_nulls
--------------- ------------ ---------- ----------
owner 7 .000052187 0
object_name 7412 .000156925 0
subobject_name 26 .47017301 9496
object_id 9581 .000104373 0
data_object_id 1765 .000664385 7780
object_type 34 .18494854 0
created 913 .001977449 0
last_ddl_time 994 .001882695 0
timestamp 982 .001928775 0
status 2 .000052187 0
temporary 2 .000052187 0
generated 2 .000052187 0
secondary 1 .000052187 0
6,分区上列的直方图信息
sql> select column_name,bucket_number,endpoint_value
from user_part_histograms
where table_name = 'test'
and partition_name = 'p1'
and column_name = 'object_id';
column_name bucket_number endpoint_value
--------------- ------------- --------------
object_id 0 2
object_id 1 1005
object_id 2 1963
object_id 3 2921
object_id 4 3888
object_id 5 4859
object_id 6 5941
object_id 7 6899
object_id 8 7885
object_id 9 8864
object_id 10 9999
7,子分区的统计信息
sql> select subpartition_name,num_rows,blocks,empty_blocks
from user_tab_subpartitions
where table_name = 'test'
and partition_name = 'p1';
subpartition_name num_rows blocks empty_blocks
------------------------------ ---------- ---------- ------------
sys_subp21 3597 50 0
sys_subp22 3566 52 0
sys_subp23 637 11 0
sys_subp24 1781 27 0
8,子分区上的列的统计信息
sql> select column_name,num_distinct,density
from user_subpart_col_statistics
where table_name = 'test'
and subpartition_name = 'sys_subp21';
column_name num_distinct density
--------------- ------------ ----------
owner 6 .000139005
object_name 3595 .000278319
subobject_name 4 .014285714
object_id 3597 .000278009
data_object_id 155 .006451613
object_type 8 .000139005
created 751 .002392334
last_ddl_time 784 .002302524
timestamp 768 .00235539
status 1 .000139005
temporary 2 .000139005
generated 2 .000139005
secondary 1 .000139005
9,子分区上的列的直方图信息
sql> select column_name,bucket_number,endpoint_value
from user_subpart_histograms
where table_name = 'test'
and subpartition_name = 'sys_subp21'
and column_name = 'object_id';
column_name bucket_number endpoint_value
--------------- ------------- --------------
object_id 0 208
object_id 1 1525
object_id 2 2244
object_id 3 2892
object_id 4 3252
object_id 5 4047
object_id 6 5238
object_id 7 6531
object_id 8 7661
object_id 9 8474
object_id 10 9998
我们对这个复合分区分析之后产生了上面这九种不同层次的统计信息。cbo想要得要一个高效的执行计划需要如此多的统计信息.
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/70109/viewspace-688245/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/70109/viewspace-688245/