手动更新oracle表统计信息
procedure p_bas_analyzetab (v_tab in varchar, v_part in varchar default null) as cnt pls_integer; v_version product_component_version.version%type; v_sysdate date; v_analyzesql clob; v_sqlerrm varchar2(210); db_version_except exception; begin begin delete from t_bas_analyzelog where fcreated < trunc(sysdate-1); commit; exception when others then null; end; for i in (select table_name tname, null pname from user_tables where table_name=upper(v_tab) and v_part is null union all select table_name tname, partition_name pname from user_tab_partitions where table_name=upper(v_tab) and (v_part is not null and partition_name=upper(v_part)) union all select table_name tname, subpartition_name pname from user_tab_subpartitions where table_name=upper(v_tab) and (v_part is not null and subpartition_name=upper(v_part))) loop begin --dbms_output.put_line('1,'||i.tname); for j in (select table_name, last_analyzed from user_tables where table_name=i.tname and temporary = 'Y') loop --2021-01-28 09:05 --dbms_output.put_line('2,'||j.table_name); select trim(version) into v_version from product_component_version where product like 'Oracle Database%'; if (v_version is not null and v_version <= '11.2.0.4.0') then if (j.last_analyzed is not null) then --dbms_output.put_line('3,'||v_version); dbms_stats.delete_table_stats(user,i.tname,no_invalidate=>false); end if; else --dbms_output.put_line(v_version); --dbms_output.put_line('4,'||v_version); execute immediate 'analyze table '||i.tname||' delete statistics'; dbms_stats.gather_table_stats(user,i.tname,no_invalidate=>false); end if; return; end loop; if (i.tname not like 'TMP%') then select count(*), max(fcreated) into cnt, v_sysdate from t_bas_analyzelog where ftname=i.tname and (i.pname is null or fpname=i.pname); if (cnt>0) then if (sysdate > v_sysdate+3/144) then delete from t_bas_analyzelog where ftname=i.tname and (i.pname is null or fpname=i.pname); else return; end if; end if; insert into t_bas_analyzelog(finst_id, faudsid, ftname, fpname, fcreated) select userenv('instance'), userenv('sessionid'), i.tname, i.pname, sysdate from dual; commit; end if; v_analyzesql := 'ownname=>'''||user||''',tabname=>'''||i.tname||''''||(case when i.pname is not null then ',partname=>'''||i.pname||'''' end); if (i.tname not like 'TMP%') then v_analyzesql := v_analyzesql||', method_opt=>''for all columns size auto'',estimate_percent=>dbms_stats.auto_sample_size, degree=>2, no_invalidate=>false '; else v_analyzesql := v_analyzesql||', method_opt=>''for all columns size 254'', estimate_percent=>dbms_stats.auto_sample_size, degree=>2, no_invalidate=>false '; end if; --v_analyzesql := v_analyzesql||', estimate_percent=>dbms_stats.auto_sample_size, degree=>2, no_invalidate=>false '; v_analyzesql := 'begin sys.dbms_stats.gather_table_stats('||v_analyzesql||'); end;'; execute immediate v_analyzesql; if (i.tname not like 'TMP%') then delete from t_bas_analyzelog where ftname=i.tname and (i.pname is null or fpname=i.pname); commit; end if; exception when db_version_except then dbms_output.put_line('db_version_except!'); when others then v_sqlerrm :=substr(dbms_utility.format_error_backtrace,1,100); --raise; end; end loop; end;
手动更新oracle表统计信息
procedure p_bas_analyzetab(v_tab in varchar, v_part in varchar default null) as cnt pls_integer; v_...
点击下载文档
本文2024-09-16 18:39:09发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-23525.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章