Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
CHAPTER 9 ■ REDO AND UNDO 325 ops$tkyte@ORA10G> update t set indexed = lower(indexed); 48771 rows updated. ops$tkyte@ORA10G> select used_ublk ... 10 / USED_UBLK ---------- 1938 As you can see, updating that indexed column in this example generated almost five times the undo. This is due to the inherit complexity of the index structure itself and the fact that we updated every single row in the table—moving every single index key value in this structure. ORA-01555: snapshot too old Error In the last chapter, we briefly investigated the ORA-01555 error and looked at one cause of it: committing too frequently. Here we are going to take a much more detailed look at the causes and solutions for the ORA-01555 error. ORA-01555 is one of those errors that confound people. It is the foundation for many myths, inaccuracies, and suppositions. ■Note ORA-01555 is not related to data corruption or data loss at all. It is a “safe” error in that regard; the only outcome is that the query that received this error is unable to continue processing. The error is actually straightforward and has only two real causes, but since there is a special case of one of them that happens so frequently, I’ll say that there are three: • The undo segments are too small for the work you perform on your system. • Your programs fetch across COMMITs (actually a variation on the preceding point). We covered this in the last chapter. • Block cleanout. The first two points are directly related to Oracle’s read consistency model. As you recall from Chapter 7, the results of your query are pre-ordained, meaning they are well defined before Oracle goes to retrieve even the first row. Oracle provides this consistent point in time “snapshot” of the database by using the undo segments to roll back blocks that have changed since your query began. Every statement you execute, such as the following: update t set x = 5 where x = 2; insert into t select * from t where x = 2; delete from t where x = 2; select * from t where x = 2;
326 CHAPTER 9 ■ REDO AND UNDO will see a read-consistent view of T and the set of rows where X=2, regardless of any other concurrent activity in the database. ■Note The four statements presented here are just examples of the types of statements that would see a read-consistent view of T. They were not meant to be run as a single transaction in the database, as the first update would cause the following three statements to see no records. They are purely illustrative. All statements that “read” the table take advantage of this read consistency. In the example just shown, the UPDATE reads the table to find rows where x=2 (and then UPDATEs them). The INSERT reads the table to find where X=2, and then INSERTs them, and so on. It is this dual use of the undo segments, both to roll back failed transactions and to provide for read consistency, that results in the ORA-01555 error. The third item in the previous list is a more insidious cause of ORA-01555, in that it can happen in a database where there is a single session, and this session is not modifying the tables that are being queried when the ORA-01555 error is raised! This doesn’t seem possible— why would we need undo data for a table we can guarantee is not being modified? We’ll find out shortly. Before we take a look at all three cases with illustrations, I’d like to share with you the solutions to the ORA-1555 error, in general: • Set the parameter UNDO_RETENTION properly (larger than the amount of time it takes to execute your longest running transaction). V$UNDOSTAT can be used to determine the duration of your long-running queries. Also, ensure sufficient space on disk has been set aside so the undo segments are allowed to grow to the size they need to be based on the requested UNDO_RETENTION. • Increase or add more rollback segments when using manual undo management. This decreases the likelihood of undo data being overwritten during the course of your longrunning query. This method goes toward solving all three of the previous points. • Reduce the runtime of your query (tune it). This is always a good thing if possible, so it might be the first thing you try. This will reduce the need for larger undo segments. This method goes toward solving all three of the previous points. • Gather statistics on related objects. This will help avoid the third point listed earlier. Since the block cleanout is the result of a very large mass UPDATE or INSERT, this needs to be done anyway after a mass UPDATE or large load. We’ll come back to these solutions, as they are important facts to know. It seemed appropriate to display them prominently before we begin. Undo Segments Are in Fact Too Small The scenario is this: you have a system where the transactions are small. As a result of this, you need very little undo segment space allocated. Say, for example, the following is true:
- Page 320 and 321: CHAPTER 8 ■ TRANSACTIONS 275 Auto
- Page 322 and 323: CHAPTER 8 ■ TRANSACTIONS 277 3 Au
- Page 324 and 325: CHAPTER 8 ■ TRANSACTIONS 279 5 pr
- Page 326: CHAPTER 8 ■ TRANSACTIONS 281 scot
- Page 329 and 330: 284 CHAPTER 9 ■ REDO AND UNDO cri
- Page 331 and 332: 286 CHAPTER 9 ■ REDO AND UNDO Fir
- Page 333 and 334: 288 CHAPTER 9 ■ REDO AND UNDO The
- Page 335 and 336: 290 CHAPTER 9 ■ REDO AND UNDO We
- Page 337 and 338: 292 CHAPTER 9 ■ REDO AND UNDO Wha
- Page 339 and 340: 294 CHAPTER 9 ■ REDO AND UNDO row
- Page 341 and 342: 296 CHAPTER 9 ■ REDO AND UNDO If
- Page 343 and 344: 298 CHAPTER 9 ■ REDO AND UNDO ops
- Page 345 and 346: 300 CHAPTER 9 ■ REDO AND UNDO Inv
- Page 347 and 348: 302 CHAPTER 9 ■ REDO AND UNDO The
- Page 349 and 350: 304 CHAPTER 9 ■ REDO AND UNDO 41
- Page 351 and 352: 306 CHAPTER 9 ■ REDO AND UNDO ins
- Page 353 and 354: 308 CHAPTER 9 ■ REDO AND UNDO So,
- Page 355 and 356: 310 CHAPTER 9 ■ REDO AND UNDO ops
- Page 357 and 358: 312 CHAPTER 9 ■ REDO AND UNDO ops
- Page 359 and 360: 314 CHAPTER 9 ■ REDO AND UNDO •
- Page 361 and 362: 316 CHAPTER 9 ■ REDO AND UNDO ...
- Page 363 and 364: 318 CHAPTER 9 ■ REDO AND UNDO •
- Page 365 and 366: 320 CHAPTER 9 ■ REDO AND UNDO bac
- Page 367 and 368: 322 CHAPTER 9 ■ REDO AND UNDO As
- Page 369: 324 CHAPTER 9 ■ REDO AND UNDO ops
- Page 373 and 374: 328 CHAPTER 9 ■ REDO AND UNDO Thi
- Page 375 and 376: 330 CHAPTER 9 ■ REDO AND UNDO ops
- Page 377 and 378: 332 CHAPTER 9 ■ REDO AND UNDO Whe
- Page 379 and 380: 334 CHAPTER 9 ■ REDO AND UNDO Tha
- Page 381 and 382: 336 CHAPTER 9 ■ REDO AND UNDO tou
- Page 383 and 384: 338 CHAPTER 10 ■ DATABASE TABLES
- Page 385 and 386: 340 CHAPTER 10 ■ DATABASE TABLES
- Page 387 and 388: 342 CHAPTER 10 ■ DATABASE TABLES
- Page 389 and 390: 344 CHAPTER 10 ■ DATABASE TABLES
- Page 391 and 392: 346 CHAPTER 10 ■ DATABASE TABLES
- Page 393 and 394: 348 CHAPTER 10 ■ DATABASE TABLES
- Page 395 and 396: 350 CHAPTER 10 ■ DATABASE TABLES
- Page 397 and 398: 352 CHAPTER 10 ■ DATABASE TABLES
- Page 399 and 400: 354 CHAPTER 10 ■ DATABASE TABLES
- Page 401 and 402: 356 CHAPTER 10 ■ DATABASE TABLES
- Page 403 and 404: 358 CHAPTER 10 ■ DATABASE TABLES
- Page 405 and 406: 360 CHAPTER 10 ■ DATABASE TABLES
- Page 407 and 408: 362 CHAPTER 10 ■ DATABASE TABLES
- Page 409 and 410: 364 CHAPTER 10 ■ DATABASE TABLES
- Page 411 and 412: 366 CHAPTER 10 ■ DATABASE TABLES
- Page 413 and 414: 368 CHAPTER 10 ■ DATABASE TABLES
- Page 415 and 416: 370 CHAPTER 10 ■ DATABASE TABLES
- Page 417 and 418: 372 CHAPTER 10 ■ DATABASE TABLES
- Page 419 and 420: 374 CHAPTER 10 ■ DATABASE TABLES
326<br />
CHAPTER 9 ■ REDO AND UNDO<br />
will see a read-consistent view of T <strong>and</strong> the set of rows where X=2, regardless of any other concurrent<br />
activity in the database.<br />
■Note The four statements presented here are just examples of the types of statements that would see a<br />
read-consistent view of T. They were not meant to be run as a single transaction in the database, as the first<br />
update would cause the following three statements to see no records. They are purely illustrative.<br />
All statements that “read” the table take advantage of this read consistency. In the example<br />
just shown, the UPDATE reads the table to find rows where x=2 (<strong>and</strong> then UPDATEs them). The<br />
INSERT reads the table to find where X=2, <strong>and</strong> then INSERTs them, <strong>and</strong> so on. It is this dual use<br />
of the undo segments, both to roll back failed transactions <strong>and</strong> to provide for read consistency,<br />
that results in the ORA-01555 error.<br />
The third item in the previous list is a more insidious cause of ORA-01555, in that it can<br />
happen in a database where there is a single session, <strong>and</strong> this session is not modifying the<br />
tables that are being queried when the ORA-01555 error is raised! This doesn’t seem possible—<br />
why would we need undo data for a table we can guarantee is not being modified? We’ll find<br />
out shortly.<br />
Before we take a look at all three cases with illustrations, I’d like to share with you the<br />
solutions to the ORA-1555 error, in general:<br />
• Set the parameter UNDO_RETENTION properly (larger than the amount of time it takes to<br />
execute your longest running transaction). V$UNDOSTAT can be used to determine the<br />
duration of your long-running queries. Also, ensure sufficient space on disk has been<br />
set aside so the undo segments are allowed to grow to the size they need to be based on<br />
the requested UNDO_RETENTION.<br />
• Increase or add more rollback segments when using manual undo management. This<br />
decreases the likelihood of undo data being overwritten during the course of your longrunning<br />
query. This method goes toward solving all three of the previous points.<br />
• Reduce the runtime of your query (tune it). This is always a good thing if possible, so it<br />
might be the first thing you try. This will reduce the need for larger undo segments. This<br />
method goes toward solving all three of the previous points.<br />
• Gather statistics on related objects. This will help avoid the third point listed earlier.<br />
Since the block cleanout is the result of a very large mass UPDATE or INSERT, this needs<br />
to be done anyway after a mass UPDATE or large load.<br />
We’ll come back to these solutions, as they are important facts to know. It seemed appropriate<br />
to display them prominently before we begin.<br />
Undo Segments Are in Fact Too Small<br />
The scenario is this: you have a system where the transactions are small. As a result of this, you<br />
need very little undo segment space allocated. Say, for example, the following is true: