flashback之——挖掘SCN(DDL和DML操作示例)

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

1、查询当前日志组21:43:00 [email protected]>select * from v$log;

         1    1    36   52428800   512     1 NO  CURRENT     1349824

2、查询日志文件 21:42:44 [email protected]>select * from v$logfile;

GROUP# STATUS  TYPE    MEMBER                                             IS_

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

         3         ONLINE  +DATA/testdb11/redo03.log                          NO

         2         ONLINE  +DATA/testdb11/redo02.log                          NO

         1         ONLINE  +DATA/testdb11/redo01.log                          NO

3、查询归档日志文件21:42:28 [email protected]>select name from v$archived_log;

/home/oracle/archivelog_bak/TestDB111_31_846843855.dbf

/home/oracle/archivelog_bak/TestDB111_32_846843855.dbf

/home/oracle/archivelog_bak/TestDB111_33_846843855.dbf

/home/oracle/archivelog_bak/TestDB111_34_846843855.dbf

/home/oracle/archivelog_bak/TestDB111_35_846843855.dbf

/home/oracle/archivelog_bak/TestDB111_36_846843855.dbf

DML操作 挖scn和时间点

依次在sqlplus中执行 NEW当前的日志组多个加逗号隔开,ADDFILE最后一次归档文件

EXECUTE DBMS_LOGMNR.ADD_LOGFILE( -      

   LOGFILENAME => ‘+DATA/testdb11/redo01.log‘, -             

   OPTIONS => DBMS_LOGMNR.NEW);

EXECUTE DBMS_LOGMNR.ADD_LOGFILE( -

   LOGFILENAME => ‘/home/oracle/archivelog_bak/TestDB111_36_846843855.dbf‘, -

   OPTIONS => DBMS_LOGMNR.ADDFILE);

   

EXECUTE DBMS_LOGMNR.START_LOGMNR(OPTIONS => -

   DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG);

EXECUTE DBMS_LOGMNR.END_LOGMNR;

alter session set nls_date_format=‘yyyy-mm-dd hh24:mi:ss‘;

col username for a10

col sql_redo for a50

select username,scn,timestamp,sql_redo from v$logmnr_contents where seg_name=‘t1‘ order by scn;

1363373 2014-05-20 20:15:41   

俩种闪回

flashback table scott.t1 to scn 1363373;

flashback table scott.t1 to timestmp to_timestmp(‘2014-05-20 20:15:41‘,‘yyyy-mm-dd hh24:mi:ss‘);

例:DML 操作闪回表

SQL> create table t1 as select * from scott.dept;

Table created.

SQL> select * from t1;

    DEPTNO DNAME          LOC

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

        10 ACCOUNTING     NEW YORK

        20 RESEARCH       DALLAS

        30 SALES          CHICAGO

        40 OPERATIONS     BOSTON

SQL> delete t1;

4 rows deleted.

SQL> insert into t1 select * from scott.dept where deptno=10;

1 row created.

SQL> select * from t1;

    DEPTNO DNAME          LOC

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

        10 ACCOUNTING     NEW YORK

SQL> commit;

Commit complete.

SQL> select * from v$log;

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIME

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

         1          1         86   52428800          2 YES INACTIVE                862829 2014-07-22 16:00:01

         2          1         87   52428800          2 YES INACTIVE                862850 2014-07-22 16:00:03

         3          1         88   52428800          2 NO  CURRENT                 862976 2014-07-22 16:02:18

首先

开启database补充日志

alter database add supplemental log data;

当前日志组

EXECUTE DBMS_LOGMNR.ADD_LOGFILE( -      

   LOGFILENAME => ‘/prod_log/prod/redo13.log‘, -             

   OPTIONS => DBMS_LOGMNR.NEW);

最后一次归档可写多个,倒序写

EXECUTE DBMS_LOGMNR.ADD_LOGFILE( -

   LOGFILENAME => ‘/arch/1_87_853529715.dbf‘, -

   OPTIONS => DBMS_LOGMNR.ADDFILE);

   

EXECUTE DBMS_LOGMNR.ADD_LOGFILE( -

   LOGFILENAME => ‘/arch/1_86_853529715.dbf‘, -

   OPTIONS => DBMS_LOGMNR.ADDFILE);

 

EXECUTE DBMS_LOGMNR.ADD_LOGFILE( -

   LOGFILENAME => ‘/arch/1_85_853529715.dbf‘, -

   OPTIONS => DBMS_LOGMNR.ADDFILE);

      

EXECUTE DBMS_LOGMNR.START_LOGMNR(OPTIONS => -

   DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG);

EXECUTE DBMS_LOGMNR.END_LOGMNR;

alter session set nls_date_format=‘yyyy-mm-dd hh24:mi:ss‘;

col username for a10

col sql_redo for a50

select username,scn,timestamp,sql_redo from v$logmnr_contents where seg_name=‘T1‘ order by scn;

USERNAME          SCN TIMESTAMP           SQL_REDO

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

SYS            863211 2014-07-22 16:10:20 insert into "SYS"."T1"("DEPTNO","DNAME","LOC") val

                                          ues (‘40‘,‘OPERATIONS‘,‘BOSTON‘);

SYS            863228 2014-07-22 16:10:51 delete from "SYS"."T1" where "DEPTNO" = ‘10‘ and "

                                          DNAME" = ‘ACCOUNTING‘ and "LOC" = ‘NEW YORK‘ and R

                                          OWID = ‘AAAM4GAABAAAO2iAAA‘;

SYS            863228 2014-07-22 16:10:51 delete from "SYS"."T1" where "DEPTNO" = ‘20‘ and "

                                          DNAME" = ‘RESEARCH‘ and "LOC" = ‘DALLAS‘ and ROWID

                                           = ‘AAAM4GAABAAAO2iAAB‘;

开启行迁移

alter table t1 enable row movement;

基于scn

flashback table t1 to scn 863227;

基于时间点

flashback table t1 to timestamp to_timestamp(‘2014-07-22 16:10:50‘,‘yyyy-mm-dd hh24:mi:ss‘);

闪回查询

select * from t1 as of timestamp to_timestamp(‘2014-07-22 16:10:50‘,‘yyyy-mm-dd hh24:mi:ss‘);

sys用户不能使用flashback,用闪回查询创建

create table t2 as select * from t1 as of timestamp to_timestamp(‘2014-07-22 16:10:50‘,‘yyyy-mm-dd hh24:mi:ss‘);

注:可能出现的报错信息

SQL> select * from t1 as of timestamp to_timestamp(‘2014-07-22 16:10:20‘,‘yyyy-mm-dd hh24:mi:ss‘);

select * from t1 as of timestamp to_timestamp(‘2014-07-22 16:10:20‘,‘yyyy-mm-dd hh24:mi:ss‘)

              *

ERROR at line 1:

ORA-01466: unable to read data - table definition has changed

时间点找的不对,应该找delete删除之前的几秒钟

SQL> flashback table t1 to timestamp to_timestamp(‘2014-07-22 16:10:30‘,‘yyyy-mm-dd hh24:mi:ss‘);

flashback table t1 to timestamp to_timestamp(‘2014-07-22 16:10:30‘,‘yyyy-mm-dd hh24:mi:ss‘)

                *

ERROR at line 1:

ORA-08185: Flashback not supported for user SYS

DDL操作 闪回数据库  ***注:最好在备库上做闪回数据库操作,再逻辑导入到主库中

SQL> create table t2 as select * from dept;

Table created.

SQL> select * from t2;

    DEPTNO DNAME          LOC

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

        10 ACCOUNTING     NEW YORK

        20 RESEARCH       DALLAS

        30 SALES          CHICAGO

        40 OPERATIONS     BOSTON

SQL> drop table t2 purge;

Table dropped.

设置参数,存放数据字典

mkdir /home/oracle/logmnr

SQL> show parameter utl

NAME                                 TYPE        VALUE

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

create_stored_outlines               string

utl_file_dir                         string

SQL> alter system set utl_file_dir=‘/home/oracle/logmnr‘ scope=spfile;

System altered.

SQL> startup force;

ORACLE instance started.

Total System Global Area  570425344 bytes

Fixed Size                  2022480 bytes

Variable Size             209716144 bytes

Database Buffers          352321536 bytes

Redo Buffers                6365184 bytes

Database mounted.

Database opened.

建立数据字典文件dict.ora

execute dbms_logmnr_d.build(‘dict.ora‘,‘/home/oracle/logmnr‘,dbms_logmnr_d.store_in_flat_file);

添加日志分析

EXECUTE DBMS_LOGMNR.ADD_LOGFILE( -      

   LOGFILENAME => ‘/arch/1_110_853529715.dbf‘, -             

   OPTIONS => DBMS_LOGMNR.NEW);

EXECUTE DBMS_LOGMNR.ADD_LOGFILE( -

   LOGFILENAME => ‘/arch/1_109_853529715.dbf‘, -

   OPTIONS => DBMS_LOGMNR.ADDFILE);

   

EXECUTE DBMS_LOGMNR.ADD_LOGFILE( -

   LOGFILENAME => ‘/arch/1_108_853529715.dbf‘, -

   OPTIONS => DBMS_LOGMNR.ADDFILE);

 

EXECUTE DBMS_LOGMNR.ADD_LOGFILE( -

   LOGFILENAME => ‘/arch/1_107_853529715.dbf‘, -

   OPTIONS => DBMS_LOGMNR.ADDFILE);

execute dbms_logmnr.end_logmnr;

执行分析

execute dbms_logmnr.start_logmnr(dictfilename=>‘/home/oracle/logmnr/dict.ora‘,options=>dbms_logmnr.ddl_dict_tracking);

查看分析结果

alter session set nls_date_format=‘yyyy-mm-dd hh24:mi:ss‘;

col username for a10

col sql_redo for a50

select username,scn,timestamp,sql_redo from v$logmnr_contents where username=‘SCOTT‘ and lower(sql_redo) like ‘%table%‘;

SQL> select username,scn,timestamp,sql_redo from v$logmnr_contents where username=‘SCOTT‘ and lower(sql_redo) like ‘%table%‘;

USERNAME          SCN TIMESTAMP           SQL_REDO

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

SCOTT          898096 2014-07-22 17:54:04 drop table t1 purge;

SCOTT          898346 2014-07-22 17:55:27 create table t2 as select * from dept;

SCOTT          899047 2014-07-22 17:56:24 drop table t2 purge;

flashback database to scn 898096;

flashback database to timestamp to_timestamp(‘2014-07-22 17:55:45‘,‘yyyy-mm-dd hh24:mi:ss‘);

关库到mount 闪回

SQL> shutdown immediate;

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL> startup mount;

ORACLE instance started.

Total System Global Area  570425344 bytes

Fixed Size                  2022480 bytes

Variable Size             218104752 bytes

Database Buffers          343932928 bytes

Redo Buffers                6365184 bytes

Database mounted.

SQL> flashback database to timestamp to_timestamp(‘2014-07-22 17:55:45‘,‘yyyy-mm-dd hh24:mi:ss‘);

Flashback complete.

只读

SQL> alter database open read only;

Database altered.

SQL> select * from scott.t2;

    DEPTNO DNAME          LOC

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

        10 ACCOUNTING     NEW YORK

        20 RESEARCH       DALLAS

        30 SALES          CHICAGO

        40 OPERATIONS     BOSTON

SQL> shutdown immediate;

SQL> startup mount;

ORACLE instance started.

Total System Global Area  570425344 bytes

Fixed Size                  2022480 bytes

Variable Size             218104752 bytes

Database Buffers          343932928 bytes

Redo Buffers                6365184 bytes

Database mounted.

SQL> alter database open resetlogs;

Database altered.

SQL> select * from scott.t2;

    DEPTNO DNAME          LOC

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

        10 ACCOUNTING     NEW YORK

        20 RESEARCH       DALLAS

        30 SALES          CHICAGO

        40 OPERATIONS     BOSTON

flashback之——挖掘SCN(DDL和DML操作示例)

时间: 2024-10-15 09:12:17

flashback之——挖掘SCN(DDL和DML操作示例)的相关文章

第一讲SQL命令的DDL和DML操作讲解

知识点: 一.sql命令DDL(结构化操作) 二.sql命令DML操作(增删改查) 1.sql命令DDL(结构化操作) 1.1表添加字段: alter table 表名 add 列定义 如: alter table Student add email varchar(128); 1.2 修改字段: alter table 表名 change 旧字段名 新字段名: alter table Student change email StuEmail varchar(256); 1.3删除字段: al

Oracle ddl 和 dml 操作

  ddl 操作 窗口设置用户权限的方法  Oracle的数据类型  按住Ctrl点击表名 ,可以鼠标操作  插入的数据需要满足创建表的检查  主表clazz删除数据从表设置级联也会一同删除 有约束也强制删除 Oracle dml操作  简化分页查询 创建百万条记录的表 Oracle 数据库的备份 导入导出 原文地址:https://www.cnblogs.com/qingyundian/p/9142659.html

Hive的DDL和DML操作

1.数据定义语言: hive> CREATE TABLE pokes (foo INT, bar STRING); hive> CREATE TABLE invites (foo INT, bar STRING) PARTITIONED BY (ds STRING); --分区是虚拟的,用于加载特别的数据集到表中. 2.CREATE /DROP/ALTER  DATABASE 创建语法: CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_nam

oracle全攻略——查询优化,DDL和DML操作

Oracle的闪回技术--闪回错误的DML操作

提交DML操作后,该操作使用的还原段就可以被其它对象使用了,为了保证闪回操作时这些数据仍然被保存在还原段中,可能需要重新设置undo_retention参数,表示一个事务提交后,该事务的数据必须保存在还原段中的时间:但是这也并不能完全保证指定的时间的数据一定能够被恢复,还原表空间没有足够的时间时,仍会覆盖要求保留的磁盘空间. 查看undo_retention的当前值: SQL> show parameter undo_retention NAME TYPE VALUE -------------

JAVA-Unit01: 数据库原理 、 SQL(DDL、DML)

Unit01: 数据库原理 . SQL(DDL.DML) SQL语句是不区分大小写的,但是行业里习惯将关键字与分关键字用大小写岔开以提高可读性. SELECT SYSDATE FROM dual DDL语句 数据定义语言 用于操作数据库对象 数据库对象有:表,视图,索引,序列 创建表: CREATE TABLE employee( id NUMBER(4), name VARCHAR2(20), gender CHAR(1), birth DATE, salary NUMBER(6,2), jo

DDL、DML和DCL的区别与理解

DML.DDL.DCL区别 . 总体解释: DML(data manipulation language): 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言 DDL(data definition language): DDL比DML要多,主要的命令有CREATE.ALTER.DROP等,DDL主要是用在定义或改变表(TABLE)的结构,数据类型,表之间的链接和约束等初始化工作上,他们大多在建立表时使用 DCL(Da

MySQL/MariaDB DML操作之Select

前言 上文我们已经讲解了MySQL/MariaDB的基础知识和DDL相关操作,接下来我们来说一下MySQL/MariaDB的DML操作,因select查询较复杂也较重要,所以本文主要是对select查询的详解. DML操作 DML之select 投影查询 select col_name,[col_name1,...] from table_name; select * from table_name ;#显示全表 遍历整张数据表,但对系统资源消耗较大,再进行大数据量的查询时,禁止使用这类操作 条

DDL 和DML 区别

DML(Data Manipulation Language)数据操纵语言: 适用范围:对数据库中的数据进行一些简单操作,如insert,delete,update,select等. DDL(Data Definition Language)数据定义语言: 适用范围:对数据库中的某些对象(例如,database,table)进行管理,如Create,Alter和Drop. 区别: 1.DML操作是可以手动控制事务的开启.提交和回滚的. 2.DDL操作是隐性提交的,不能rollback!