Script

Script - Check Current Undo Configuration and Advise Recommended Setup (Doc ID 1579035.1)

APPLIES TO:

Oracle Database - Enterprise Edition - Version 8.1.7.0 and later
Information in this document applies to any platform.
*** Checked for relevance on 05-Apr-2016 ***

GOAL

 This document is intended to check the current undo configuration and provides recommendation based on the previous workload.AUTION

本文档旨在检查当前的 undo 配置,并根据先前的工作量提供建议。

This sample code is provided for diagnosis and troubleshooting purposes only, and is NOT supported by Oracle Support. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
提供: 此示例代码仅用于诊断和故障排除,Oracle Support不支持此示例代码。它已经过内部测试,但是,我们不能保证它会为您服务。使用前,请确保在测试环境中运行它 
There are different scripts for database versions post 10g and prior to 10g. Ensure to execute the correct one to get the proper recommendation. Ensure to execute the script as SYS user.
10g之后和10g之前的数据库版本有不同的脚本。确保执行正确的建议以获得正确的建议。确保以SYS用户身份执行脚本。

SOLUTION

For database version 10g and above, use the following script. Ensure to execute the same as SYSDBA.

对于10g及更高版本的数据库,请使用以下脚本。确保执行与SYSDBA相同的操作

RDBMS version 10g and above:

SET SERVEROUTPUT ON
SET LINES 600
ALTER SESSION SET NLS_DATE_FORMAT = 'DD/MM/YYYY HH24:MI:SS';

DECLARE
    v_analyse_start_time    DATE := SYSDATE - 7;
    v_analyse_end_time      DATE := SYSDATE;
    v_cur_dt                DATE;
    v_undo_info_ret         BOOLEAN;
    v_cur_undo_mb           NUMBER;
    v_undo_tbs_name         VARCHAR2(100);
    v_undo_tbs_size         NUMBER;
    v_undo_autoext          BOOLEAN;
    v_undo_retention        NUMBER(5);
    v_undo_guarantee        BOOLEAN;
    v_instance_number       NUMBER;
    v_undo_advisor_advice   VARCHAR2(100);
    v_undo_health_ret       NUMBER;
    v_problem               VARCHAR2(1000);
    v_recommendation        VARCHAR2(1000);
    v_rationale             VARCHAR2(1000);
    v_retention             NUMBER;
    v_utbsize               NUMBER;
    v_best_retention        NUMBER;
    v_longest_query         NUMBER;
    v_required_retention    NUMBER;
BEGIN
    select sysdate into v_cur_dt from dual;
    DBMS_OUTPUT.PUT_LINE(CHR(9));
    DBMS_OUTPUT.PUT_LINE('- Undo Analysis started at : ' || v_cur_dt || ' -');
    DBMS_OUTPUT.PUT_LINE('--------------------------------------------------');

    v_undo_info_ret := DBMS_UNDO_ADV.UNDO_INFO(v_undo_tbs_name, v_undo_tbs_size, v_undo_autoext, v_undo_retention, v_undo_guarantee);
    select sum(bytes)/1024/1024 into v_cur_undo_mb from dba_data_files where tablespace_name = v_undo_tbs_name;

    DBMS_OUTPUT.PUT_LINE('NOTE:The following analysis is based upon the database workload during the period -');
    DBMS_OUTPUT.PUT_LINE('Begin Time : ' || v_analyse_start_time);
    DBMS_OUTPUT.PUT_LINE('End Time   : ' || v_analyse_end_time);
    
    DBMS_OUTPUT.PUT_LINE(CHR(9));
    DBMS_OUTPUT.PUT_LINE('Current Undo Configuration');
    DBMS_OUTPUT.PUT_LINE('--------------------------');
    DBMS_OUTPUT.PUT_LINE(RPAD('Current undo tablespace',55) || ' : ' || v_undo_tbs_name);
    DBMS_OUTPUT.PUT_LINE(RPAD('Current undo tablespace size (datafile size now) ',55) || ' : ' || v_cur_undo_mb || 'M');
    DBMS_OUTPUT.PUT_LINE(RPAD('Current undo tablespace size (consider autoextend) ',55) || ' : ' || v_undo_tbs_size || 'M');
    IF V_UNDO_AUTOEXT THEN
        DBMS_OUTPUT.PUT_LINE(RPAD('AUTOEXTEND for undo tablespace is',55) || ' : ON');  
    ELSE
        DBMS_OUTPUT.PUT_LINE(RPAD('AUTOEXTEND for undo tablespace is',55) || ' : OFF');  
    END IF;
    DBMS_OUTPUT.PUT_LINE(RPAD('Current undo retention',55) || ' : ' || v_undo_retention);

    IF v_undo_guarantee THEN
        DBMS_OUTPUT.PUT_LINE(RPAD('UNDO GUARANTEE is set to',55) || ' : TRUE');
    ELSE
        dbms_output.put_line(RPAD('UNDO GUARANTEE is set to',55) || ' : FALSE');
    END IF;
    DBMS_OUTPUT.PUT_LINE(CHR(9));

    SELECT instance_number INTO v_instance_number FROM V$INSTANCE;

    DBMS_OUTPUT.PUT_LINE('Undo Advisor Summary');
    DBMS_OUTPUT.PUT_LINE('---------------------------');

    v_undo_advisor_advice := dbms_undo_adv.undo_advisor(v_analyse_start_time, v_analyse_end_time, v_instance_number);
    DBMS_OUTPUT.PUT_LINE(v_undo_advisor_advice);

    DBMS_OUTPUT.PUT_LINE(CHR(9));
    DBMS_OUTPUT.PUT_LINE('Undo Space Recommendation');
    DBMS_OUTPUT.PUT_LINE('-------------------------');

    v_undo_health_ret := dbms_undo_adv.undo_health(v_analyse_start_time, v_analyse_end_time, v_problem, v_recommendation, v_rationale, v_retention, v_utbsize);
    IF v_undo_health_ret > 0 THEN
        DBMS_OUTPUT.PUT_LINE('Minimum Recommendation           : ' || v_recommendation);
        DBMS_OUTPUT.PUT_LINE('Rationale                        : ' || v_rationale);
        DBMS_OUTPUT.PUT_LINE('Recommended Undo Tablespace Size : ' || v_utbsize || 'M');
    ELSE
        DBMS_OUTPUT.PUT_LINE('Allocated undo space is sufficient for the current workload.');
    END IF;
    
    SELECT dbms_undo_adv.best_possible_retention(v_analyse_start_time, v_analyse_end_time) into v_best_retention FROM dual;
    SELECT dbms_undo_adv.longest_query(v_analyse_start_time, v_analyse_end_time) into v_longest_query FROM dual;
    SELECT dbms_undo_adv.required_retention(v_analyse_start_time, v_analyse_end_time) into v_required_retention FROM dual;

    DBMS_OUTPUT.PUT_LINE(CHR(9));
    DBMS_OUTPUT.PUT_LINE('Retention Recommendation');
    DBMS_OUTPUT.PUT_LINE('------------------------');
    DBMS_OUTPUT.PUT_LINE(RPAD('The best possible retention with current configuration is ',60) || ' : ' || v_best_retention || ' Seconds');
    DBMS_OUTPUT.PUT_LINE(RPAD('The longest running query ran for ',60) || ' : ' || v_longest_query || ' Seconds');
    DBMS_OUTPUT.PUT_LINE(RPAD('The undo retention required to avoid errors is ',60) || ' : ' || v_required_retention || ' Seconds');

END;
/

Sample Output

- Undo Analysis started at : 30/08/2013 11:08:40 -
--------------------------------------------------
NOTE:The following analysis is based upon the database workload during the period -
Begin Time : 23/08/2013 11:08:40
End Time   : 30/08/2013 11:08:40

Current Undo Configuration
--------------------------
Current undo tablespace                                 : UNDOTBS2
Current undo tablespace size (datafile size now)        : 20M
Current undo tablespace size (consider autoextend)      : 20M
AUTOEXTEND for undo tablespace is                       : ON
Current undo retention                                  : 900
UNDO GUARANTEE is set to                                : FALSE

Undo Advisor Summary
---------------------------
Finding 1:Undo Tablespace is under pressure. Recommendation 1:Size undo tablespace to 26 MB

Undo Space Recommendation
-------------------------
Minimum Recommendation           : Size undo tablespace to 26 MB
Rationale                        : Increase undo tablespace size so that long running queries will not fail
Recommended Undo Tablespace Size : 26M

Retention Recommendation
------------------------
The best possible retention with current configuration is    : 9171 Seconds
The longest running query ran for                            : 2543 Seconds
The undo retention required to avoid errors is               : 2543 Seconds

PL/SQL procedure successfully completed.

RDBMS version 9i and below

Script

For 9i and below, please download the  script undo_analyse9i.sql.

--undo_analyse9i.sql

SET SERVEROUTPUT ON
SET LINES 600
ALTER SESSION SET NLS_DATE_FORMAT = 'DD/MM/YYYY HH24:MI:SS';

DECLARE
	v_begin_time           varchar2(30);
	v_end_time             varchar2(30);
	v_max_qry_len          number;
	v_unto_tbs             varchar2(100);
	v_cur_undo_ret         number;
	v_cur_undo_mb          number;
	v_undo_autoext         varchar2(5);
	v_cur_dt               date;
	v_recommended_undo_mb  number;
	v_analyse_start_time   date;
	v_analyse_end_time     date;
BEGIN
	select sysdate
	into v_cur_dt
	from dual;

	select sysdate into v_cur_dt from dual;
	DBMS_OUTPUT.PUT_LINE(CHR(10));
	DBMS_OUTPUT.PUT_LINE('--------------------------------------------------');
	DBMS_OUTPUT.PUT_LINE('- Undo Analysis started at : ' || v_cur_dt || ' -');
	DBMS_OUTPUT.PUT_LINE('--------------------------------------------------');

	SELECT begin_time,end_time into v_analyse_start_time, v_analyse_end_time
	FROM v$undostat WHERE undoblks = (SELECT MAX(undoblks) FROM v$undostat);
	
	DBMS_OUTPUT.PUT_LINE('NOTE:The following analysis is based upon the peak database workload during the period -');
	DBMS_OUTPUT.PUT_LINE('Begin Time : ' || v_analyse_start_time);
	DBMS_OUTPUT.PUT_LINE('End Time   : ' || v_analyse_end_time);
	

	select min(BEGIN_TIME), max(end_time) into v_begin_time, v_end_time from v$undostat;
	select max(MAXQUERYLEN) into v_max_qry_len from v$undostat;	
	select value into v_cur_undo_ret from v$parameter where name = 'undo_retention';
	select value into v_unto_tbs from v$parameter where name = 'undo_tablespace';

	SELECT ROUND((UR * (UPS * DBS))/1024/1024)
	INTO v_recommended_undo_mb
	FROM
	  (	select 	max(MAXQUERYLEN) UR
		from 	v$undostat) ,
	  (SELECT undoblks/((end_time-begin_time)*86400) AS UPS
	   FROM v$undostat
	   WHERE undoblks =
		   (SELECT MAX(undoblks)
			FROM v$undostat)),
	  (SELECT block_size AS DBS
	   FROM dba_tablespaces
	   WHERE tablespace_name =
		   (SELECT UPPER(value)
			FROM v$parameter
			WHERE name = 'undo_tablespace'));
	

	select 	sum(bytes)/1024/1024 into v_cur_undo_mb from dba_data_files	where tablespace_name = v_unto_tbs;
								
	SELECT CASE(select count(*) from dba_data_files
				where tablespace_name = v_unto_tbs
				and	autoextensible = 'YES')
				WHEN 0 THEN 'OFF'
				ELSE 'ON' END
	into 	v_undo_autoext
	from 	dual;

	DBMS_OUTPUT.PUT_LINE(CHR(10));
	DBMS_OUTPUT.PUT_LINE('----');
	DBMS_OUTPUT.PUT_LINE(RPAD('Longest running query ran for : ',40) || ' : ' || v_max_qry_len || ' Seconds');
	DBMS_OUTPUT.PUT_LINE(RPAD('Current undo retention is ',40) || ' : ' || v_cur_undo_ret || ' Seconds');
	DBMS_OUTPUT.PUT_LINE(RPAD('Current undo tablespace size is ',40) || ' : ' || v_cur_undo_mb || 'M');
	DBMS_OUTPUT.PUT_LINE(RPAD('AUTOEXTEND for undo tablespace size is ',40) || ' : ' || v_undo_autoext);
	DBMS_OUTPUT.PUT_LINE('----');
	DBMS_OUTPUT.PUT_LINE(RPAD('Recommended undo retention is ',40) || ' : ' || TO_CHAR(v_max_qry_len+1) || ' Seconds');
	DBMS_OUTPUT.PUT_LINE(RPAD('Recommended undo tablespace size is ',40) || ' : ' || v_recommended_undo_mb || 'M');

	select sysdate into v_cur_dt from dual;
	DBMS_OUTPUT.PUT_LINE(CHR(10));
	DBMS_OUTPUT.PUT_LINE('----------------------------------------------------');
	DBMS_OUTPUT.PUT_LINE('- Undo Analysis completed at : ' || v_cur_dt || ' -');
	DBMS_OUTPUT.PUT_LINE('----------------------------------------------------');
	
END;
/

Sample Output

--------------------------------------------------
- Undo Analysis started at : 28/08/2013 11:03:23 -
--------------------------------------------------
NOTE:The following analysis is based upon the peak database workload during the period -
Begin Time : 27/08/2013 21:56:36
End Time   : 27/08/2013 22:06:36


----
Longest running query ran for :          : 1368 Seconds
Current undo retention is                : 900 Seconds
Current undo tablespace size is          : 10M
AUTOEXTEND for undo tablespace size is   : ON
----
Recommended undo retention is            : 1369 Seconds
Recommended undo tablespace size is      : 43M


----------------------------------------------------
- Undo Analysis completed at : 28/08/2013 11:03:23 -
----------------------------------------------------

PL/SQL procedure successfully completed.

REFERENCES

NOTE:877613.1 - AUM Common Analysis/Diagnostic Scripts
NOTE:1580225.1 - What is the Undo Advisor and how to Use it through the DBMS_UNDO_ADV package

原文地址:https://www.cnblogs.com/zylong-sys/p/11964114.html