数据库优化之统计分析实战篇
Posted ITPUB
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库优化之统计分析实战篇相关的知识,希望对你有一定的参考价值。
-
你们开启了的每天收集统计信息,每天窗口运行正常吗? -
所有的表都使用oracle的自动窗口收集?大表是不是要考虑定制收集?分区表是不是要增量收集? -
在不影响业务的情况下,什么时间段收集统计信息? -
如何判断统计信息是否过期,如何判断哪些字段需要收集直方图,收集的规则是什么?
dbms_scheduler.set_attribute(name => 'TUESDAY_WINDOW', attribute => 'duration', value =>numtdosinterval(480,'minute'));
end;
/
dbms_scheduler.set_attribute(
name => 'TUESDAY_WINDOW',
attribute => 'window_priority',
value => 'HIGH');
END;
/
dbms_scheduler.disable(
name => 'TUESDAY_WINDOW');
dbms_scheduler.set_attribute(
name => 'TUESDAY_WINDOW',
attribute => 'repeat_interval',
value => 'freq=daily;byday=TUE;byhour=03;byminute=0;bysecond=0');
dbms_scheduler.enable(
name => 'TUESDAY_WINDOW');
END;
/
BEGIN
DBMS_STATS.GATHER_TABLE_STATS(ownname => 'XX',
tabname => 'XX',
estimate_percent => 30,
degree => 4,
no_invalidate => FALSE,
method_opt => 'for all columns size auto',
cascade=>TRUE
);
END;
cursor stale_table is
select owner, segment_name,
case when size_gb<0.5 then 30
when size_gb>=0.5 and size_gb<1 then 20
when size_gb >=1 and size_gb<5 then 10
when size_gb>=5 and size_gb<10 then
when size_gb>=10 then 1
end as percent,
8 as degree
from (select owner,segment_name,sum(bytes/1024/1024/1024)size_gb
from dba_segments where owner='' AND SEGMENT_NAME IN
(SELECT /*+ UNNEST */ DISTINCT TABLE_NAME FROM DBA_TAB_STATISTICS WHERE
(LAST_ANALYZED IS NULL OR STALE_STATS='YES') AND OWNER='')
GROUP BY OWNER,SEGMENT_NAME);
BEGIN
FOR STALE IN STALE_TABLE LOOP
DBMS_STATS.GATHER_TABLE_STATS(OWNNAME =>STALE.OWNER,
TABNAME=>STALE.SEGMENT_NAME,
ESTIMATE_PERCENT =>STALE.PERCENT,
METHOD_OPT =>'for all colums size skewonly',---默认oracle 是for all colums size auto
degree =>8,
granularity =>'ALL',
cascade =>true);
end 1oop;
end;
为什么要增量收集,我们这边有一个log表,记录用户下的所有源表的增删改查,这个表用于用户程序的增量同步的判断。
and table_name in('XX')
from (select t2.table_name,
t2.partition_name,
t2.subpartition_name,
t2.inserts,
t2.updates,
t2.deletes,
t1.NUM_ROWS,
case
when (t1.NUM_ROWS = 0 or t1.NUM_ROWS is null) then
100
else
trunc((t2.inserts + t2.updates + t2.deletes) / t1.NUM_ROWS * 100,
2)
end "mod_pct(%)",
case
when t1.LAST_ANALYZED is null then
'未分析'
else
to_char(t1.LAST_ANALYZED)
end LAST_ANALYZED,
t2.timestamp,
'begin dbms_stats.gather_table_stats('||'ownname=>'''|| t2.table_owner ||
''','||'tabname=>''' || t2.table_name ||''','||'estimate_percent=>100'||','||'degree=>4'||',cascade => TRUE); end;' EXEC_STATS
from dba_tab_statistics t1, dba_tab_modifications t2
where t1.OWNER = t2.table_owner
and t1.TABLE_NAME = t2.table_name
and DECODE(t1.PARTITION_NAME, t2.partition_name, 0, 1) = 0
and DECODE(t1.SUBPARTITION_NAME, t2.subpartition_name, 0, 1) = 0
and t2.table_owner = '' AND T2.table_name='')
where "mod_pct(%)" >= 0
order by "mod_pct(%)" desc;
如果发现不足之处,望指出。
原文链接:
http://www.itpub.net/thread-2067391-1-1.html
大会以"架构创新之路"为主题,共设置两个主场分享时段,24个技术交流专场时段;邀请来自互联网、电子商务、金融、电信、政府、行业协会等20多个领域,150多位技术专家及行业领袖来分享他们的经验;并将吸引4000多名系统运维、架构师、及各种企业的IT决策人士参会,为他们提供最具价值的交流平台。
以上是关于数据库优化之统计分析实战篇的主要内容,如果未能解决你的问题,请参考以下文章