溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

UNDO Segment深入解析

發(fā)布時間:2020-07-01 00:06:05 來源:網(wǎng)絡 閱讀:3977 作者:客居天涯 欄目:關系型數(shù)據(jù)庫

Undo Segment深入解析

  在undo自動管理時,設置了undo_retention以后,undo塊就存在四種狀態(tài)。

Active:表示正在使用該undo的事務還沒有提交或回滾。
Inactive:表示該undo上沒有活動的事務,該狀態(tài)的undo可以被其他事務覆蓋。
Expired:表示該undo持續(xù)inactive的時間超過undo_retention所指定的時間。
Freed:表示該undo塊內(nèi)容是空的,從來沒有被使用過。

Undo Retention

     After a transaction is committed, undo data is no longer needed for rollback or transaction recovery purposes. However, for consistent read purposes, long-running queries may require this old undo information for producing older p_w_picpaths of data blocks. Furthermore, the success of several Oracle Flashback features can also depend upon the availability of older undo information. For these reasons, it is desirable to retain the old undo information for as long as possible.
When automatic undo management is enabled, there is always a current undo retention period, which is the minimum amount of time that Oracle Database attempts to retain old undo information before overwriting it. Old (committed) undo information that is older than the current undo retention period is said to be expired. Old undo information with an age that is less than the current undo retention period is said to be unexpired.
Oracle Database automatically tunes the undo retention period based on undo tablespace size and system activity. You can specify a minimum undo retention period (in seconds) by setting the UNDO_RETENTION initialization parameter. The database makes its best effort to honor the specified minimum undo retention period, provided that the undo tablespace has space available for new transactions. When available space for new transactions becomes short, the database begins to overwrite expired undo. If the undo tablespace has no space for new transactions after all expired undo is overwritten, the database may begin overwriting unexpired undo information. If any of this overwritten undo information is required for consistent read in a current long-running query, the query could fail with the snapshot too old error message.
The following points explain the exact impact of the UNDO_RETENTION parameter on undo retention:
The UNDO_RETENTION parameter is ignored for a fixed size undo tablespace. The database may overwrite unexpired undo information when tablespace space becomes low.
For an undo tablespace with the AUTOEXTEND option enabled, the database attempts to honor the minimum retention period specified by UNDO_RETENTION. When space is low, instead of overwriting unexpired undo information, the tablespace auto-extends. If the MAXSIZE clause is specified for an auto-extending undo tablespace, when the maximum size is reached, the database may begin to overwrite unexpired undo information.
Automatic Tuning of Undo Retention
Oracle Database automatically tunes the undo retention period based on how the undo tablespace is configured.
If the undo tablespace is fixed size, the database tunes the retention period for the best possible undo retention for that tablespace size and the current system load. This tuned retention period can be significantly greater than the specified minimum retention period.
If the undo tablespace is configured with the AUTOEXTEND option, the database tunes the undo retention period to be somewhat longer than the longest-running query on the system at that time. Again, this tuned retention period can be greater than the specified minimum retention period.
Note:
Automatic tuning of undo retention is not supported for LOBs. This is because undo information for LOBs is stored in the segment itself and not in the undo tablespace. For LOBs, the database attempts to honor the minimum undo retention period specified by UNDO_RETENTION. However, if space becomes low, unexpired LOB undo information may be overwritten.
You can determine the current retention period by querying the TUNED_UNDORETENTION column of the V$UNDOSTAT view. This view contains one row for each 10-minute statistics collection interval over the last 4 days. (Beyond 4 days, the data is available in the DBA_HIST_UNDOSTAT view.) TUNED_UNDORETENTION is given in seconds.
select to_char(begin_time, 'DD-MON-RR HH24:MI') begin_time,
to_char(end_time, 'DD-MON-RR HH24:MI') end_time, tuned_undoretention
from v$undostat order by end_time;
BEGIN_TIME      END_TIME        TUNED_UNDORETENTION
--------------- --------------- -------------------
04-FEB-05 00:01 04-FEB-05 00:11               12100
      ...                                          
07-FEB-05 23:21 07-FEB-05 23:31               86700
07-FEB-05 23:31 07-FEB-05 23:41               86700
07-FEB-05 23:41 07-FEB-05 23:51               86700
07-FEB-05 23:51 07-FEB-05 23:52               86700
576 rows selected.


   回滾段自動管理模式下UNDO block分配算法:

1、如果當前extent有空閑的數(shù)據(jù)塊,則使用當前extent

2、如果當前extent的下一個extent已經(jīng)處于過期(expired)狀態(tài),那么環(huán)繞(wrap)到一個extent,然后使用該extent的第一個數(shù)據(jù)塊。

3、如果下一個extent沒有expired,則從undo tablespace中分配空間。如果有剩余空間則使用新分配extent的第一個數(shù)據(jù)塊。此時undo tablespace的使用率開始增加。

4、如果沒有剩余空閑的extent,則從offline狀態(tài)的回滾段中偷?。╯teal)過期的extent,加入當前回滾段,并使用第一個數(shù)據(jù)塊。

5、如果offline狀態(tài)的回滾段中沒有expired extent,則從online狀態(tài)的回滾段中偷?。╯teal)過期區(qū)加入到當前的回滾段中,并使用extent中的第一個數(shù)據(jù)塊。

6、如果undo tablespace可以擴展,則擴展undo tablespace,并將新extent加入到當前的回滾段中,同時使用第一個數(shù)據(jù)塊,此時undo所占的操作系統(tǒng)空間開始增加。

7、如果undo tablespace 不能擴展,則自動調(diào)整(下降幅度為10%)回滾段的保留時間,然后偷取在更短保留時間下的未過期的extent,如果還未找到過期的extent,則繼續(xù)以10%的速度減少回滾段的保留時間,重復幾次。

8、隨機從其他offline狀態(tài)的回滾段中偷取未過期(unexpired)的extent。

9、嘗試使用當前回滾段中未過期的extent,如果所有的區(qū)都為ACTIVE狀態(tài),則進入下一步。

10、如果以上所有的嘗試都失敗,則報ORA-30036的錯誤。

[oracle@rh7 ~]$ oerr ora 30036
30036, 00000, "unable to extend segment by %s in undo tablespace '%s'"
// *Cause:   the specified undo tablespace has no more space available.
// *Action:  Add more space to the undo tablespace before retrying
//           the operation. An alternative is to wait until active
//           transactions to commit.

案例:

10:34:45 SYS@ prod>select tablespace_name,contents from dba_tablespaces;

TABLESPACE_NAME                CONTENTS
------------------------------ ---------
SYSTEM                         PERMANENT
SYSAUX                         PERMANENT
TEMP                           TEMPORARY
USERS                          PERMANENT
UNDOTBS2                       UNDO
EXAMPLE                        PERMANENT
TBS1                           PERMANENT

7 rows selected.

Elapsed: 00:00:00.03
10:34:56 SYS@ prod>create undo tablespace undotbs1
10:35:15   2  datafile '/u01/app/oracle/oradata/prod/undotbs1.dbf' size 1m;

Tablespace created.

Elapsed: 00:00:01.38
10:35:40 SYS@ prod>show parameter undo

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
undo_management                      string      AUTO
undo_retention                       integer     900
undo_tablespace                      string      UNDOTBS2

10:35:49 SYS@ prod>alter system set undo_tablespace=undotbs1;

System altered.

Elapsed: 00:00:00.13
10:36:03 SYS@ prod>


10:36:03 SYS@ prod>select s.sid,s.serial#,s.username,u.segment_name,count(u.extent_id) "Extent count",
10:37:37   2  t.used_ublk,t.used_urec,s.program
10:38:00   3   from v$session s,v$transaction t,dba_undo_extents u
10:38:24   4  where s.taddr=t.addr and u.segment_name like '_SYSSMU'||t.xidusn||'_%$' and u.status='ACTIVE'
10:39:50   5  GROUP BY s.sid,s.serial#,s.username,u.segment_name,t.used_ublk,t.used_urec,s.program
10:40:37   6  order by t.used_ublk desc,t.used_urec desc,s.sid,s.serial#,s.username,s.program;

開啟新的session,執(zhí)行DML:

10:41:45 SYS@ prod>conn scott/tiger
Connected.
10:42:45 SCOTT@ prod>insert into t1 select * from t1 where rownum <1000;

999 rows created.

查看undo tablespace 使用情況:

10:43:29 SYS@ prod> select s.sid,s.serial#,s.username,u.segment_name,count(u.extent_id) "Extent count",
  2  t.used_ublk,t.used_urec,s.program
  3   from v$session s,v$transaction t,dba_undo_extents u
  4  where s.taddr=t.addr and u.segment_name like '_SYSSMU'||t.xidusn||'_%$' and u.status='ACTIVE'
  5  GROUP BY s.sid,s.serial#,s.username,u.segment_name,t.used_ublk,t.used_urec,s.program
  6* order by t.used_ublk desc,t.used_urec desc,s.sid,s.serial#,s.username,s.program

       SID    SERIAL# USERNAME   SEGMENT_NAME         Extent count  USED_UBLK  USED_UREC PROGRAM
---------- ---------- ---------- -------------------- ------------ ---------- ---------- ----------
        37         36 SCOTT      _SYSSMU12_2144756092            1          1         11 sqlplus@rh
                                 $                                                       6 (TNS V1-
                                                                                         V3)
Elapsed: 00:00:00.20

10:45:01 SCOTT@ prod>insert into t1 select * from t1 where rownum <20000;

19999 rows created.

Elapsed: 00:00:00.07


10:44:52 SYS@ prod>select s.sid,s.serial#,s.username,u.segment_name,count(u.extent_id) "Extent count",
  2  t.used_ublk,t.used_urec,s.program
  3   from v$session s,v$transaction t,dba_undo_extents u
  4  where s.taddr=t.addr and u.segment_name like '_SYSSMU'||t.xidusn||'_%$' and u.status='ACTIVE'
  5  GROUP BY s.sid,s.serial#,s.username,u.segment_name,t.used_ublk,t.used_urec,s.program
  6* order by t.used_ublk desc,t.used_urec desc,s.sid,s.serial#,s.username,s.program

       SID    SERIAL# USERNAME   SEGMENT_NAME         Extent count  USED_UBLK  USED_UREC PROGRAM
---------- ---------- ---------- -------------------- ------------ ---------- ---------- ----------
        37         36 SCOTT      _SYSSMU16_2726800344            2          7        109 sqlplus@rh
                                 $                                                       6 (TNS V1-
                                                                                         V3)

Elapsed: 00:00:00.01
10:45:13 SYS@ prod>

10:48:16 SYS@ prod>select a.usn,a.name,b.xacts,b.extents,b.status from v$rollname a,v$rollstat b
10:49:01   2  where a.usn=b.usn;

       USN NAME                                XACTS    EXTENTS STATUS
---------- ------------------------------ ---------- ---------- ---------------
         0 SYSTEM                                  0          6 ONLINE
        11 _SYSSMU11_2517864848$                   0          2 ONLINE
        12 _SYSSMU12_2144756092$                   0          2 ONLINE
        13 _SYSSMU13_527038519$                    0          3 ONLINE
        14 _SYSSMU14_2951869305$                   0          2 ONLINE
        15 _SYSSMU15_2206823906$                   0          2 ONLINE
        16 _SYSSMU16_2726800344$                   1          2 ONLINE
        17 _SYSSMU17_2098084560$                   0          2 ONLINE
        
 10:50:45 SYS@ prod>select SEGMENT_NAME,TABLESPACE_NAME,EXTENT_ID,STATUS from dba_undo_extents

SEGMENT_NAME              TABLESPACE_NAME                 EXTENT_ID STATUS
------------------------- ------------------------------ ---------- ---------
_SYSSMU17_2098084560$     UNDOTBS1                                0 UNEXPIRED
_SYSSMU17_2098084560$     UNDOTBS1                                1 UNEXPIRED
_SYSSMU16_2726800344$     UNDOTBS1                                0 ACTIVE
_SYSSMU16_2726800344$     UNDOTBS1                                1 ACTIVE
_SYSSMU15_2206823906$     UNDOTBS1                                0 UNEXPIRED
_SYSSMU15_2206823906$     UNDOTBS1                                1 UNEXPIRED
_SYSSMU14_2951869305$     UNDOTBS1                                0 UNEXPIRED
_SYSSMU14_2951869305$     UNDOTBS1                                1 UNEXPIRED
_SYSSMU13_527038519$      UNDOTBS1                                0 UNEXPIRED
_SYSSMU13_527038519$      UNDOTBS1                                1 UNEXPIRED
_SYSSMU13_527038519$      UNDOTBS1                                2 UNEXPIRED
_SYSSMU12_2144756092$     UNDOTBS1                                0 UNEXPIRED
_SYSSMU12_2144756092$     UNDOTBS1                                1 UNEXPIRED
_SYSSMU11_2517864848$     UNDOTBS1                                0 UNEXPIRED
_SYSSMU11_2517864848$     UNDOTBS1                                1 UNEXPIRED
_SYSSMU30_1737877121$     UNDOTBS2                                0 EXPIRED
_SYSSMU30_1737877121$     UNDOTBS2                                1 UNEXPIRED

SEGMENT_NAME              TABLESPACE_NAME                 EXTENT_ID STATUS
------------------------- ------------------------------ ---------- ---------
_SYSSMU30_1737877121$     UNDOTBS2                                2 EXPIRED
_SYSSMU29_2754652023$     UNDOTBS2                                0 EXPIRED
_SYSSMU29_2754652023$     UNDOTBS2                                1 EXPIRED
_SYSSMU29_2754652023$     UNDOTBS2                                2 EXPIRED
_SYSSMU29_2754652023$     UNDOTBS2                                3 UNEXPIRED
_SYSSMU28_707429450$      UNDOTBS2                                0 UNEXPIRED
_SYSSMU28_707429450$      UNDOTBS2                                1 EXPIRED
_SYSSMU28_707429450$      UNDOTBS2                                2 EXPIRED
_SYSSMU27_3269963619$     UNDOTBS2                                0 EXPIRED
_SYSSMU27_3269963619$     UNDOTBS2                                1 EXPIRED
_SYSSMU27_3269963619$     UNDOTBS2                                2 EXPIRED
_SYSSMU27_3269963619$     UNDOTBS2                                3 UNEXPIRED
_SYSSMU27_3269963619$     UNDOTBS2                                4 EXPIRED
_SYSSMU27_3269963619$     UNDOTBS2                                5 EXPIRED
_SYSSMU26_2968904537$     UNDOTBS2                                0 EXPIRED
_SYSSMU26_2968904537$     UNDOTBS2                                1 EXPIRED
_SYSSMU26_2968904537$     UNDOTBS2                                2 EXPIRED

SEGMENT_NAME              TABLESPACE_NAME                 EXTENT_ID STATUS
------------------------- ------------------------------ ---------- ---------
_SYSSMU26_2968904537$     UNDOTBS2                                3 EXPIRED
_SYSSMU26_2968904537$     UNDOTBS2                                4 UNEXPIRED
_SYSSMU26_2968904537$     UNDOTBS2                                5 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                                0 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                                1 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                                2 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                                3 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                                4 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                                5 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                                6 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                                7 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                                8 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                                9 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               10 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               11 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               12 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               13 EXPIRED

SEGMENT_NAME              TABLESPACE_NAME                 EXTENT_ID STATUS
------------------------- ------------------------------ ---------- ---------
_SYSSMU25_2810228709$     UNDOTBS2                               14 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               15 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               16 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               17 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               18 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               19 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               20 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               21 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               22 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               23 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               24 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               25 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               26 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               27 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               28 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               29 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               30 EXPIRED

SEGMENT_NAME              TABLESPACE_NAME                 EXTENT_ID STATUS
------------------------- ------------------------------ ---------- ---------
_SYSSMU25_2810228709$     UNDOTBS2                               31 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               32 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               33 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               34 EXPIRED
_SYSSMU25_2810228709$     UNDOTBS2                               35 UNEXPIRED
_SYSSMU24_386518199$      UNDOTBS2                                0 EXPIRED
_SYSSMU24_386518199$      UNDOTBS2                                1 UNEXPIRED
_SYSSMU24_386518199$      UNDOTBS2                                2 EXPIRED
_SYSSMU23_4084707454$     UNDOTBS2                                0 EXPIRED
_SYSSMU23_4084707454$     UNDOTBS2                                1 UNEXPIRED
_SYSSMU23_4084707454$     UNDOTBS2                                2 EXPIRED
_SYSSMU22_3375463809$     UNDOTBS2                                0 UNEXPIRED
_SYSSMU22_3375463809$     UNDOTBS2                                1 UNEXPIRED
_SYSSMU22_3375463809$     UNDOTBS2                                2 EXPIRED
_SYSSMU22_3375463809$     UNDOTBS2                                3 EXPIRED
_SYSSMU21_2312338076$     UNDOTBS2                                0 EXPIRED
_SYSSMU21_2312338076$     UNDOTBS2                                1 EXPIRED

SEGMENT_NAME              TABLESPACE_NAME                 EXTENT_ID STATUS
------------------------- ------------------------------ ---------- ---------
_SYSSMU21_2312338076$     UNDOTBS2                                2 UNEXPIRED

86 rows selected.

Elapsed: 00:00:00.06


10:52:33 SYS@ prod>alter session set nls_date_format='yyyy-mm-dd hh34:mi:ss';

Session altered.

Elapsed: 00:00:00.02
10:53:03 SYS@ prod>select BEGIN_TIME,END_TIME,UNDOTSN,UNDOBLKS,TUNED_UNDORETENTION from v$undostat;

BEGIN_TIME          END_TIME               UNDOTSN   UNDOBLKS TUNED_UNDORETENTION
------------------- ------------------- ---------- ---------- -------------------
2014-07-01 10:52:51 2014-07-01 10:53:08          2          0                 921
2014-07-01 10:42:51 2014-07-01 10:52:51          2         74                 921
2014-07-01 10:32:51 2014-07-01 10:42:51          2         43                1260

估算undo tablespace 的大小:

UndoTablesapce = UR * (UPS * DBS)

UR: undo_retention 
UPS:在業(yè)務高峰期每秒產(chǎn)生的undo blocks的數(shù)量
DBS:undo tablespace的數(shù)據(jù)庫的大小

10:53:08 SYS@ prod>select (UR * (UPS * DBS)) AS "BYTES"
10:56:12   2  from (select value AS UR from v$parameter where name='undo_retention'),
10:57:04   3       (select undoblks/((end_time-begin_time)*900) as UPS
10:58:39   4       FROM v$undostat
10:58:50   5       where undoblks=(select max(undoblks) from v$undostat)),
10:59:29   6       (select block_size as DBS    
10:59:51   7       FROM dba_tablespaces
11:00:01   8      where tablespace_name=(select upper(value) from v$parameter where name='undo_tablespace'));

     BYTES
----------
  87293952

Elapsed: 00:00:00.07
11:00:45 SYS@ prod>

11:00:45 SYS@ prod>select (UR * (UPS * DBS))/1024/1024 AS "BYTES"
11:01:42   2   from (select value AS UR from v$parameter where name='undo_retention'),
11:01:42   3        (select undoblks/((end_time-begin_time)*900) as UPS
11:01:42   4        FROM v$undostat
11:01:42   5        where undoblks=(select max(undoblks) from v$undostat)),
11:01:42   6        (select block_size as DBS    
11:01:42   7        FROM dba_tablespaces
11:01:42   8       where tablespace_name=(select upper(value) from v$parameter where name='undo_tablespace'));

     BYTES
----------
     83.25

Elapsed: 00:00:00.10
11:01:44 SYS@ prod>


以上部分內(nèi)容參考了《Oracle DBA實戰(zhàn)攻略》書中的內(nèi)容,這里表示感謝!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI