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 295 • We’ve obviously increased the round-trips to and from the database. If we commit every record, we are generating that much more traffic back and forth. • Every time we commit, we must wait for our redo to be written to disk. This will result in a “wait.” In this case, the wait is named “log file sync.” We can actually observe the latter easily by slightly modifying the Java application. We’ll do two things: • Add a call to DBMS_MONITOR to enable SQL tracing with wait events. In Oracle9i, we would use alter session set events '10046 trace name context forever, level 12' instead, as DBMS_MONITOR is new in Oracle 10g. • Change the con.commit() call to be a call to a SQL statement to perform the commit. If you use the built-in JDBC commit() call, this does not emit a SQL COMMIT statement to the trace file, and TKPROF, the tool used to format a trace file, will not report the time spent doing the COMMIT. So, we modify the doInserts() method as follows: doInserts( con, 1, 1 ); Statement stmt = con.createStatement (); stmt.execute ( "begin dbms_monitor.session_trace_enable(waits=>TRUE); end;" ); doInserts( con, iters.intValue(), iters.intValue() ); To the main method, we add the following: PreparedStatement commit = con.prepareStatement ("begin /* commit size = " + commitCount + " */ commit; end;" ); int rowcnt = 0; int committed = 0; ... if ( rowcnt == commitCount ) { commit.executeUpdate(); rowcnt = 0; committed++; Upon running that application with 10,000 rows to insert, committing every row, the TKPROF report would show results similar to the following: begin /* commit size = 1 */ commit; end; .... Elapsed times include waiting on following events: Event waited on Times Max. Wait Total Waited ---------------------------------------- Waited ---------- ------------ SQL*Net message to client 10000 0.00 0.01 SQL*Net message from client 10000 0.00 0.04 log file sync 8288 0.06 2.00

296 CHAPTER 9 ■ REDO AND UNDO If we insert 10,000 rows and only commit when all 10,000 are inserted, we get results similar to the following: begin /* commit size = 10000 */ commit; end; .... Elapsed times include waiting on following events: Event waited on Times Max. Wait Total Waited ---------------------------------------- Waited ---------- ------------ log file sync 1 0.00 0.00 SQL*Net message to client 1 0.00 0.00 SQL*Net message from client 1 0.00 0.00 When we committed after every INSERT, we waited almost every time—and if you wait a little bit of time but you wait often, then it all adds up. Fully two seconds of our runtime was spent waiting for a COMMIT to complete—in other words, waiting for LGWR to write the redo to disk. In stark contrast, when we committed once, we didn’t wait very long (not a measurable amount of time actually). This proves that a COMMIT is a fast operation; we expect the response time to be more or less flat, not a function of the amount of work we’ve done. So, why is a COMMIT’s response time fairly flat, regardless of the transaction size? Before we even go to COMMIT in the database, we’ve already done the really hard work. We’ve already modified the data in the database, so we’ve already done 99.9 percent of the work. For example, operations such as the following have already taken place: • Undo blocks have been generated in the SGA. • Modified data blocks have been generated in the SGA. • Buffered redo 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 COMMIT, all that is left to happen is the following: • An SCN is generated for our transaction. In case you are not familiar with it, the SCN is a simple timing mechanism Oracle uses to guarantee the ordering of transactions and to enable recovery from failure. It is also used to guarantee read-consistency and checkpointing in the database. Think of the SCN as a ticker; every time someone COMMITs, the SCN is incremented by one. • LGWR writes all of our remaining buffered redo log entries to disk and records the SCN in the online redo log files as well. This step is actually the COMMIT. If this step occurs, we have committed. Our transaction entry is “removed” from V$TRANSACTION—this shows that we have committed. • All locks recorded in V$LOCK held by our session are released, and everyone who was enqueued waiting on locks we held will be woken up and allowed to proceed with their work.

296<br />

CHAPTER 9 ■ REDO AND UNDO<br />

If we insert 10,000 rows <strong>and</strong> only commit when all 10,000 are inserted, we get results<br />

similar to the following:<br />

begin /* commit size = 10000 */ commit; end;<br />

....<br />

Elapsed times include waiting on following events:<br />

Event waited on Times Max. Wait Total Waited<br />

---------------------------------------- Waited ---------- ------------<br />

log file sync 1 0.00 0.00<br />

SQL*Net message to client 1 0.00 0.00<br />

SQL*Net message from client 1 0.00 0.00<br />

When we committed after every INSERT, we waited almost every time—<strong>and</strong> if you wait a<br />

little bit of time but you wait often, then it all adds up. Fully two seconds of our runtime was<br />

spent waiting for a COMMIT to complete—in other words, waiting for LGWR to write the redo to<br />

disk. In stark contrast, when we committed once, we didn’t wait very long (not a measurable<br />

amount of time actually). This proves that a COMMIT is a fast operation; we expect the response<br />

time to be more or less flat, not a function of the amount of work we’ve done.<br />

So, why is a COMMIT’s response time fairly flat, regardless of the transaction size? Before we<br />

even go to COMMIT in the database, we’ve already done the really hard work. We’ve already modified<br />

the data in the database, so we’ve already done 99.9 percent of the work. For example,<br />

operations such as the following have already taken place:<br />

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

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

• Buffered redo 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 COMMIT, all that is left to happen is the following:<br />

• An SCN is generated for our transaction. In case you are not familiar with it, the SCN is<br />

a simple timing mechanism <strong>Oracle</strong> uses to guarantee the ordering of transactions <strong>and</strong><br />

to enable recovery from failure. It is also used to guarantee read-consistency <strong>and</strong> checkpointing<br />

in the database. Think of the SCN as a ticker; every time someone COMMITs, the<br />

SCN is incremented by one.<br />

• LGWR writes all of our remaining buffered redo log entries to disk <strong>and</strong> records the SCN in<br />

the online redo log files as well. This step is actually the COMMIT. If this step occurs, we<br />

have committed. Our transaction entry is “removed” from V$TRANSACTION—this shows<br />

that we have committed.<br />

• All locks recorded in V$LOCK held by our session are released, <strong>and</strong> everyone who was<br />

enqueued waiting on locks we held will be woken up <strong>and</strong> allowed to proceed with their<br />

work.

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

Saved successfully!

Ooh no, something went wrong!