Back to Chapters
Chapter Progress: 0 %0 / 5 Lessons Read
‹›

3. Clean layers: bronze, silver and gold

3.3 Views, materialized views and time travel

Highlights & notes

Select lesson text to highlight it, or tap Add note.

    Bookmarks

    Bookmark a lesson to see it here.

      A view is a saved query with a name. It stores no data. Each time someone reads the view, BigQuery runs the query again. Use a view to give people a simple, safe window onto a table.

      A materialized view saves the answer as well, and BigQuery keeps it fresh when the base table changes. Reads are fast and cheap. Use one for totals that many dashboards ask for again and again.

      BigQuery also remembers the past. With time travel you can read a table as it was at any moment in the last 7 days. It is the undo button when someone deletes rows by mistake.

      Which one?

      • Table: the data is stored. You refresh it yourself, or Dataform does.
      • View: no data stored. Always current. Costs a query each time.
      • Materialized view: the answer is stored and refreshed for you. Best for repeated totals.
      Must Know:

      A view is not free to read. SELECT * FROM my_view runs the query behind it, and you pay for the bytes that query reads.

      How it fits together

      1. Table: stored rows
      2. View: saved query
      3. Materialized view: saved answer

      A view, then a trip into the past

      Order 104 was deleted by mistake a few minutes ago.

      SQL

      CREATE VIEW gold.delivered_orders ASSELECT order_id, amountFROM silver.ordersWHERE status = 'delivered';SELECT *FROM silver.orders  FOR SYSTEM_TIME AS OF  TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 HOUR);

      Rows at this step

      order_id | status    | amount
      ---------+-----------+-------
      101      | delivered | 55000
      104      | cancelled | 12000
      106      | pending   | 650

      Try it yourself

      1. Create a view like the one in the walkthrough on your own sales.orders table.
      2. Type SELECT * from the view and read the dry-run bytes. Reading a view still reads the table behind it.
      3. Run the time travel query with INTERVAL 1 MINUTE on a table you made more than a minute ago.

      Question 1 · Easy

      What does a normal view store?

      Question 2 · Medium

      Ten dashboards ask for "total sales per day" every few minutes. What fits best?

      Question 3 · Hard

      Rows were deleted by mistake two hours ago. What can you do in BigQuery?

      Key takeaways

      A view saves a query, a materialized view saves the answer and keeps it fresh, and time travel reads a table as it was at any point in the last 7 days.

      Lesson 13 of 90

      Save your progress

      Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.

      Start free Log in
      PreviousViews, materialized views and time travel
      Save your progress free for 1 month. Start free · Log in