Lesson 6 of 13 Amazon Redshift

Speeding Up Repeated Queries with Redshift Materialised Views

This lesson explains how Redshift materialised views store the precomputed result of an expensive aggregation and keep it current, so repeated dashboard queries return in a fraction of the time.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe materialised view features such as auto refresh and automatic query rewriting.
  • Explain how materialised views store streaming ingestion data.
  • Choose between standard, late-binding and materialised views.

Features

A materialised view stores the result of a query, so that queries using it read the stored result instead of recalculating it. Redshift materialised views include several features that keep them current and useful with little effort.

Incremental refresh applies only the changes made to the base tables since the last refresh, rather than recomputing the whole view, where the view's query allows it; this makes frequent refreshes affordable. Automatic refresh keeps views current in the background when the warehouse has spare capacity. Automatic query rewrite lets Redshift answer a query from a matching materialised view even when the query does not name the view, so existing dashboards benefit without changes. Automated materialised views go further: Redshift detects repeated query patterns and creates and maintains views for them itself.

Materialised views in streaming ingestion

Materialised views are also how Redshift streaming ingestion stores data. A view is defined over a Kinesis data stream or an MSK topic, and each refresh reads new records from the stream into the view; with automatic refresh, the view keeps up with the stream continuously. Further materialised views can be built on top of this raw view, for example to parse JSON payloads or aggregate events per minute, so dashboards query compact, current summaries rather than every event. The lesson Loading Streams Directly into Redshift covers this in detail.

The following diagram shows a materialised view between a large sales table and the dashboards that read it.

The daily revenue materialised view. Instead of 200 dashboard users scanning two billion sales rows every minute, the view stores the aggregation by date and region and is refreshed incrementally with only new sales. Automatic query rewrite sends existing reports that run the same aggregation to the view as well.
Refresh runs in the background when the warehouse has spare capacity.

Choosing a view type

Redshift offers three kinds of view. A standard view stores only a query, which runs each time the view is used; it reuses logic but does not speed anything up. A late-binding view, created WITH NO SCHEMA BINDING, also stores only a query but checks the tables it references only when it runs. It is required for views over external Spectrum tables, and it survives its tables being dropped and recreated. A materialised view stores the query's results and is refreshed, often incrementally, so repeated expensive joins and aggregations return quickly. The following table summarises the types.

ViewStores results?Use for
Standard viewNoReusing query logic
Late-binding viewNoViews over external tables, or tables that are dropped and recreated
Materialised viewYesRepeated, expensive aggregations and joins
Sort into groups

Which view type?

Exam skills 3.2.3

Choose the Redshift view type for each need.

Log in to try this activity Interactive activities are free with an account.
Worked example

A dashboard aggregation computed once

A sales dashboard runs the same daily-revenue-by-region aggregation over two billion rows every minute for 200 users. A materialised view stores the result, as follows.

CREATE MATERIALIZED VIEW daily_region_revenue AUTO REFRESH YES AS
SELECT s.sale_date, st.region, SUM(s.amount) AS revenue
FROM sales s JOIN stores st ON st.store_id = s.store_id
GROUP BY s.sale_date, st.region;

Refreshes are incremental, applying only new sales rows. The dashboard queries now read a few thousand precomputed rows instead of scanning the fact table, and automatic query rewrite means existing reports that run the same aggregation benefit without being changed.

Check yourself

What does automatic query rewrite do?

Show answer

It lets queries use a materialised view that they do not name, when the view can answer them.

Exam Tips

Remember for the exam

  • To speed up repeated aggregations, use materialised views with automatic refresh.
  • Automatic query rewrite lets queries benefit from views they do not name.
  • Incremental refresh applies only changes from base tables.
  • Streaming ingestion lands data in materialised views.
  • Late-binding views are needed over external tables.