oracle收集统计信息
----小于500M的表每天收集全表统计信息
select 'exec dbms_stats.gather_table_stats(ownname=>''' || owner || '''' ||
',tabname=>''' || table_name || '''' ||
',estimate_percent=>100,method_opt=>''' ||
'FOR all columns size repeat''' || ',cascade=> true);'
from dba_tab_statistics
where (owner, table_name) in
(SELECT owner, table_name
FROM DBA_TABLES
WHERE OWNER in ('TS', 'SS')
AND (owner, TABLE_NAME) in
(select owner, segment_name
from dba_segments
where owner in ('TS', 'SS')
group by owner, segment_name
having sum(bytes) / 1024 / 1024 < 500)
and partitioned != 'YES')
and (stale_stats = 'YES' or last_analyzed is null)
AND OBJECT_TYPE = 'TABLE';
----非分区表大于500M,收集统计信息脚本(每天收集30%的统计信息)
select 'exec dbms_stats.gather_table_stats(ownname=>''' || owner || '''' ||
',tabname=>''' || table_name || '''' ||
',estimate_percent=>30,method_opt=>''' ||
'FOR all columns size repeat''' || ',degree => 4,cascade=> true);'
from dba_tab_statistics
where (owner, table_name) in
(SELECT owner, table_name
FROM DBA_TABLES
WHERE OWNER in ('TS', 'SS')
AND (owner, TABLE_NAME) in
(select owner, segment_name
from dba_segments
where owner in ('TS', 'SS')
group by owner, segment_name
having sum(bytes) / 1024 / 1024 >= 500)
and partitioned != 'YES')
and (stale_stats = 'YES' or last_analyzed is null)
AND OBJECT_TYPE = 'TABLE';
------分区表--每天copy上一个分区的统计信息(自增长分区表)
select 'EXEC DBMS_STATS.COPY_TABLE_STATS (' || '''' || table_owner || '''' || ',' || '''' ||
table_name || '''' || ',' || '''' || 'SYS_P402' || '''' || ',' || '''' ||
partition_name || '''' || ', FORCE=>TRUE);'
from dba_tab_partitions
where table_name = 'T_SS_AC_IM'
and partition_position =
(select max(partition_position)
from dba_tab_partitions
where table_name = 'T_SS_AC_IM');
------copy分区表统计信息脚本
[oracle@test1 ~]$ cat /home/oracle/copy_statics.sh
#!/bin/bash
source /home/oracle/.bash_profile
SDATE=$(date +%Y%m)
TDATE=$(date -d 'next-month' +%Y%m)
SPNAME="P"${SDATE}
TPNAME="P"${TDATE}
exec >> /home/oracle/copy_statics`date +%y%m%d%H`.log
sqlplus / as sysdba << EOF
set timing on
EXEC DBMS_STATS.UNLOCK_TABLE_STATS ('TS','T_TS_TR');
EXEC DBMS_STATS.UNLOCK_TABLE_STATS ('TS','T_TS_TR_ND');
EXEC DBMS_STATS.COPY_TABLE_STATS ('TS', 'T_TS_CUP_Z_TR', '$SPNAME', '$TPNAME',FORCE=>TRUE);
EXEC DBMS_STATS.COPY_TABLE_STATS ('TS', 'T_TS_TR', '$SPNAME', '$TPNAME', FORCE=>TRUE);
EXEC DBMS_STATS.COPY_TABLE_STATS ('TS', 'T_TS_TR_ND', '$SPNAME', '$TPNAME', FORCE=>TRUE);
EXEC DBMS_STATS.LOCK_TABLE_STATS ('TS','T_TS_TR');
EXEC DBMS_STATS.LOCK_TABLE_STATS ('TS','T_TS_TR_ND');
exit;
EOF