标签:oracle性能优化
1.exec dbms_stats.FLUSH_DATABASE_MONITORING_INFO;
For performance reasons, the Oracle Database does not populate these views immediately when the actual modifications occur.
Run the FLUSH_DATABASE_MONITORING_INFO procedure in the DIMS_STATS PL/SQL package to populate these views
with the latest information.
每15分钟,smon会刷新统计结果到数据字典中,oracle使用这些表的数据去判断表的统计数据是否过期,
如果当表的数据改变超过10%,oracle就认为该表的统计数据已经过期.
2.查询某个表的统计信息是否过期:
select owner, table_name name, object_type, stale_stats, last_analyzed from dba_tab_statistics where table_name in (‘TEST‘) and owner = ‘SCOTT‘;
OWNER NAME OBJECT_TYPE STA LAST_ANALYZED
------------------------------ ------------------------------ ------------ --- -------------------
SCOTT TEST TABLE YES 2015-07-10 15:49:43
YES表示过期了,因delete了10%以上的数据。
3.收集统计信息的脚本:
DECLARE
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
5
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 = ‘SCOTT‘
AND segment_name IN
(SELECT /*+ UNNEST */
DISTINCT table_name
FROM dba_tab_statistics
WHERE (last_analyzed IS NULL OR stale_stats = ‘YES‘)
AND owner = ‘SCOTT‘)
GROUP BY owner, segment_name);
30
BEGIN
dbms_stats.flush_database_monitoring_info;
33 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 columns size repeat‘,
degree => 8,
granularity => ‘ALL‘,
cascade => TRUE);
41 END LOOP;
42 END;
43 /
PL/SQL procedure successfully completed.
Elapsed: 00:00:11.58
再一次查询:
SQL> select owner, table_name name, object_type, stale_stats, last_analyzed from dba_tab_statistics where table_name in (‘TEST‘) and owner = ‘SCOTT‘;
OWNER NAME OBJECT_TYPE STA LAST_ANALYZED
------------------------------ ------------------------------ ------------ --- -------------------
SCOTT TEST TABLE NO 2015-07-10 20:10:26
Elapsed: 00:00:00.46
新建一个表,没有统计信息:
SQL> select owner,table_name name,object_type,stale_stats,last_analyzed from dba_tab_statistics where table_name in(‘TEST‘,‘TEST1‘) and owner=‘SCOTT‘ and (STALE_STATS=‘YES‘ or last_analyzed is null) ;
OWNER NAME OBJECT_TYPE STA LAST_ANALYZED
------------------------------ ------------------------------ ------------ --- -------------------
SCOTT TEST1 TABLE
Elapsed: 00:00:00.69
DECLARE
CURSOR stale_table IS
SELECT owner,
segment_name,
CASE
WHEN size_gb < 0.5 THEN--根据需求自行修改
100
WHEN size_gb >= 0.5 AND size_gb < 1 THEN
50
WHEN size_gb >= 1 AND size_gb < 5 THEN
30
WHEN size_gb >= 5 AND size_gb < 10 THEN
20
WHEN size_gb >= 10 THEN
10
END AS percent,
4 AS degree --收集
FROM (SELECT owner,
segment_name,
SUM(bytes / 1024 / 1024 / 1024) size_gb
FROM dba_segments
WHERE owner = ‘SCOTT‘
AND segment_name IN
(SELECT /*+ UNNEST */
DISTINCT table_name
FROM dba_tab_statistics
WHERE (last_analyzed IS NULL OR stale_stats = ‘YES‘)
AND owner = ‘SCOTT‘)
GROUP BY owner, segment_name);
30
BEGIN
dbms_stats.flush_database_monitoring_info;
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 columns size repeat‘,
degree => 4,
granularity => ‘ALL‘,
cascade => TRUE);
END LOOP;
42 END;
43 /--这个脚本是用于某个用户下所有表统计信息,收集,单表不必大费周折。
已经有了:
SQL> select owner,table_name name,object_type,stale_stats,last_analyzed from dba_tab_statistics where table_name in(‘TEST‘,‘TEST1‘) and owner=‘SCOTT‘;
OWNER NAME OBJECT_TYPE STA LAST_ANALYZED
------------------------------ ------------------------------ ------------ --- -------------------
SCOTT TEST TABLE NO 2015-07-10 20:32:47
SCOTT TEST1 TABLE NO 2015-07-10 20:32:35
Elapsed: 00:00:00.04
标签:oracle性能优化
原文地址:http://7642644.blog.51cto.com/7632644/1673054