oracle定時(shí)分析用戶下的所有表
定時(shí)分析用戶下的所有表
你的數(shù)據(jù)庫運(yùn)行在CBO的模式下,就要對(duì)你的表經(jīng)常做分析,尤其是變化很大的表,這樣oracle才會(huì)選擇正確的執(zhí)行計(jì)劃,下面是我寫的一個(gè)按時(shí)分析表的一個(gè)的腳本
操作步驟:
1. 編寫執(zhí)行分析表的procedure2. 編寫調(diào)用procedure的os的shell腳本3. 在os做crontab的定時(shí)任務(wù)
1. 編寫執(zhí)行分析表的procedure
create or replace procedure pro_analyze_user_objects (tablename varchar2; ---不分析的表名或表名的部分字符)is /******************************************************* author:skate time; :2009/04/16 功能:分析大于1000行的表的統(tǒng)計(jì)信息 說明:可以統(tǒng)計(jì)用戶下的所有大于1000行的表,如果要統(tǒng)計(jì)其他用戶 ,只要把這個(gè)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 isselect table_namefrom user_tables where num_rows>1000and table_name not like '%'||upper(tablename)||'%';record_cur_tab cur_tab%rowtype;
begin
open cur_tab;loopfetch 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/shdatesqlplus /nolog @ ticket_analyze.sqldateexit[oracle@svr-db-test sh]$
[oracle@svr-db-test sh]$ more ticket_analyze.sqlconnect tickets/123456set timing onexec pro_analyze_user_objects('_his');exit;
[oracle@svr-db-test sh]$
¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
3. 在os做crontab的定時(shí)任務(wù)
#ayalyze tickets all tables
1 07 * * * sh /home/oracle/sh/ticket_analyze.sh >> /home/oracle/sh/ticket_analyze.log
這樣一個(gè)每天7:01的定時(shí)分析表的任務(wù)就做完?。?!
收工?。?/P>
