统计信息在oracle中是非常重要的模块,就好似打仗时的侦察兵,提前了解敌情,来帮助CBO更好地生成执行计划。下面是一些常用地查看统计信息的语句,希望能帮助到大家。
表统计信息查看
select t.owner, t.table_name, t.num_rows, t.sample_size, t.blocks, last_analyzed
from dba_tables t
where t.owner = 'SYS'
and t.table_name = 'test';
列统计信息查看
select owner,table_name,column_name,num_distinct,num_nulls,
num_buckets,histogram,last_analyzed
from dba_tab_col_statistics
where owner = 'SYS' and table_name = 'test';
索引统计信息查看
select owner,index_name,num_rows,blevel,leaf_blocks,clustering_factor,status,last_analyzed
from dba_indexes
where owner = 'SYS'
and index_name = 'test';
判断统计信息是否陈旧
select owner, table_name, object_type, stale_stats, num_rows, last_analyzed
from dba_tab_statistics
where table_name = 'TEST3'
and owner = 'SCOTT';
tips:stale_stats 若为 yes 则陈旧 , no不陈旧
查看统计信息过期的原因(回收高水位、经常要rebuild的索引都可以用这个判断):
当表中有超过10%的数据发生变化(insert、update、delete),就会引起统计信息过去
select table_owner, table_name, inserts, updates, deletes, timestamp
from all_tab_modifications
where table_owner = 'SCOTT'
AND table_name = 'TEST3';
查看表历史收集的统计信息情况
SELECT b.OWNER,
b.OBJECT_NAME TABLE_NAME,
TO_CHAR(a.ANALYZETIME, 'YYYY-MM-DD HH24:MI:SS') LAST_ANALYZETIME,
TO_CHAR(a.SAVTIME, 'YYYY-MM-DD HH24:MI:SS') CURR_ANALYZETIME,
a.ROWCNT
FROM SYS.WRI$_OPTSTAT_TAB_HISTORY a, DBA_OBJECTS b
WHERE a.OBJ# = b.OBJECT_ID
AND b.OBJECT_NAME ='ZB_WHOLE_ORDERS_KAFKA_DISPATCH' and b.OWNER='ZJOPEN'
ORDER BY a.OBJ#, a.SAVTIME;