參考了一下別人的代碼又補充了一下寫了一個存儲過程
分析某一用戶下面的表及索引
運行完畢後然後設置job即可
create or replace procedure DBA_ANAYZE_SCHEMA(v_USERNAME VARCHAR
)
AS
v_per number(
) DEFAULT
;
v_start number := dbms_utility
get_time;
v_end number;
begin
/*********************
該存儲過程主要是對表及索引進行分析
對於包含有子分區sub
partition的表需要注意一下granularity參數
具體參考
granularity
the granularity of statistics to collect (only pertinent
if the table is partitioned)
DEFAULT
gather global
and partition
level statistics
SUBPARTITION
gather subpartition
level statistics
PARTITION
gather partition
level statistics
GLOBAL
gather global statistics
ALL
gather all (subpartition
partition
and global) statistics
*******************************/
for rec in (select segment_name
segment_type
ceil(sum(bytes)/
/
) segment_size
from user_segments where SEGMENT_NAME NOT LIKE
TMP_%
group by segment_name
segment_type)
loop
CASE WHEN rec
segment_type =
INDEX
THEN
case
when rec
segment_size <=
then
v_per :=
;
when rec
segment_size <=
then
v_per :=
;
else
v_per :=
;
end case;
begin
delete old schema index statistics;
DBMS_STATS
delete_index_stats(ownname => upper(v_USERNAME)
indname => rec
segment_name);
exception
when others then
null;
end;
begin
analyze index compute statistics;
dbms_stats
gather_index_stats(ownname=>upper(v_USERNAME)
自己改一下
INDNAME=>rec
segment_name
estimate_percent =>v_per
degree =>
);
exception
when others then
null;
end;
dbms_output
put_line(rec
segment_name||
||rec
segment_size||
m
||ceil((dbms_utility
get_time
v_start)/
)||
s
);
v_start := dbms_utility
get_time;
WHEN rec
segment_type =
TABLE
then
case when rec
segment_size <=
then
v_per :=
;
when rec
segment_size <=
then
v_per :=
;
else
v_per :=
;
end case;
begin
delete table analyze statistics
dbms_stats
delete_table_stats(ownname =>upper(v_USERNAME)
tabname =>rec
segment_name);
exception
when others then
null;
end;
begin
analyze table compute statistics;
dbms_stats
gather_table_stats(OWNNAME=>upper(v_USERNAME)
TABNAME=>rec
segment_name
ESTIMATE_PERCENT=>v_per
cascade => TRUE
granularity =>
ALL
degree =>
METHOD_OPT=>
FOR ALL INDEXED COLUMNS
);
exception
when others then
null;
end;
dbms_output
put_line(rec
segment_name||
||rec
segment_size||
m
||ceil((dbms_utility
get_time
v_start)/
)||
s
);
v_start := dbms_utility
get_time;
WHEN rec
segment_type =
TABLE PARTITION
then
case when rec
segment_size <=
then
v_per :=
;
when rec
segment_size <=
then
v_per :=
;
else
v_per :=
;
end case;
begin
delete table analyze statistics
dbms_stats
delete_table_stats(ownname =>upper(v_USERNAME)
tabname =>rec
segment_name);
exception
when others then
null;
end;
begin
analyze table compute statistics;
dbms_stats
gather_table_stats(OWNNAME=>upper(v_USERNAME)
TABNAME=>rec
segment_name
ESTIMATE_PERCENT=>v_per
cascade => TRUE
granularity =>
ALL
degree => DBMS_STATS
DEFAULT_DEGREE
METHOD_OPT=>
FOR ALL INDEXED COLUMNS
);
exception
when others then
null;
end;
WHEN rec
segment_type =
INDEX PARTITION
then
case
when rec
segment_size <=
then
v_per :=
;
when rec
segment_size <=
then
v_per :=
;
else
v_per :=
;
end case;
begin
delete old schema index statistics;
DBMS_STATS
delete_index_stats(ownname => upper(v_USERNAME)
indname => rec
segment_name);
exception
when others then
null;
end;
begin
analyze index compute statistics;
dbms_stats
gather_index_stats(ownname=>upper(v_USERNAME)
自己改一下
INDNAME=>rec
segment_name
estimate_percent =>v_per
degree =>dbms_stats
DEFAULT_DEGREE
);
exception
when others then
null;
end;
dbms_output
put_line(rec
segment_name||
||rec
segment_size||
m
||ceil((dbms_utility
get_time
v_start)/
)||
s
);
v_start := dbms_utility
get_time;
/** WHEN rec
segment_type =
LOBINDEX
then
v_start := dbms_utility
get_time;
WHEN rec
segment_type =
LOBSEGMENT
then
v_start := dbms_utility
get_time;**/
END CASE;
end loop;
end;
From:http://tw.wingwit.com/Article/program/Oracle/201311/17964.html