--analyze.sql
set serveroutput on size 100000
declare
v_per number(3);
v_start number := dbms_utility.get_time;
v_end number;
begin
for rec in (select segment_name,segment_type,ceil(sum(bytes)/1024/1024) segment_size
from user_segments group by segment_name,segment_type)
loop
if rec.segment_type = 'INDEX' then
dbms_stats.gather_index_stats(ownname=>'citictest', --自己改一下
INDNAME=>rec.segment_name
);
-- dbms_output.put_line(rec.segment_name ' ' rec.segment_size 'm ' ceil((dbms_utility.get_time - v_start)/100) 's');
v_start := dbms_utility.get_time;
elsif rec.segment_type = 'TABLE' then
case when rec.segment_size < 32 then
v_per := 100;
when rec.segment_size < 320 then
v_per := 10;
else
v_per := 1;
end case;
dbms_stats.gather_table_stats(OWNNAME=>'citictest',
TABNAME=>rec.segment_name,
ESTIMATE_PERCENT=>v_per,
METHOD_OPT=>'FOR ALL INDEXED COLUMNS');
-- dbms_output.put_line(rec.segment_name ' ' rec.segment_size 'm ' ceil((dbms_utility.get_time - v_start)/100) 's');
v_start := dbms_utility.get_time;
end if;
end loop;
end;
/
--转自