Using BigQuery Time Travel

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
Search for a command to run...

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
No comments yet. Be the first to comment.
Practical guides to making BigQuery queries faster and cheaper — partitioning, clustering, search indexes, time travel, cost optimization, and query tuning strategies.
When it comes to tuning a #SQL query in #BigQuery for top performance and cost-efficiency, here's my starting point: 🔍 1. Early Filtering:- Filter out unnecessary rows at the earliest.- Only select columns that are required.- Remove all the redundan...
Here's a useful Dataform concept: pre_operations and post_operations. As the name implies, these represent a set of actions that run before and after the main operation (table, view, or SQL operations

BigQuery has always been a SQL engine for tabular data. Object tables add an interesting twist to that. Instead of rows containing values, an object table gives you one row per file — pointing at da

Query your data lake with warehouse-grade security and performance — without moving a single file.

Ever run a heavy BigQuery SQL query, processed gigabytes of data — and then accidentally closed the tab or forgot to save the results? 😬 Don't re-run it. Your results are still there. BigQuery automa

You can use query parameters in BigQuery hashtag#SQL (now in the console as well!) — but how are they different from variables, and when should you use each? Both parameters and variables act as place

In this practical BigQuery exercise, we’re going to look at BigQuery Time Travel and see how it can help us when working with data. It’s not as powerful as Marty McFly’s DeLorean in Back to the Future (nobody knows what your future data will look like), but a useful tool in our toolset nevertheless.
First of all, what is BigQuery Time Travel? It allows for retrieving the state of a particular table at a given point within a time window, which is set by default to 7 days.
Let’s have a look at an example. At 11:00 AM we’ll create the following table.
CREATE TABLE `learning.test_time_travel` AS
SELECT 1 as id, 'abc' as value
UNION ALL
SELECT 2 as id, 'def' as value

Then, several minutes later, we make some changes to that table, say, insert a row.
INSERT INTO `learning.test_time_travel`
SELECT 3 AS id, 'hij' AS value

We can now confirm that we have the extra row. But what if we’d like to query the table as of earlier?
SELECT * FROM learning.test_time_travel
FOR SYSTEM_TIME AS OF TIMESTAMP('2023-04-02 11:00:00.000 UTC')

Using the approach above we can query the table at any particular point in the time travel window, set by default to 7 days, but configurable to be between 2 and 7 days.
The time travel window is set at a dataset level, so affects all the tables in that dataset. The default time travel windows (7 days) can be overridden either at dataset creation time or on an existing dataset.
CREATE SCHEMA my_project.my_dataset
OPTIONS (
max_time_travel_hours = your_number_of_hours_here
);
ALTER SCHEMA my_dataset
SET OPTIONS(
max_time_travel_hours = your_number_of_hours_here
);
The FOR SYSTEM_TIME AS OF clause comes after the table you’d like to apply Time Travel to, so a join query would look as follows.
SELECT t1.*, t2.other_value
FROM learning.test_time_travel t1 FOR SYSTEM_TIME AS OF TIMESTAMP('2023-04-02 11:00:00.000 UTC')
FULL OUTER JOIN learning.other_test_table t2 FOR SYSTEM_TIME AS OF TIMESTAMP('2023-04-02 13:41:00.000 UTC') on t1.id = t2.id
Also, if you’d like to copy the table at a particular point in time, that can be done using the bq utility (part of the gcloud CLI).
bq cp learning.test_time_travel@1680433200000 learning.test_time_travel_backup
Note that the timestamp there is the UNIX epoch in milliseconds, which can be obtained as follows.
SELECT UNIX_MILLIS(TIMESTAMP('2023-04-02 11:00:00.000 UTC'))
In this short practical exercise, we’ve looked at BigQuery Time Travel, a very handy tool to aid us in querying previous states of a particular table. I found it very helpful when debugging data pipelines.
Thanks for reading and stay tuned for more practical BigQuery tips.
Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.