oracle定時分析用戶下的所有表
定時分析用戶下的所有表
你的數據庫運行在CBO的模式下,就要對你的表經常做分析,尤其是變化很大的表,這樣oracle才會選擇正確的執行計劃,下面是我寫的一個按時分析表的一個的腳本
操作步驟:
1. 編寫執行分析表的procedure2. 編寫調用procedure的os的shell腳本3. 在os做crontab的定時任務
1. 編寫執行分析表的procedure
create or replace procedure pro_analyze_user_objects (tablename varchar2; ---不分析的表名或表名的部分字符)is /******************************************************* author:skate time; :2009/04/16 功能:分析大于1000行的表的統計信息 說明:可以統計用戶下的所有大于1000行的表,如果要統計其他用戶 ,只要把這個procedure放在相應的用戶下或者用all_tables
eg:exec pro_analyze_user_objects('_his') 相關分析語句如下: 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. 編寫調用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的定時任務
#ayalyze tickets all tables
1 07 * * * sh /home/oracle/sh/ticket_analyze.sh >> /home/oracle/sh/ticket_analyze.log
這樣一個每天7:01的定時分析表的任務就做完!!!
收工!!