使用存储过程对table,index进行统计, 优化查询计划

create or replace procedure sp_analyze is
 v_tablename varchar2(50);
 v_lastday   varchar2(10);
 v_today     varchar2(10);
 cursor c_tablename is
  select distinct table_name from user_tab_partitions where partition_name like v_lastday or partition_name like v_today;

 cursor c_partitionname is
     select partition_name from user_tab_partitions where table_name = v_tablename;

 cursor c_indexname is
  select index_name from user_indexes where table_name = v_tablename;

 v_partition_name varchar2(50);

 v_sqlstr varchar2(1000);
begin

 v_lastday := 'P' || to_char(sysdate - 1, 'yyyymmdd') || '%';  --查询前一天
 v_today   := 'P' || to_char(sysdate, 'yyyymmdd') || '%';      --查询今天

 for CC in c_tablename loop
  begin
   v_tablename := CC.table_name;
   for BB in c_partitionname loop
    begin
     v_partition_name := BB.partition_name;
          --对每个分区中的表进行统计,统计后就知道相关对象数据的各方面的统计信息,优化器就能据此选择更好的执行计划去执行select语句
     v_sqlstr         := 'analyze table ' || v_tablename || ' partition (' || v_partition_name || ') estimate statistics sample 5 percent';
     execute immediate v_sqlstr;
              for AA in c_indexname loop
               begin
                  --对每个表中的索引进行统计, 使系统选用最好的执行计划去执行select语句
                v_sqlstr := 'analyze index ' || AA.index_name || ' partition (' || v_partition_name || ') estimate statistics sample 5 percent';
                execute immediate v_sqlstr;
               exception
                when others then
                 rollback;
               end;
              end loop;--end of index
       exception
     when others then
      rollback;
    end;
   end loop;--end of partition
  end;
 end loop;--end of table
end sp_analyze;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值