时间旅行 'drop & recreate' table 保留期内过去的任何一天

Time travelling a 'drop & recreate' table for any past day within retention period

如果保留期为 30 天,但 table 每天被丢弃并重新创建,是否有可能看到 table 回溯 10 天的样子?

如果 table 被截断而不是重新创建,是否可以回到第 30 天?

Undrop 可能会恢复 table 被删除前的最新版本。是否可以恢复保留期内的任何版本?

是的,您可以查询 table 在其时间旅行期间的任何时间点。

Undrop 会将 table 恢复到被删除时的状态。

要在以前的时间点恢复 table,您需要使用 CREATE TABLE... CLONE...

  1. 如果保留期为 30 天,但 table 每天被删除并重新创建,是否有可能看到 table 回溯 10 天的样子?

UNDROP 命令仅恢复 table 的最新版本。但是@Pankaj 的测试表明,您仍然可以恢复任何尚未清除的掉落 table。

要查看历史上table的所有版本(已删除),您可以使用以下命令

SHOW TABLES HISTORY LIKE '<table_name>';

然后执行一系列 UNDROP 和 RENAME,直到恢复要恢复的 table 版本。

然而,这不是一个好的做法。如果您每天重新创建 table,请缩短其保留期限。如果要恢复 1 个月的历史记录,请执行截断而不是删除。

  1. 如果 table 被截断而不是重新创建,是否可以回到第 30 天?

是的。

create table tbl_clone clone tbl at (timestamp => dateadd('day',-30,current_timestamp()));
  1. Undrop 可能恢复 table 被删除前的最新版本。是否可以恢复保留期内的任何版本?

是的,正如@Pankaj 已经提到和指出的那样。

这是一个有趣的问题。如果 table 被多次删除,我们可以执行 UNDROP。 可以在这里找到一个很好的解释 - https://community.snowflake.com/s/article/Solution-Unable-to-access-Deleted-time-travel-data-even-within-retention-period

https://docs.snowflake.com/en/user-guide/data-time-travel.html?_ga=2.118857801.110877935.1647736580-83170813.1644772168&_gac=1.251330994.1646009703.EAIaIQobChMIuZ3o2Zeh9gIVjR-tBh3PvQUIEAAYASAAEgKYevD_BwE#example-dropping-and-restoring-a-table-multiple-times

我也测试了场景,如下图-

参考以下历史记录

SNOWFLAKE1#COMPUTE_WH@TEST_DB.PUBLIC>select query_id,query_text,start_time from table(information_schema.query_history()) where query_text like '%test_undrop_1%';
+--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------+-------------------------------+
| QUERY_ID                             | QUERY_TEXT                                                                                                                    | START_TIME                    |
|--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------+-------------------------------|
| 01a31a99-0000-81fe-0001-fa120003d75e | select query_id,query_text,start_time from table(information_schema.query_history()) where query_text like '%test_undrop_1%'; | 2022-03-22 14:13:58.953 -0700 |
| 01a31a99-0000-81c6-0001-fa120003f7ee | drop table test_undrop_1;                                                                                                     | 2022-03-22 14:13:55.098 -0700 |
| 01a31a99-0000-81fe-0001-fa120003d73e | create or replace table test_undrop_1(id number, name varchar2(10));                                                          | 2022-03-22 14:13:53.425 -0700 |
| 01a31a99-0000-81fe-0001-fa120003d72a | drop table test_undrop_1;                                                                                                     | 2022-03-22 14:13:46.968 -0700 |
| 01a31a99-0000-81c6-0001-fa120003f79e | create or replace table test_undrop_1(id1 number, name varchar2(10));                                                         | 2022-03-22 14:13:44.002 -0700 |
| 01a31a99-0000-81fe-0001-fa120003d70e | drop table test_undrop_1;                                                                                                     | 2022-03-22 14:13:36.078 -0700 |
| 01a31a99-0000-81c6-0001-fa120003f77e | select query_id,query_text,start_time from table(information_schema.query_history()) where query_text like '%test_undrop_1%'; | 2022-03-22 14:13:14.711 -0700 |
| 01a31a99-0000-81fe-0001-fa120003d70a | select count(*) from test_undrop_1;                                                                                           | 2022-03-22 14:13:04.640 -0700 |
| 01a31a98-0000-81fe-0001-fa120003d706 | select * from test_undrop_1;                                                                                                  | 2022-03-22 14:12:52.230 -0700 |
| 01a31a98-0000-81c6-0001-fa120003f75e | create or replace table test_undrop_1(id1 number, name1 varchar2(10));                                                        | 2022-03-22 14:12:43.734 -0700 |
+--------------------------------------+-------------------------------------------------------------------------------------------------------------------------------+-------------------------------+

SNOWFLAKE1#COMPUTE_WH@TEST_DB.PUBLIC>select * from test_undrop_1;
+----+------+
| ID | NAME |
|----+------|
+----+------+
0 Row(s) produced. Time Elapsed: 0.760s
SNOWFLAKE1#COMPUTE_WH@TEST_DB.PUBLIC>alter table TEST_UNDROP_1 rename to test_undrop_1_1;
+----------------------------------+
| status                           |
|----------------------------------|
| Statement executed successfully. |
+----------------------------------+
1 Row(s) produced. Time Elapsed: 0.142s

UNDROP-1

SNOWFLAKE1#COMPUTE_WH@TEST_DB.PUBLIC>undrop table test_undrop_1;
+--------------------------------------------+
| status                                     |
|--------------------------------------------|
| Table TEST_UNDROP_1 successfully restored. |
+--------------------------------------------+
1 Row(s) produced. Time Elapsed: 0.155s
SNOWFLAKE1#COMPUTE_WH@TEST_DB.PUBLIC>select * from test_undrop_1;
+----+------+
| ID | NAME |
|----+------|
+----+------+
0 Row(s) produced. Time Elapsed: 0.223s
SNOWFLAKE1#COMPUTE_WH@TEST_DB.PUBLIC>alter table TEST_UNDROP_1 rename to test_undrop_1_2;
+----------------------------------+
| status                           |
|----------------------------------|
| Statement executed successfully. |
+----------------------------------+
1 Row(s) produced. Time Elapsed: 0.191s

UNDROP-2

SNOWFLAKE1#COMPUTE_WH@TEST_DB.PUBLIC>undrop table test_undrop_1;
+--------------------------------------------+
| status                                     |
|--------------------------------------------|
| Table TEST_UNDROP_1 successfully restored. |
+--------------------------------------------+
1 Row(s) produced. Time Elapsed: 0.155s
SNOWFLAKE1#COMPUTE_WH@TEST_DB.PUBLIC>select * from test_undrop_1;
+-----+------+
| ID1 | NAME |
|-----+------|
+-----+------+
0 Row(s) produced. Time Elapsed: 0.140s
SNOWFLAKE1#COMPUTE_WH@TEST_DB.PUBLIC>alter table TEST_UNDROP_1 rename to test_undrop_1_3;
+----------------------------------+
| status                           |
|----------------------------------|
| Statement executed successfully. |
+----------------------------------+
1 Row(s) produced. Time Elapsed: 0.396s

UNDROP-3(是的!得到了我的 table 版本)

SNOWFLAKE1#COMPUTE_WH@TEST_DB.PUBLIC>undrop table test_undrop_1;
+--------------------------------------------+
| status                                     |
|--------------------------------------------|
| Table TEST_UNDROP_1 successfully restored. |
+--------------------------------------------+
1 Row(s) produced. Time Elapsed: 0.149s
SNOWFLAKE1#COMPUTE_WH@TEST_DB.PUBLIC>select * from test_undrop_1;
+-----+-------+
| ID1 | NAME1 |
|-----+-------|
+-----+-------+
0 Row(s) produced. Time Elapsed: 0.178s