dbms_stat 采集统计信息状态为 stale 的 table、partition table

db: oracle 12.2.0.1


declare
   lv_user varchar(100);
begin
   dbms_stats.flush_database_monitoring_info();
   
   --采集该用户下对象的统计信息
   lv_user:='SCOTT';
   
   --非分区表
   for c_part in (
       
       select dts.stale_stats,
              dts.owner,
              dts.table_name,
              dts.partition_name,
              dts.subpartition_name,
              dts.object_type--,
              --dts.* 
         from dba_tab_statistics dts
        where 1=1
          and dts.owner=lv_user
          and dts.object_type ='TABLE'
          and NVL(dts.stale_stats,'YES') ='YES'
          and not exists (select *
                            from dba_part_tables dpt
                           where 1=1
                             and dts.owner = dpt.owner
                             and dts.table_name = dpt.table_name
                          )
   )
   loop
      BEGIN
          dbms_stats.gather_table_stats(
              ownname=> c_part.owner ,
              tabname=> c_part.table_name,
              estimate_percent=> 100 , 
              cascade=> TRUE,
              method_opt=>'for all indexed columns size skewonly ',
              no_invalidate=>FALSE,
              granularity=>'AUTO',
              degree=>8
          );
      exception
          when others then
            rollback;    
      END ;
   
   end loop;
   
   --分区表
   for c_part_subpart in (
       
       select dts.stale_stats,
              dts.owner,
              dts.table_name,
              dts.partition_name,
              dts.subpartition_name,
              dts.object_type--,
              --dts.* 
         from dba_tab_statistics dts
        where 1=1
          and dts.owner=lv_user
          and dts.object_type ='PARTITION'
          and NVL(dts.stale_stats,'YES') ='YES'
   )
   loop
      BEGIN
          dbms_stats.gather_table_stats(
              ownname=> c_part_subpart.owner ,
              tabname=> c_part_subpart.table_name,
              partname=> c_part_subpart.partition_name,
              estimate_percent=> 100 , 
              cascade=> TRUE,
              method_opt=>'for all indexed columns size skewonly ',
              no_invalidate=>FALSE,
              granularity=>'AUTO',
              degree=>8
          );
      exception
          when others then
            rollback;    
      END ;
   
   end loop;

end;

参考:

你可能感兴趣的:(#,oracle,table,index,#,oracle,optimizer,statistics)