一、as of timestamp
1、创建
create table admin.zcjcs as select * from view_yy_xjsjsc;
drop table admin.zcjcs;
2、准备
select * from admin.zcjcs; --191 行
select * from admin.zcjcs where 分局ID > 100; --98 行
3、删除、闪回
delete from admin.zcjcs where 分局ID > 100;
select * from admin.zcjcs; --93 行
select * from admin.zcjcs as of timestamp sysdate-5/1440; --191 行
insert into admin.zcjcs
select *
from admin.zcjcs as of timestamp to_timestamp('2023-03-06 16:40:00', 'YYYY-MM-DD hh24:mi:ss')
where 分局ID > 100;
select * from admin.zcjcs; --191 行
4、备注
如上述示例中所表示的,as of timestamp 的确非常易用,
但是在某些情况下,我们建议使用 as of scn 的方式执行 flashback query,
比如需要对多个相互有主外键约束的表进行恢复时,如果使用 as of timestamp 的方式,
可能会由于时间点不统一的缘故造成数据选择或插入失败,
通过 scn 方式则能够确保记录的约束一致性。
==============================================================
二、As of scn
1、创建、查当前数据库scn号
create table admin.zcjcs as select * from admin.view_yy_xjsjsc;
select * from admin.zcjcs;
SELECT dbms_flashback.get_system_change_number FROM dual;
SELECT CURRENT_SCN FROM V$DATABASE;
2、删除、查删除前数据
select * from admin.zcjcs where 分局ID > 100;
select * from admin.zcjcs as of scn 26754747920858 where 分局ID > 100;
select * from admin.zcjcs as of scn 26754747920859 where 分局ID > 100;
delete from admin.zcjcs where 分局ID > 100;
3、插入
insert into admin.zcjcs
select *
from admin.zcjcs as of scn 26754747920858
where 分局ID > 100;
4、备注
Oracle 在内部都是使用 scn,即使你指定的是 as of timestamp,也会将其转换成 scn,
系统时间标记与 scn 之间存在一张表,
即 SYS.SMON_SCN_TIME
每隔 5 分钟,系统产生一次系统时间标记与 scn 的匹配并存入表,
该表中记录了最近 1440 个系统时间标记与 scn 的匹配记录,
由于该表只维护了最近的1440 条记录,
因此如果使用 as of timestamp 的方式则只能 flashback 最近 5 天内的数据
(假设系统是在持续不断运行并无中断或关机重启之类操作的话)。
5、查看 SCN 和 timestamp 之间的对应关系
select scn,to_char(time_dp,'yyyy-mm-dd hh24:mi:ss')from sys.smon_scn_time;