9i_crt_gather_table_stats(生成表的統計資訊收集指令碼,關鍵在引數)
set serverout on;
declare
v_owner varchar2(200) := upper('&tab_owner');
v_table_name varchar2(200) := upper('&tab_name');
v_num_rows number;
v_message varchar2(300);
v_est_percent varchar2(300);
V_SQLTEXT VARCHAR2(4000);
v_degree number;
V_COUNT number;
V_COUNT2 number;
v_statid varchar2(31);
v_METHOD_OPT varchar2(50);
v_last_analyzed varchar2(30);
v_sqltext2 varchar2(400);
begin
SELECT SUBSTR(V_TABLE_NAME, 1, 28)||MOD(NVL(MAX(SUBSTR(STATID, -1, 1)), 0) + 1,10)
INTO v_statid
FROM DBMGR.CBO_STATS_BAK
WHERE C1 = V_TABLE_NAME
AND C5 = v_owner
AND D1 = (SELECT MAX(D1)
FROM DBMGR.CBO_STATS_BAK
WHERE C1 = V_TABLE_NAME
AND C5 = v_owner
AND STATID NOT LIKE 'DATABASE_');
select to_char(last_analyzed,'YYYYMMDD HH24:MI:SS') into v_last_analyzed from dba_tables where table_name=V_TABLE_NAME and wner=v_owner;
v_sqltext:='begin dbms_stats.export_table_stats(ownname => '''||v_owner||''','||chr(10)||
'tabname => '''||v_table_name||''','||chr(10)||
'stattab => ''CBO_STATS_BAK'','||chr(10)||
'statown => ''dbmgr'','||chr(10)||
'cascade => true,'||chr(10)||
'statid => '''||v_statid||''');'||chr(10)||
'end;'||chr(10)||
'/';
v_sqltext2:='insert into dbmgr.cbo_backup_info(OWNER,TABLE_NAME,last_analyzed,statid,Backup_Date) values('''||v_owner||''','''||v_table_name||''',to_date('''||v_last_analyzed||''',''YYYY-MM-DD HH24:MI:SS''),'''||v_statid||''',SYSDATE);';
dbms_output.put_line('------------backup_scripts---------------') ;
dbms_output.put_line(v_sqltext) ;
dbms_output.put_line(v_sqltext2) ;
dbms_output.put_line('commit;') ;
select count(*)
into v_count
from dbstats.STATS_GATHER_LOG_DETAIL sts
where sts.object_owner = v_owner
and sts.object_name = v_table_name
and sts.gather_message like 'BEGIN%'
and rownum = 1;
if v_count > 0 then
begin
SELECT replace(gather_message, 'CASCADE => FALSE', 'CASCADE => TRUE')
into v_sqltext
FROM DBSTATS.STATS_GATHER_LOG_DETAIL SG
WHERE SG.GATHER_START_TIME =
(select MAX(GATHER_START_TIME)
from dbstats.STATS_GATHER_LOG_DETAIL sts
where sts.object_owner = v_owner
and sts.object_name = v_table_name
and sts.object_type='TABLE'
and sts.gather_message like 'BEGIN%')
AND SG.object_owner = v_owner
AND SG.object_name = v_table_name;
SELECT COUNT(*)
INTO V_COUNT2
FROM DBA_TAB_PARTITIONS
WHERE table_owner = v_owner
AND table_name = v_table_name;
if v_count2=0 then
v_sqltext:=v_sqltext;
else
select REPLACE(V_SQLTEXT,'GRANULARITY => ''DEFAULT''','GRANULARITY => ''ALL''') into V_SQLTEXT from dual;
end if;
dbms_output.put_line('------------gather_scripts---------------') ;
dbms_output.put_line(substr(v_sqltext,
0,
instr(v_sqltext, 'BLOCK_SAMPLE') - 1));
dbms_output.put_line(substr(v_sqltext,
instr(v_sqltext, 'BLOCK_SAMPLE')));
end;
else
begin
select num_rows
into v_num_rows
from dba_tables
where wner = v_owner
AND table_name = v_table_name;
if v_num_rows is null then
V_METHOD_OPT:='FOR ALL COLUMNS SIZE 1';
else v_METHOD_OPT:='FOR ALL COLUMNS SIZE REPEAT';
end if;
if v_num_rows < 5000000 or v_num_rows is null then
v_est_percent := 20;
end if;
if v_num_rows between 5000000 and 10000000 then
v_est_percent := 10;
end if;
if v_num_rows between 10000000 and 100000000 then
v_est_percent := 5;
end if;
if v_num_rows > 100000000 then
v_est_percent := 1;
end if;
if v_num_rows < 1000000 or v_num_rows is null then
v_degree := 1;
end if;
if v_num_rows between 1000000 and 5000000 then
v_degree := 2;
end if;
if v_num_rows between 5000000 and 10000000 then
v_degree := 4;
end if;
if v_num_rows between 10000000 and 50000000 then
v_degree := 6;
end if;
if v_num_rows between 50000000 and 100000000 then
v_degree := 8;
end if;
if v_num_rows > 100000000 then
v_degree := 12;
end if;
SELECT COUNT(*)
INTO V_COUNT
FROM DBA_TAB_PARTITIONS
WHERE table_owner = v_owner
AND table_name = v_table_name;
if V_COUNT = 0 then
V_SQLTEXT := 'BEGIN DBMS_STATS.GATHER_TABLE_STATS(OWNNAME => ''' || V_OWNER || ''',' || CHR(10) ||
'TABNAME => ''' || V_TABLE_NAME || ''',' || CHR(10) ||
'ESTIMATE_PERCENT => ' || v_est_percent || ',' || CHR(10) ||
'METHOD_OPT => '''||V_METHOD_OPT||''',' || CHR(10) ||
'DEGREE => ' || v_degree || ',' || CHR(10) ||
'CASCADE => TRUE,' || CHR(10) ||
'NO_INVALIDATE => FALSE);' || CHR(10) ||
'END;';
dbms_output.put_line('------------gather_scripts---------------') ;
dbms_output.put_line(V_SQLTEXT);
dbms_output.put_line('------------gather_scripts---------------') ;
else
V_SQLTEXT := 'BEGIN DBMS_STATS.GATHER_TABLE_STATS(OWNNAME => ''' || V_OWNER || ''',' || CHR(10) ||
'TABNAME => ''' || V_TABLE_NAME || ''',' || CHR(10) ||
'ESTIMATE_PERCENT => ' || v_est_percent || ',' || CHR(10) ||
'METHOD_OPT => '''||V_METHOD_OPT||''',' || CHR(10) ||
'DEGREE => ' || v_degree || ',' || CHR(10) ||
'GRANULARITY => ''ALL'',' || CHR(10) ||
'CASCADE => TRUE,' || CHR(10) ||
'NO_INVALIDATE => FALSE);' || CHR(10) ||
'END;';
dbms_output.put_line('------------gather_scripts---------------') ;
dbms_output.put_line(V_SQLTEXT);
dbms_output.put_line('------------gather_scripts---------------') ;
end if;
end;
end if;
exception
when others then
v_message := sqlerrm;
dbms_output.put_line(v_message);
end;
/
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11411056/viewspace-734328/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 10g_crt_gather_table_stats(生成表的統計資訊收集指令碼,關鍵在引數)指令碼
- Oracle運維指令碼-收集統計資訊Oracle運維指令碼
- 【TABLE】Oracle表資訊收集指令碼Oracle指令碼
- 通過shell指令碼生成資料統計資訊的報表指令碼
- 透過shell指令碼生成資料統計資訊的報表指令碼
- Oracle 11g 統計資訊收集指令碼Oracle指令碼
- MySQL對所有表收集統計資訊(Python 2指令碼)MySqlPython指令碼
- Oracle收集統計資訊之NO_INVALIDATE引數Oracle
- Oracle 判斷 並 手動收集 統計資訊 指令碼Oracle指令碼
- 收集統計資訊的SQL指令碼(sosi.sql)--崔華大師SQL指令碼
- 重新收集oracle表的統計資訊Oracle
- MySQL索引統計資訊更新相關的引數MySql索引
- 表統計資訊匯出匯入指令碼指令碼
- MySQL中的統計資訊相關引數介紹MySql
- 在WinDbg指令碼中使用引數指令碼
- 關於oracle自動收集統計資訊Oracle
- 【統計資訊】Oracle常用的收集統計資訊方式Oracle
- QTP - 指令碼相關收集QT指令碼
- Oracle 11g手工收集表統計資訊Oracle
- Oracle收集統計資訊Oracle
- Oracle 統計資訊收集Oracle
- 收集oracle統計資訊Oracle
- ORACLE 收集統計資訊Oracle
- oracle外部表建立以及收集統計資訊以及臨時表Oracle
- powerdesigner生成的sql建表指令碼如何去除雙引號"SQL指令碼
- 關鍵字引數與非關鍵字引數(可變引數)詳解
- 全域性臨時表GTT的統計資訊收集辦法:
- SAMPLE語句在統計資訊收集中應用
- 關閉特定物件統計資訊自動收集物件
- Shell 中 $ 關於指令碼引數的幾種用法指令碼
- 收集全庫統計資訊
- mysql如收集統計資訊MySql
- MySQL 5.5 統計資訊收集MySql
- 檢視錶的統計資訊SQL指令碼SQL指令碼
- 資訊集--資訊系統分析設計關鍵
- 收集統計資訊的簡單操作
- ORACLE 統計資訊的收集與管理Oracle
- 統計資訊收集不完的解決