oracle定时分析用户下的所有表

来源:互联网 发布:软件测试知识点汇总 编辑:程序博客网 时间:2024/06/11 18:03

author:skate
time:  2009/04/16


定时分析用户下的所有表

 

你的数据库运行在CBO的模式下,就要对你的表经常做分析,尤其是变化
很大的表,这样oracle才会选择正确的执行计划,下面是我写的一个按时
分析表的一个的脚本


操作步骤:

1. 编写执行分析表的procedure
2. 编写调用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 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. 编写调用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的定时任务

 

 

#ayalyze tickets all tables

1 07 * * * sh /home/oracle/sh/ticket_analyze.sh >> /home/oracle/sh/ticket_analyze.log


这样一个每天7:01的定时分析表的任务就做完!!!

收工!!

 

 

---end---

 

原创粉丝点击