|
An error is reported from the client, indicating that the log shows that an error info is thrown when an SQL statement is executed as follows:
Java. SQL. sqlexception: ORA-01555: Snapshot too old: rollback segment number 2 with name "_ syssmu2 $"
Too small
After searching, I finally got a little understanding of this error. Below is an article about ORA-01555:
Read consistency and block cleanouts)
Oracle always enforces statement-level read consistency. this guarantees that the data returned by a single query is consistent with respect to time when the query began. therefore, a query never sees the data-changes made by transactions that commit
The course of execution of the query.
Oracle uniquely identifies any given point in time by a set of numbers called the system change numbers (SCN ). so SCN can be defined as the state of the database at any one given point in time. to produce read-consistency, Oracle marks the current SCN as
Query enters the execution phase. The query can only see the snapshot of the records as they were at the time of marked SCN.
Oracle uses rollback segments to reconstruct the read-consistent snapshot of the data. whenever a transaction makes any changes, a snapshot of the record before the changes were made is copied to a rollback segment and the data block header is marked appropriately
With the address of the rollback segment block where the changes are recorded. The data block also maintains the SCN of the last committed change to the block.
[To ensure read consistency, data in a query statement is retrieved from snapshot during execution, ignoring the modifications made by other transactions during query execution. Snapshot is managed in the rollback segment. Each time a transaction modifies some data, the DB will store the data snapshots before the modification in the data block of the rollback segment.]
Causes and Solutions of ORA-01555
As the data blocks are read on behalf of the query, only blocks with lower SCN than the query SCN will be read. if a block has uncommitted changes of other transactions or changed data with more recent SCN, then the data is reconstructed using the saved Snapshot
From the rollback segments. In some rare situations, if RDBMS is not able to reconstruct the snapshot for a long running query, the query results in an ORA-01555 error.
[Query data blocks with a smaller value than the SCN Number of the read-only rollback segment at the beginning of each query (these blocks have not been modified during query execution ). If the data of a data block is modified (the SCN number is large), the corresponding snapshot in the rollback segment needs to be rebuilt. In some cases, if a snapshot reconstruction fails due to a long execution time of a query, A ORA-01555 error occurs ???? (What do you mean)
A rollback segment maintains the snapshot of the changed data as long as the transaction is still active (commit or rollback has not been issued ). once a transaction is committed, RDBMS marks it with current SCN and the space used by the snapshot becomes available
For reuse.
[After the transaction is inactive, the space of snapshot is released (it can be overwritten ).??? Isn't it said that if some data is modified by another transaction during the query, there will be a ora-01555 problem ???
However, the database has a parameter (undo_retention) that specifies the snapshot retention time. If the query execution time exceeds this parameter and the current rollback segment is insufficient, the space of snapshot is also occupied. ]
Therefore, ORA-01555 will result if the query is looking for the snapshot which is so old that rollback segment information cocould not be found because of wrap around or overwrite.
[That is, the cause of the ORA-01555 is that the snapshot was modified (cleared or overwritten) during the query execution ). ]
Situations where ORA-01555 errors commonly occur:
1. fewer and smaller rollback segments for a very actively changing Database
If the database has transactions changing data and commiting very often, then the chance of reusing the space used by a committed transaction is higher. A long running query then may not be able to reconstruct the snapshot due to wrap around and overwrite
In rollback segments. Larger rollback segments in this case will reduce the chance of reusing the committed transaction slots.
2. Synchronized upted rollback segment
If the rollback segment is already upted and cocould not be read, then a statement needing to reconstruct a before image snapshot will result in the error.
3. Fetch upload SS commit
This is the situation when a query opens a cursor, then loops through fetching, changing, and committing the records on the same table. In this scenerio, very often an ORA-01555 can result.
Let's take the following example to explain this:
A cursor was opened at SCN = 10. the execution SCN of the query is then marked as SCN = 10. every fetch by that cursor now needs to get the read-consistent data from SCN = 10. the user program is now fetching X numbers of records, changing them, and committing them.
Let's say they were committed with SCN = 20. if a later fetch happens to retrieve a record which is in one of the previously committed blocks, then the fetch will see that the SCN there as 20. since the fetch has to get the snapshot from SCN = 10 it will try
Find it in the rollback segments. If it cocould rollback sufficiently backwards as previously explained, then it cocould reconstruct the snapshot from SCN = 10. If not, then it will result in an ORA-01555 error.
Committing less often which will result in larger rollback segments will reduce the probability of getting 'snapshot too old' error.
4. Fetch into SS commits with delayed block clean out
To complicate things, now we see how delayed block clean outs play an important role in getting this error.
When a data or index block is modified in the database and the transaction committed, oracle does a fast commit by marking the transaction as committed in the rollback segment header but does not clean the datablocks that were modified. the next transaction
Which does a select on the modified blocks will do the actual cleanout of the block. This is known as a delayed block cleanout.
Now, take the same scenario as described in previous section. but instead of assuming one table, let us assume that there are two tables in question. i. e: the cursor is opened and then in a loop, it fetches from one table and changes records in another, and
Commits. Even though the records are getting committed in another table it cocould still cause ORA-01555 because cleanout has not been done on the table from which the records are being fetched.
For this case, a full table scan before opening and fetching through the cursor will help.
Summary:
Fetches into SS commits as explained in last two cases are not supported by ANSI standard. according to ANSI standard a cursor is invalidated when a commit is already med and shoshould be closed and reopened. oracle allows users to do fetch into SS commits but users
Shocould be aware that it might result in ORA-01555.
Related dB Configuration:
Undo_retention
Undo_retention specifies (in seconds) the low threshold value of Undo retention. for autoextend undo tablespaces, the system retains undo for at least the time specified in this parameter, and automatically tunes the Undo retention period to satisfy the Undo
Requirements of the queries. for fixed-size undo tablespaces, the system automatically tunes for the maximum possible undo retention period, based on undo tablespace size and usage history, and ignores undo_retention unless retention guarantee is enabled.
The setting of this parameter shocould account for any flashback requirements of the system. automatic Tuning of Undo retention is not supported for lobs. the retention value for LOB columns is set to the value of the undo_retention parameter.
The undo_retention parameter can only be honored if the current undo tablespace has enough space. if an active transaction requires undo space and the Undo tablespace does not have available space, then the system starts reusing unexpired undo space. this action
Can potentially cause some queries to fail with a "snapshot too old" message.
Solution:
Alter system set undo_retention = 10800 scope = both; (set to 3 hours) I actually set it to 1 hour, or, depending on the actual situation!
Reprinted from: http://hi.baidu.com/hihengsheng/blog/item/80d22a53db5d622943a75b3e.html |