Home Database Mysql Tutorial 使用dbms_flashback工具包实现闪回查询功能

使用dbms_flashback工具包实现闪回查询功能

Jun 07, 2016 pm 03:10 PM

Flashback Query是借助Oracle Undo过期数据而实现的一种方便的逻辑恢复功能。在Undo Tablespace支持的情况下,我们可以查询到过去

Flashback Query是借助Oracle Undo过期数据而实现的一种方便的逻辑恢复功能。在Undo Tablespace支持的情况下,我们可以查询到过去一个特定的时间点(或者SCN点)某个数据表的时间版本。

标准的Flashback Query语句是需要借助as of timestamp| as of scn语句在数据表后面,用于指定查看的数据表过去时间点是什么。这种方式从数据库管理员的角度的确是很方便,特别是那些直接访问后台挽救数据的开发管理人员。

但是在两种情况下,as of指定时间的方式存在一些问题。首先是应用程序中的语句,开发嵌入到应用程序的代码是不能轻易修改的,也就是说我们在procedure或者package的外面,是不能加入那些as of语句指定时间。另一方面,一个时间点数据可能是涉及多个数据表版本操作,逐个表指定是存在很多的问题。于是,使用dbms_flashback包的过去时间点上下文指定功能,就可以解决上面说的问题。

1、环境说明

笔者使用Oracle 11gR2进行测试实验,具体版本为11.2.0.4。

SQL> select * from v$version;

BANNER

--------------------------------------------------------------------------------

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

PL/SQL Release 11.2.0.4.0 - Production

CORE    11.2.0.4.0 Production

TNS for Linux: Version 11.2.0.4.0 - Production

NLSRTL Version 11.2.0.4.0 – Production

当前没有配置补充日志supplemental log data,同时Undo配置关键参数如下:

SQL> select SUPPLEMENTAL_LOG_DATA_MIN from v$database;

SUPPLEMENTAL_LOG_DATA_MIN

-------------------------

NO

SQL> show parameter undo

NAME                                TYPE        VALUE

------------------------------------ ----------- ------------------------------

undo_management                      string      AUTO

undo_retention                      integer    9000

undo_tablespace                      string      UNDOTBS1

dbms_flashback包的描述信息如下:

SQL> desc dbms_flashback

Element                        Type     

------------------------------ --------- 

ENABLE_AT_TIME                PROCEDURE 

ENABLE_AT_SYSTEM_CHANGE_NUMBER PROCEDURE 

DISABLE                        PROCEDURE 

GET_SYSTEM_CHANGE_NUMBER      FUNCTION 

NOCASCADE                      CONSTANT 

NOCASCADE_FORCE                CONSTANT 

NONCONFLICT_ONLY              CONSTANT 

CASCADE                        CONSTANT 

TRANSACTION_BACKOUT            PROCEDURE

在笔者之前的文章中,经常使用dbms_flashback.get_system_change_number来获取系统的SCN编号,并且演示过transaction_backout方法逆转整体事务的策略。本篇集中在enable_at_time、enable_at_system_change_number和disable方法上。

2、dbms_flashback时间机器

enable_at_time和enable_at_system_change_number的作用相同,都是将当前会话的上下文逆转到过去的一个时间点,区别仅在于制定的是时间点还是SCN编号。

正确执行两个方法之后,所有的查询都是基于指定的时间点进行的,类似于电影中的“时间机器”。背后使用的Flashback Query过程根本不需要我们手工指定时间点在数据表后面。

注意:同flashback query使用相同,dbms_flashback方法不允许在SYS用户下使用,如果使用就会报错。

SQL> exec dbms_flashback.enable_at_system_change_number(query_scn => 2107410);

begin dbms_flashback.enable_at_system_change_number(query_scn => 2107410); end;

ORA-08185: ???§ SYS ???§??????

ORA-06512: ?? "SYS.DBMS_FLASHBACK", line 12

ORA-06512: ?? line 1

我们的演示实验会在scott用户下进行。首先需要给scott用户赋予dbms_flashback包的执行权限。

SQL> grant execute on dbms_flashback to scott;

Grant succeeded

切换到scott用户,创建实验数据表。

SQL> create table test as select empno, sal from emp where rownum

Table created

SQL> select * from test;

EMPNO      SAL

----- ---------

 7369    800.00

 7499  1600.00

 7521  1250.00

此时系统时间和SCN编号如下:

SQL> select dbms_flashback.get_system_change_number from dual;

GET_SYSTEM_CHANGE_NUMBER

------------------------

                2107631

SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

TO_CHAR(SYSDATE,'YYYY-MM-DDHH2

------------------------------

2015-06-29 13:49:13

之后进行所谓的“误操作”。

SQL> update test set sal=1000 where empno=7521;

1 row updated

SQL> commit;

Commit complete

SQL> select * from test where empno=7521;

EMPNO      SAL

----- ---------

 7521  1000.00

传统的Flashback Query策略。

SQL> select * from test as of scn 2107631 where empno=7521;

EMPNO      SAL

----- ---------

 7521  1250.00

下面使用dbms_flashback方法,,指定出一个SCN编号。

SQL> exec dbms_flashback.enable_at_system_change_number(query_scn => 2107631); --开启了查询;

PL/SQL procedure successfully completed

SQL> select * from test where empno=7521;

EMPNO      SAL

----- ---------

 7521  1250.00

SQL> exec dbms_flashback.disable;

PL/SQL procedure successfully completed

--disable之后,数据恢复

SQL> select * from test where empno=7521;

EMPNO      SAL

----- ---------

 7521  1000.00

注意:使用enable方法之后,我们以直接的方式查询到过去的时间点方法。如果操作结束,需要使用disable方法关闭设置的上下文时间。

如果指定timestamp方法,效果是相同的。

SQL> exec dbms_flashback.enable_at_time(query_time => to_timestamp('2015-06-29 13:49:13','yyyy-mm-dd hh24:mi:ss'));

PL/SQL procedure successfully completed

SQL> select * from test where empno=7521;

EMPNO      SAL

----- ---------

 7521  1250.00

SQL> exec dbms_flashback.disable;

PL/SQL procedure successfully completed

最后,我们考虑一下,如果在过去的时间上下文中进行修改,修改相关数据和无关数据,结果是如何呢?

SQL> exec dbms_flashback.enable_at_system_change_number(query_scn => 2107631);

PL/SQL procedure successfully completed

SQL> select * from test where empno=7521;

EMPNO      SAL

----- ---------

 7521  1250.00

SQL> select * from test;

EMPNO      SAL

----- ---------

 7369    800.00

 7499  1600.00

 7521  1250.00

SQL> delete test where empno=7499;

delete test where empno=7499

ORA-08182: 在闪回模式下操作不受支持

SQL> update test set sal=1000 where empno=7369;

update test set sal=1000 where empno=7369

ORA-08182: 在闪回模式下操作不受支持

SQL> insert into test values (1000,1000);

insert into test values (1000,1000)

ORA-08182: 在闪回模式下操作不受支持

SQL> create table m as select * from test;

create table m as select * from test

ORA-08182: 在闪回模式下操作不受支持

和时间机器一样,不能改变历史。

3、结论

Dbms_flashback工具包提供了关于闪回技术的很多功能和有意义的场景。借助dbms_flashback的flashback query上下文,我们可以方便的实现上下文历史数据查询检索。

本文永久更新链接地址

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

RDS MySQL integration with Redshift zero ETL RDS MySQL integration with Redshift zero ETL Apr 08, 2025 pm 07:06 PM

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

See all articles