首页 > 数据库 > Oracle > 浅析undo表空间的管理方式
这几天,没怎么总结一些知识。
一、什么是undo
1)还原数据
当某个进程更改了数据库中的数据时,Oracle
DB 会保存旧值(还原数据)。按数据修改前的原样存储数据。如果捕获了还原数据,则可以回退未提交的数据。还原数据用于支持读取一致性和闪回查询。还原还可用于“倒回”(闪回)事务处理和表。
在读取一致性查询中,提供与查询开始时的数据一致的结果。如果要成功完成读取一致性查询,原始信息必须仍作为还原信息存在。如果原始数据不再可用,则会收到“快照太旧”错误。只要保留了还原信息,Oracle DB 就能通过重建数据来满足读取一致性查询的要求。
闪回查询有目的地查找过去某个时间存在的某个版本的数据。只要过去那个时间的还原信息仍存在,闪回查询就能成功完成。闪回事务处理使用还原功能来创建补偿事务处理,以便回退事务处理及其相关事务处理。使用闪回表功能可将表恢复到特定的时间点。
还原数据也可用于从失败的事务处理中进行恢复。如果在用户决定提交或回退事务处理之前用户会话异常结束(可能因为网络错误或客户机故障),就会导致事务处理失败。当实例崩溃或发出 SHUTDOWN ABORT 命令时,事务处理也可能会失败。
2)事务处理和还原数据
事务处理开始时就会分配还原段。在事务处理的整个过程中,更改数据时,原始(更改之前)值会被复制到还原段。通过选中 V$TRANSACTION 动态性能视图,可以查看哪些事务处理被分配到哪些还原段。
还原段是为了支持事务处理,由实例自动创建的专用段。像所有段一样,还原段由区组成,区又由数据块组成。还原段根据需要可自动增长和收缩,它充当所分配的事务处理的循环存储缓冲区。
事务处理会填充其还原段中的区,直至完成了事务处理或占用了所有空间为止。如果填充完区之后还需要更多的空间,事务处理则获取段中下一个区的空间。占用了所有区之后,事务处理会自动转回到第一个区或请求为还原段分配新区。

3)还原数据与重做数据
还原数据和重做数据初看很相似,但是二者的作用却截然不同。如果需要还原更改,则需要还原数据,而且为了保持读取一致性和执行回退,也需要还原数据。在因某种原因而丢失了更改时,如果需要再次执行更改,则需要用到重做数据。还原块更改也可以写入重做日志。
提交过程需要验证在事务处理中所做的更改是否已写入重做日志文件。这个重做日志文件会永久保存在磁盘上,而不是保存在内存中。此外,重做日志文件通常被多路复用。因此,磁盘上有重做数据的多个副本。虽然更改可能尚未写入实际存储表块的数据文件,但只要写入到持久重做日志,就足以确保数据库的一致性。
假设在已提交的更改反映到数据文件中之前刚好发生断电。因为事务处理已提交,所有这种情况不会导致问题。当系统再次启动时,就能够前滚断电时尚未反映到数据文件中的任何重做记录。
总结下来Oracle使用undo做以下作用:
| Column | Datatype | Description |
|---|---|---|
BEGIN_TIME
|
DATE
|
Identifies the beginning of the time interval |
END_TIME
|
DATE
|
Identifies the end of the time interval |
UNDOTSN
|
NUMBER
|
Represents the last active undo tablespace in the duration of time. The tablespace ID of the active undo tablespace is returned in this column. If more than one undo tablespace was active in that period, the active undo tablespace that was active at the end of the period is reported. |
UNDOBLKS
|
NUMBER
|
Represents the total number of undo blocks consumed. You can use this column to obtain the consumption rate of undo blocks, and thereby estimate the size of the undo tablespace needed to handle the workload on your system. |
TXNCOUNT
|
NUMBER
|
Identifies the total number of transactions executed within the period |
MAXQUERYLEN
|
NUMBER
|
Identifies the length of the longest query (in seconds) executed in the instance during the period. You can use this statistic to estimate the proper setting of the UNDO_RETENTIONinitialization parameter. The length of a query is measured from the cursor open time to the last fetch/execute time of the cursor. Only the length of those cursors that have been fetched/executed during the period are reflected in the view.
|
MAXQUERYID
|
VARCHAR2(13)
|
SQL identifier of the longest running SQL statement in the period |
MAXCONCURRENCY
|
NUMBER
|
Identifies the highest number of transactions executed concurrently within the period |
UNXPSTEALCNT
|
NUMBER
|
Number of attempts to obtain undo space by stealing unexpired extents from other transactions |
UNXPBLKRELCNT
|
NUMBER
|
Number of unexpired blocks removed from certain undo segments so they can be used by other transactions |
UNXPBLKREUCNT
|
NUMBER
|
Number of unexpired undo blocks reused by transactions |
EXPSTEALCNT
|
NUMBER
|
Number of attempts to steal expired undo blocks from other undo segments |
EXPBLKRELCNT
|
NUMBER
|
Number of expired undo blocks stolen from other undo segments |
EXPBLKREUCNT
|
NUMBER
|
Number of expired undo blocks reused within the same undo segments |
SSOLDERRCNT
|
NUMBER
|
Identifies the number of times the error ORA-01555 occurred. You can use this statistic to decide whether or not the UNDO_RETENTIONinitialization parameter is set properly given the size of the undo tablespace. Increasing the value of UNDO_RETENTION can reduce the occurrence of this error.
|
NOSPACEERRCNT
|
NUMBER
|
Identifies the number of times space was requested in the undo tablespace and there was no free space available. That is, all of the space in the undo tablespace was in use by active transactions. The corrective action is to add more space to the undo tablespace. |
ACTIVEBLKS
|
NUMBER
|
Total number of blocks in the active extents of the undo tablespace for the instance at the sampled time in the period |
UNEXPIREDBLKS
|
NUMBER
|
Total number of blocks in the unexpired extents of the undo tablespace for the instance at the sampled time in the period |
EXPIREDBLKS
|
NUMBER
|
Total number of blocks in the expired extents of the undo tablespace for the instance at the sampled time in the period |
TUNED_UNDORETENTION
|
NUMBER
|
Amount of time (in seconds) for which undo will not be recycled from the time it was committed. At any point in time, the latest value ofTUNED_UNDORETENTION is used to determine whether data committed at a particular time in the past can be recycled.
|
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30430420/viewspace-1813697/ ,如需转载,请注明出处,否则将追究法律责任。