OceanBase 闪回查询

前言

在OB中,drop表可以通过 回收站 或者 以往的备份恢复来还原单表。当delete数据时,由于delete操作的对象不会进入回收站,此时需要通过闪回查询功能查看delete的数据,以便后续恢复

本次实验版本为 OceanBase 4.2.1.8,MySQL租户。参考OB社区 banjin 老师文章进行测试验证

OceanBase 社区

undo_retention

该参数作用为系统应保留的多版本数据范围,单位为秒。属于租户级别变量,默认为1800秒。 当出现delete误删除数据时,可以通过该参数进行闪回查询,恢复误删除数据,可以根据实际业务需求适当更改该参数值

复制代码
obclient [tpcc]> ALTER SYSTEM SET undo_retention=1800;
Query OK, 0 rows affected (0.129 sec)


obclient [tpcc]> SHOW PARAMETERS LIKE 'undo_retention';
+-------+----------+-----------+----------+----------------+-----------+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+--------+---------+-------------------+
| zone  | svr_type | svr_ip    | svr_port | name           | data_type | value | info                                                                                                                                                                           | section | scope  | source  | edit_level        |
+-------+----------+-----------+----------+----------------+-----------+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+--------+---------+-------------------+
| zone3 | observer | 10.0.0.63 |     2882 | undo_retention | NULL      | 1800  | the low threshold value of undo retention. The system retains undo for at least the time specified in this config when active txn protection is banned. Range: [0, 4294967295] | TENANT  | TENANT | DEFAULT | DYNAMIC_EFFECTIVE |
| zone2 | observer | 10.0.0.62 |     2882 | undo_retention | NULL      | 1800  | the low threshold value of undo retention. The system retains undo for at least the time specified in this config when active txn protection is banned. Range: [0, 4294967295] | TENANT  | TENANT | DEFAULT | DYNAMIC_EFFECTIVE |
+-------+----------+-----------+----------+----------------+-----------+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+--------+---------+-------------------+
2 rows in set (0.009 sec)

官方解释如下

DML闪回查询

准备测试数据
复制代码
obclient [tpcc]> create table banjin_flash (id int ,name varchar(10),dizhi varchar(10),primary key (id));
Query OK, 0 rows affected (0.568 sec)


obclient [tpcc]> insert into banjin_flash values (1,'zhangsan','北京');
Query OK, 1 row affected (0.135 sec)


obclient [tpcc]> insert into banjin_flash values (2,'lisi','上海');
Query OK, 1 row affected (0.002 sec)


obclient [tpcc]> insert into banjin_flash values (3,'wangwu','天津');
Query OK, 1 row affected (0.002 sec)


obclient [tpcc]> insert into banjin_flash values (4,'zhaoliu','河北');
Query OK, 1 row affected (0.002 sec)


obclient [tpcc]>
更改数据

记录更改数据时的时间,便于后续闪回恢复

复制代码
obclient [tpcc]> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-11-16 14:25:10 |
+---------------------+
1 row in set (0.002 sec)


obclient [tpcc]> update banjin_flash set dizhi = '湖南' where name='lisi';
Query OK, 1 row affected (0.010 sec)
Rows matched: 1  Changed: 1  Warnings: 0


obclient [tpcc]> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-11-16 14:25:30 |
+---------------------+
1 row in set (0.001 sec)


obclient [tpcc]> delete from banjin_flash;
Query OK, 4 rows affected (0.004 sec)


obclient [tpcc]> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-11-16 14:25:50 |
+---------------------+
1 row in set (0.001 sec)


obclient [tpcc]>
闪回数据

1.可以看到由于之前删除了表中的数据,在当前状态查看不到表中任何数据

2.在 2024-11-16 14:25:30 时间段 dizhi 为上海的数据已经被修改为 湖南

3.在 2024-11-16 14:25:10 时间段 表中的数据为初始时候的状态

复制代码
obclient [tpcc]> select * from banjin_flash;
Empty set (0.002 sec)


obclient [tpcc]>
obclient [tpcc]>
----  在 2024-11-16 14:25:30 时间段 dizhi 为上海的数据已经被修改为 湖南
obclient [tpcc]> SELECT * FROM banjin_flash AS OF SNAPSHOT time_to_usec('2024-11-16 14:25:30') * 1000;
+------+----------+--------+
| id   | name     | dizhi  |
+------+----------+--------+
|    1 | zhangsan | 北京   |
|    2 | lisi     | 湖南   |
|    3 | wangwu   | 天津   |
|    4 | zhaoliu  | 河北   |
+------+----------+--------+
4 rows in set (0.002 sec)
---  在 2024-11-16 14:25:10 时间段 表中的数据为初始时候的状态
obclient [tpcc]>
obclient [tpcc]> SELECT * FROM banjin_flash AS OF SNAPSHOT time_to_usec('2024-11-16 14:25:10') * 1000;
+------+----------+--------+
| id   | name     | dizhi  |
+------+----------+--------+
|    1 | zhangsan | 北京   |
|    2 | lisi     | 上海   |
|    3 | wangwu   | 天津   |
|    4 | zhaoliu  | 河北   |
+------+----------+--------+
4 rows in set (0.001 sec)


obclient [tpcc]>
obclient [tpcc]>

过程中有DDL的闪回

表增字段后的闪回效果

可以看到如果后续对表进行了alter操作,闪回查询时 新加字段被Default 值补齐,如果没有default值会用null补齐

复制代码
obclient [tpcc]> alter table banjin_flash add column dianhua decimal(11) default 1;
Query OK, 0 rows affected (0.178 sec)


obclient [tpcc]>
obclient [tpcc]> SELECT * FROM banjin_flash AS OF SNAPSHOT time_to_usec('2024-11-16 14:25:30') * 1000;
+------+----------+--------+---------+
| id   | name     | dizhi  | dianhua |
+------+----------+--------+---------+
|    1 | zhangsan | 北京   |       1 |
|    2 | lisi     | 湖南   |       1 |
|    3 | wangwu   | 天津   |       1 |
|    4 | zhaoliu  | 河北   |       1 |
+------+----------+--------+---------+
4 rows in set (0.015 sec)


obclient [tpcc]> SELECT * FROM banjin_flash AS OF SNAPSHOT time_to_usec('2024-11-16 14:25:10') * 1000;
+------+----------+--------+---------+
| id   | name     | dizhi  | dianhua |
+------+----------+--------+---------+
|    1 | zhangsan | 北京   |       1 |
|    2 | lisi     | 上海   |       1 |
|    3 | wangwu   | 天津   |       1 |
|    4 | zhaoliu  | 河北   |       1 |
+------+----------+--------+---------+
4 rows in set (0.001 sec)


obclient [tpcc]> alter table banjin_flash add column dianhua1 decimal(11) ;
Query OK, 0 rows affected (0.083 sec)


obclient [tpcc]> SELECT * FROM banjin_flash AS OF SNAPSHOT time_to_usec('2024-11-16 14:25:30') * 1000;
+------+----------+--------+---------+----------+
| id   | name     | dizhi  | dianhua | dianhua1 |
+------+----------+--------+---------+----------+
|    1 | zhangsan | 北京   |       1 |     NULL |
|    2 | lisi     | 湖南   |       1 |     NULL |
|    3 | wangwu   | 天津   |       1 |     NULL |
|    4 | zhaoliu  | 河北   |       1 |     NULL |
+------+----------+--------+---------+----------+
4 rows in set (0.015 sec)


obclient [tpcc]> SELECT * FROM banjin_flash AS OF SNAPSHOT time_to_usec('2024-11-16 14:25:10') * 1000;
+------+----------+--------+---------+----------+
| id   | name     | dizhi  | dianhua | dianhua1 |
+------+----------+--------+---------+----------+
|    1 | zhangsan | 北京   |       1 |     NULL |
|    2 | lisi     | 上海   |       1 |     NULL |
|    3 | wangwu   | 天津   |       1 |     NULL |
|    4 | zhaoliu  | 河北   |       1 |     NULL |
+------+----------+--------+---------+----------+
4 rows in set (0.001 sec)


obclient [tpcc]>
表删除字段后的闪回效果

可以看到如果对表进行了 alter 删除字段操作后,无法使用闪回还原数据

复制代码
obclient [tpcc]> alter table banjin_flash drop column dianhua1;
Query OK, 0 rows affected (0.835 sec)


obclient [tpcc]> SELECT * FROM banjin_flash AS OF SNAPSHOT time_to_usec('2024-11-16 14:25:10') * 1000;
ERROR 1412 (HY000): Unable to read data -- Table definition has changed
obclient [tpcc]>
obclient [tpcc]> SELECT * FROM banjin_flash AS OF SNAPSHOT time_to_usec('2024-11-16 14:25:30') * 1000;
ERROR 1412 (HY000): Unable to read data -- Table definition has changed
obclient [tpcc]>
obclient [tpcc]>
truncate 闪回效果

重新初始化数据,发现truncate操作后也无法进行闪回数据

复制代码
obclient [tpcc]> create table banjin_flash (id int ,name varchar(10),dizhi varchar(10),primary key (id));
Query OK, 0 rows affected (0.106 sec)


obclient [tpcc]> insert into banjin_flash values (1,'zhangsan','北京');
Query OK, 1 row affected (0.025 sec)


obclient [tpcc]> insert into banjin_flash values (2,'lisi','上海');
Query OK, 1 row affected (0.003 sec)


obclient [tpcc]> insert into banjin_flash values (3,'wangwu','天津');
Query OK, 1 row affected (0.053 sec)


obclient [tpcc]> insert into banjin_flash values (4,'zhaoliu','河北');
Query OK, 1 row affected (0.002 sec)


obclient [tpcc]> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-11-16 14:44:19 |
+---------------------+
1 row in set (0.001 sec)


obclient [tpcc]> update banjin_flash set dizhi = '湖南' where name='lisi';
Query OK, 1 row affected (0.014 sec)
Rows matched: 1  Changed: 1  Warnings: 0


obclient [tpcc]> select now();
+---------------------+
| now()               |
+---------------------+
| 2024-11-16 14:44:37 |
+---------------------+
1 row in set (0.001 sec)


obclient [tpcc]> truncate table banjin_flash;
Query OK, 0 rows affected (0.099 sec)


obclient [tpcc]> SELECT * FROM banjin_flash AS OF SNAPSHOT time_to_usec('2024-11-16 14:44:19') * 1000;
ERROR 1412 (HY000): Unable to read data -- Table definition has changed
obclient [tpcc]>
obclient [tpcc]> SELECT * FROM banjin_flash AS OF SNAPSHOT time_to_usec('2024-11-16 14:44:37') * 1000;
ERROR 1412 (HY000): Unable to read data -- Table definition has changed
obclient [tpcc]>

总结

1.当出现delete操作时,可以根据 OB的undo_retention功能进行闪回查询查看误删除的数据

2.后续过程中如果出现 删除字段 以及 truncate表操作 无法进行闪回数据

3.truncate 操作的对象既不会进入 回收站 也无法通过闪回查询,生产环境一定要谨慎操作

相关推荐
指尖上跳动的旋律1 小时前
shell脚本定义特殊字符导致执行mysql文件错误的问题
数据库·mysql
一勺菠萝丶2 小时前
MongoDB 常用操作指南(Docker 环境下)
数据库·mongodb·docker
m0_748244832 小时前
StarRocks 排查单副本表
大数据·数据库·python
C++忠实粉丝3 小时前
Redis 介绍和安装
数据库·redis·缓存
wmd131643067123 小时前
将微信配置信息存到数据库并进行调用
数据库·微信
是阿建吖!3 小时前
【Linux】基础IO(磁盘文件)
linux·服务器·数据库
凡人的AI工具箱3 小时前
每天40分玩转Django:Django国际化
数据库·人工智能·后端·python·django·sqlite
ClouGence3 小时前
Redis 到 Redis 数据迁移同步
数据库·redis·缓存
m0_748236583 小时前
《Web 应用项目开发:从构思到上线的全过程》
服务器·前端·数据库
苏三说技术3 小时前
Redis 性能优化的18招
数据库·redis·性能优化