This document is a reading note from the official Oracle documentation Oracle Database Utilities 11g Release 2 (11.2) E22490-05, and it mainly introduces several examples of log mining using logmnr.
Environment Information
Operating System Version
Microsoft Windows Version 6.1.7601
Database Version
Oracle 11g Release 11.2.0.1.0
Preparation Work
Create a test table. All subsequent experiments are based on this test table.
Copy some data from the emp table of the scott user.
1 2 3
SQL>create table scott.tab_emp as select ename,sal from scott.emp; Table created.
View the data in the test table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
SQL>select*from scott.tab_emp; ENAME SAL ---------- ---------- SMITH 800 ALLEN 1600 WARD 1250 JONES 2975 MARTIN 1250 BLAKE 2850 CLARK 2450 SCOTT 3000 KING 5000 TURNER 1500 ADAMS 1100 JAMES 950 FORD 3000 MILLER 1300 14rows selected.
Log Mining Examples
View Changes in the Most Recently Archived Redo Log File
Locate the path of the most recently archived redo log file. Suppose the operation we want to check just occurred, and the log is recorded in the latest archived log file.
1 2 3 4
SQL>SELECT NAME FROM V$ARCHIVED_LOG WHERE FIRST_TIME = (SELECTMAX(FIRST_TIME) FROM V$ARCHIVED_LOG); NAME -------------------------------------------------------------------------------- E:\ORACLE\ARCH_LOG\ARC0000000021_0831311361.0001
View Committed Changes in the Most Recently Archived Redo Log File
In the previous example, the data analyzed included uncommitted SQL statements. In this example, we will only query the committed SQL statements.
View the data in the test table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
SQL>select*from scott.tab_emp; ENAME SAL ---------- ---------- SMITH 800 ALLEN 1600 WARD 1250 JONES 2975 MARTIN 1250 BLAKE 2850 CLARK 2450 SCOTT 3000 KING 5000 TURNER 1500 ADAMS 1100 JAMES 950 FORD 3000 MILLER 1300 14rows selected.
Execute SQL statements.
1 2 3 4 5 6 7 8 9 10
SQL>deletefrom scott.tab_emp where ename in ('TURNER','ADAMS'); 2rows deleted. SQL>rollback; Rollback complete. SQL>update scott.tab_emp set sal=8888where ename='SCOTT'; 1row updated. SQL>commit; Commit complete. SQL>altersystem switch logfile; System altered.
Locate the path of the most recently archived redo log file.
1 2 3 4
SQL>SELECT NAME FROM V$ARCHIVED_LOG WHERE FIRST_TIME = (SELECTMAX(FIRST_TIME) FROM V$ARCHIVED_LOG); NAME ------------------------------------------------------ E:\ORACLE\ARCH_LOG\ARC0000000023_0831311361.0001
SQL>SELECT scn,username,sql_redo from v$logmnr_contents where table_name ='TAB_EMP'; SCN USERNAME SQL_REDO ---------- -------- ---------------------------------------------------------------------------------------------------------------- 1082062UNKNOWNdeletefrom "SCOTT"."TAB_EMP" where "ENAME" ='TURNER'and "SAL" ='1500'and ROWID ='AAASNgAAEAAAAJ7AAJ'; 1082062UNKNOWNdeletefrom "SCOTT"."TAB_EMP" where "ENAME" ='ADAMS'and "SAL" ='1100'and ROWID ='AAASNgAAEAAAAJ7AAK'; 1082088UNKNOWNinsert into "SCOTT"."TAB_EMP"("ENAME","SAL") values ('ADAMS','1100'); 1082088UNKNOWNinsert into "SCOTT"."TAB_EMP"("ENAME","SAL") values ('TURNER','1500'); 1082098UNKNOWNupdate "SCOTT"."TAB_EMP" set "SAL" ='8888'where "SAL" ='3000'and ROWID ='AAASNgAAEAAAAJ7AAH';
As seen, this analysis includes the rollback logs as well. If you only wish to analyze committed transactions, the COMMITTED_DATA_ONLY parameter must be added.
SQL>SELECT scn,username,sql_redo from v$logmnr_contents where table_name ='TAB_EMP'; SCN USERNAME SQL_REDO ---------- -------- ---------------------------------------------------------------------------------------------------- 1082098UNKNOWNupdate "SCOTT"."TAB_EMP" set "SAL" ='8888'where "SAL" ='3000'and ROWID ='AAASNgAAEAAAAJ7AAH';
Here, the uncommitted DELETE statement has been filtered out, and only the committed UPDATE statement is shown.
Format Reconstructed SQL Statements
In the previous examples, the analyzed SQL statements were not formatted, making them difficult to read and analyze, especially when searching through a large number of SQL logs. By specifying the PRINT_PRETTY_SQL parameter, you can format the output SQL statements. However, note that this parameter may prevent the reconstructed statements from being executed directly.