

Redshift materialized views simplify complex queries across multiple tables with large amounts of data. The result is significant performance improvement!
To derive information from data, we need to analyze it. We do this by writing SQL against database tables. Sometimes this might require joining multiple tables, aggregating data and using complex SQL functions.
If this task needs to be repeated, you save the SQL script and execute it or may even create a SQL view. A view by the way, is nothing more than a stored SQL query you execute as frequently as needed.
However, a view does not generate output data until it is executed. In other words, if a complex sql query takes forever to run, a view based on the same SQL will do the same. This is where materialized views come in handy.
When a materialized view is created, the underlying SQL query gets executed right away and the output data stored. So, when you call the materialized view, all its doing is extracting data from the stored results.
Think of a materialized view as the best of a table (data storage) and a view (stored sql query).
A Redshift materialized views save us the most expensive resource of all – time.
Materialized views in Redshift have some noteworthy features. Let’s take a look at a few.
Redshift materialized views are not without limitations. Let’s take a look at the common ones.
Incremental refresh on the other hand has more than a few. I have them listed below.
Now that we have a feel for the limitations on materialized views, let’s look at 6 best practices when using them.
Select * from SVL_MV_REFRESH_STATUS;
This is an extremely helpful view, so get familiar with it. At a minimum check for the 5 listed details in the SVL_MV_REFRESH_STATUS view.
The Redshift CREATE MATERIALZIED VIEW statement creates the view based on a SELECT AS statement. This is very similar to a standard CTAS statement.
A major benefit of this Select statement, you can combine fields from as many Redshift tables or external tables using the SQL JOIN clause.
Let’s look at how to create one. Instead of the traditional approach, I have two examples listed. The first with defaults and the second with parameters set.
It’s a lot simpler to understand this way.
In this first example we create a materialized view based on a single Redshift table. The default values for backup, distribution style and auto refresh are shown below. Note, you do not have to explicitly state the defaults. They are implied.
Example1: Redshift create materialized view using DEFAULTS. CREATE MATERIALIZED VIEW mv_new_address AS SELECT * from addresses where address_updated ='Y'; BACKUP: YES DISTRIBUTION STYLE: EVEN AUTO REFRESH: NO
In this second example we create the same materialized view but specify the parameter values based on our needs.
The values used in this example are meant to clarify the syntax and usage of these parameters. Be sure to determine your optimal parameter values based on your application needs.
Example2: Redshift create materialized view with user defined parameter values. CREATE MATERIALIZED VIEW mv_new_address
BACKUP NO
DISTSTYLE KEY
DISTKEY (zipcode)
SORTKEY AUTO
AUTO REFRESH YES
AS SELECT * from addresses where address_updated ='Y';
When you create a materialized view, you must set the AUTO REFRESH parameter to YES. If this feature is not set, your view will not be refreshed automatically.
In case you forgot or chose not to initially, use an ALTER command to turn on auto refresh at any time.
Both terms apply to refreshing the underlying data used in a materialized view.
In an incremental refresh, the changes to data since the last refresh is determined and applied to the materialized view. On the other hand, in a full refresh the SELECT clause in the view is executed and the entire data set is replaced.
In summary, Redshift materialized views do save development and execution time. However, it’s important to know how and when to use them. Make sure you really understand the below key areas –
Need to Create tables in Redshift?
We have a post on Creating Redshift tables with examples, 10 ways. Most developers find it helpful.
| Cookie | Duration | Description |
|---|---|---|
| cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
| cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
| cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
| cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
| cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
| viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |