Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 9 ■ REDO AND UNDO 299 Table 9-2. Time to ROLLBACK by Transaction Size Rows Inserted Rollback Time Commit Time (Seconds) (Seconds) 10 0.04 0.06 100 0.05 0.04 1,000 0.06 0.06 10,000 0.22 0.06 100,000 1.46 0.07 This is to be expected, as a ROLLBACK has to physically undo the work we’ve done. Similar to a COMMIT, a series of operations must be performed. Before we even get to the ROLLBACK, the database has already done a lot of work. To recap, the following would have happened: • Undo segment records have been generated in the SGA. • Modified data blocks have been generated in the SGA. • A buffered redo log for the preceding two items has been generated in the SGA. • Depending on the size of the preceding three items, and the amount of time spent, some combination of the previous data may be flushed onto disk already. • All locks have been acquired. When we ROLLBACK, • We undo all of the changes made. This is accomplished by reading the data back from the undo segment, and in effect, reversing our operation and then marking the undo entry as applied. If we inserted a row, a ROLLBACK will delete it. If we updated a row, a rollback will reverse the update. If we deleted a row, a rollback will re-insert it again. • All locks held by our session are released, and everyone who was enqueued waiting on locks we held will be released. A COMMIT, on the other hand, just flushes any remaining data in the redo log buffers. It does very little work compared to a ROLLBACK. The point here is that you don’t want to roll back unless you have to. It is expensive since you spend a lot of time doing the work, and you’ll also spend a lot of time undoing the work. Don’t do work unless you’re sure you are going to want to COMMIT it. This sounds like common sense—of course I wouldn’t do all of the work unless I wanted to COMMIT it. Many times, however, I’ve seen a situation where a developer will use a “real” table as a temporary table, fill it up with data, report on it, and then roll back to get rid of the temporary data. In the next section, we’ll talk about true temporary tables and how to avoid this issue.

300 CHAPTER 9 ■ REDO AND UNDO Investigating Redo As a developer, it’s often important to be able to measure how much redo your operations generate. The more redo you generate, the longer your operations will take, and the slower the entire system will be. You are not just affecting your session, but every session. Redo management is a point of serialization within the database. There is just one LGWR in any Oracle instance, and eventually all transactions end up at LGWR, asking it to manage their redo and COMMIT their transaction. The more it has to do, the slower the system will be. By seeing how much redo an operation tends to generate, and testing more than one approach to a problem, you can find the best way to do things. Measuring Redo It is pretty straightforward to see how much redo is being generated, as shown earlier in the chapter. I used the AUTOTRACE built-in feature of SQL*Plus. But AUTOTRACE works only with simple DML—it cannot, for example, be used to view what a stored procedure call did. For that, we’ll need access to two dynamic performance views: • V$MYSTAT, which has just our session’s statistics in it • V$STATNAME, which tells us what each row in V$MYSTAT represents (the name of the statistic we are looking at) I do these sorts of measurements so often that I use two scripts I call mystat and mystat2. The mystat.sql script saves the beginning value of the statistic I’m interested in, such as redo size, in a SQL*Plus variable: set verify off column value new_val V define S="&1" set autotrace off select a.name, b.value from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) like '%' || lower('&S')||'%' / The mystat2.sql script simply prints out the difference between the beginning value and the end value of that statistic: set verify off select a.name, b.value V, to_char(b.value-&V,'999,999,999,999') diff from v$statname a, v$mystat b where a.statistic# = b.statistic# and lower(a.name) like '%' || lower('&S')||'%' / Now we’re ready to measure how much redo a given transaction would generate. All we need to do is this:

CHAPTER 9 ■ REDO AND UNDO 299<br />

Table 9-2. Time to ROLLBACK by Transaction Size<br />

Rows Inserted Rollback Time Commit Time<br />

(Seconds) (Seconds)<br />

10 0.04 0.06<br />

100 0.05 0.04<br />

1,000 0.06 0.06<br />

10,000 0.22 0.06<br />

100,000 1.46 0.07<br />

This is to be expected, as a ROLLBACK has to physically undo the work we’ve done. Similar<br />

to a COMMIT, a series of operations must be performed. Before we even get to the ROLLBACK, the<br />

database has already done a lot of work. To recap, the following would have happened:<br />

• Undo segment records have been generated in the SGA.<br />

• Modified data blocks have been generated in the SGA.<br />

• A buffered redo log for the preceding two items has been generated in the SGA.<br />

• Depending on the size of the preceding three items, <strong>and</strong> the amount of time spent,<br />

some combination of the previous data may be flushed onto disk already.<br />

• All locks have been acquired.<br />

When we ROLLBACK,<br />

• We undo all of the changes made. This is accomplished by reading the data back from<br />

the undo segment, <strong>and</strong> in effect, reversing our operation <strong>and</strong> then marking the undo<br />

entry as applied. If we inserted a row, a ROLLBACK will delete it. If we updated a row, a<br />

rollback will reverse the update. If we deleted a row, a rollback will re-insert it again.<br />

• All locks held by our session are released, <strong>and</strong> everyone who was enqueued waiting on<br />

locks we held will be released.<br />

A COMMIT, on the other h<strong>and</strong>, just flushes any remaining data in the redo log buffers. It<br />

does very little work compared to a ROLLBACK. The point here is that you don’t want to roll back<br />

unless you have to. It is expensive since you spend a lot of time doing the work, <strong>and</strong> you’ll also<br />

spend a lot of time undoing the work. Don’t do work unless you’re sure you are going to want<br />

to COMMIT it. This sounds like common sense—of course I wouldn’t do all of the work unless I<br />

wanted to COMMIT it. Many times, however, I’ve seen a situation where a developer will use a<br />

“real” table as a temporary table, fill it up with data, report on it, <strong>and</strong> then roll back to get rid<br />

of the temporary data. In the next section, we’ll talk about true temporary tables <strong>and</strong> how to<br />

avoid this issue.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!