定時分析用戶下的所有表
你的數(shù)據(jù)庫運(yùn)行在CBO的模式下,就要對你的表經(jīng)常做分析,尤其是變化
很大的表,這樣Oracle才會選擇正確的執(zhí)行計劃,下面是我寫的一個按時
分析表的一個的腳本
操作步驟:
1. 編寫執(zhí)行分析表的PRocedure
2. 編寫調(diào)用procedure的os的shell腳本
3. 在os做crontab的定時任務(wù)
1. 編寫執(zhí)行分析表的procedure
create or replace procedure pro_analyze_user_objects (tablename varchar2 ---不分析的表名或表名的部分字符
)
is
/*******************************************************
author:skate
time :2009/04/16
功能:分析大于1000行的表的統(tǒng)計信息
說明:可以統(tǒng)計用戶下的所有大于1000行的表,如果要統(tǒng)計其他用戶
,只要把這個procedure放在相應(yīng)的用戶下或者用all_tables
eg:exec pro_analyze_user_objects('_his')
相關(guān)分析語句如下:
analyze table D_COURSE compute statistics for all indexes for all columns for table;
analyze table D_COURSE compute statistics;
********************************************************/
cursor cur_tab is
select table_name
from user_tables where num_rows>1000
and table_name not like '%'||upper(tablename)||'%';
record_cur_tab cur_tab%rowtype;
begin
open cur_tab;
loop
fetch cur_tab into record_cur_tab;
exit when cur_tab%notfound;
execute immediate 'analyze table '|| record_cur_tab.table_name || ' compute statistics';
end loop;
end pro_analyze_user_objects;
2. 編寫調(diào)用procedure的os的shell腳本
[oracle@svr-db-test sh]$ more ticket_analyze.sh
####################################
#author:skate
#time :2009/04/16
#desc : The timming analyze user of tables
####################################
cd /home/oracle/sh
date
sqlplus /nolog @ ticket_analyze.sql
date
exit
[oracle@svr-db-test sh]$
[oracle@svr-db-test sh]$ more ticket_analyze.sql
connect tickets/123456
set timing on
exec pro_analyze_user_objects('_his');
exit;
[oracle@svr-db-test sh]$
¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
3. 在os做crontab的定時任務(wù)
#ayalyze tickets all tables
1 07 * * * sh /home/oracle/sh/ticket_analyze.sh >> /home/oracle/sh/ticket_analyze.log
這樣一個每天7:01的定時分析表的任務(wù)就做完!!!
收工!!
新聞熱點(diǎn)
疑難解答
圖片精選