search
HomeDatabaseMysql TutorialOracle利用Logmnr找回误删除数据

Logmnr在Oracle 9i以后做了众多的改进,如可以不需要build flat文本文件就可以分析日志了,也就表示可以不需要修改参数utl_file_

Logmnr在Oracle 9i以后做了众多的改进,如可以不需要build flat文本文件就可以分析日志了,也就表示可以不需要修改参数utl_file_dir就可以分析日志了,避免了修改参数utl_file_dir导致的重起数据库问题。另外也开始支持把字典信息build到联机日志中,在异地分析归档日志。

在flashback也不能帮上忙的时候,logmnr却是非常有用的。因为只要误操作时期的归档日志存在,就可以通过归档日志来恢复误删除(delete)的数据。

如果在Oracle 9i以上,采用在本地的在线数据字典分析归档日志,就这么简单:

Piner@9iR2>exec -
           sys.dbms_logmnr.add_logfile(LogFileName=>'/archive_log/archive/1_9.arc',-
           options => dbms_logmnr.new);
PL/SQL procedure successfully completed.

Piner@9iR2>exec sys.dbms_logmnr.start_logmnr( -
           Options => sys.dbms_logmnr.dict_from_online_catalog);
PL/SQL procedure successfully completed.
可以看到,在线分析其实就只需要这两步,添加日志并分析日志。注意以上OPTIONS => DBMS_LOGMNR.NEW,表示添加第一个日志,如需要另外添加更多的日志,可以用如下方式即可。

Piner@9iR2>exec sys.dbms_logmnr.add_logfile(LogFileName=>'/archive_log/archive/1_10.arc');
如果日志分析完成,可以把需要的信息保存到临时表,如

Piner@9iR2>create table tmp_logmnr as
     2  select operation,sql_redo,sql_undo from v$logmnr_contents
     3  where seg_name='TEST';
然后,终止日志分析过程。

Piner@9iR2>exec sys.dbms_logmnr.end_logmnr
对于临时表中的SQL_UNDO,可以选择性的恢复,如采用如下的脚本来恢复,这里为了减少阻塞,每1000条提交一次。另外,需要注意的是,SQL_UNDO中的分号,如果想用动态SQL来执行的话,是需要去掉的。【LINUX公社  】

declare
  mysql varchar2(4000);
  num number :=0;
begin
  for c_tmp in (select sql_undo from tmp_logmnr where operation = 'DELETE') loop
  --去掉语句中的分号,这里假定只有语句结尾有分号,语句中间并没有分号。
  --如果语句中也有分号,则可以考虑替换语句结尾的;'(分号单引号)为'(单引号)。
    mysql := replace(c_tmp.sql_undo,';','');
    execute immediate mysql;
    num := num + 1;
    if mod(num,1000)=0 then
      commit;
    end if;
  end loop;
  commit;
exception
  when others then
   --异常处理
end;
/
以上的PL/SQL代码其实还可以加强,如恢复成功一条,就设置状态值为1,否则,设置状态值为-1,方便跟踪那些记录恢复成功,哪些记录恢复失败了。

另外,,需要注意的是,在Oracle 10g以下,LOGMNR的临时表v$logmnr_contents,使用的是system表空间,在Oracle 10g以后改为sysaux表空间。可以使用如下的命令,更改logmnr的特定表空间,防止system表空间出现空间不够。

Piner@9iR2>exec  sys.dbms_logmnr_d.set_tablespace('USERS');

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Apr 15, 2025 am 12:11 AM

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL vs. Other Databases: Comparing the OptionsMySQL vs. Other Databases: Comparing the OptionsApr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor