DEV Community

Mia Liang
Mia Liang

Posted on • Edited on

Materialized Views: An Introduction

A materialized view is one that is precomputed, allowing it to periodically cache the results of an incoming query in the name of increasing performance and efficiency as much as possible. Generally speaking, those queries that use materialized views are faster and consume fewer resources than those that process queries to base tables - something that is true even if they're both retrieving the exact same data.

What is a Materialized View?

Based on all the above, it's easy to see why materialized views have become one of the best opportunities that organizations have to boost the performance of workloads that use common and repeated queries. Materialized views are also notable because they require no maintenance to speak of, as the view is always recomputed in the background after the base table in question has been changed. Any incremental changes from that base table is added to the materialized view automatically, with absolutely no inputs required on behalf of users.

Another major benefit is that a materialized view is always 100% consistent with the base table, meaning that you're always working from fresh data without exception. Even if a base table is changed in some way due to an update, or because of the expiration of a partition, the materialized view tools you're using will invalidate anything that was impacted and re-read the portion of the base table in question to guarantee accuracy.

Use Cases for Materialized Views

One of the biggest use cases for materialized views involves unlocking faster query performance whenever possible. Organizations who are working with raw tables to perform online analytical processing aggregation that depend on significant processing, for example, stand to benefit enormously from materialized views. The same is true if they are working within a situation that has predictable and repeatable queries, like if they were hoping to improve their business intelligence pipeline.

But as referenced, one of the main use cases for materialized views involves the aggregation of real time data across an enterprise. Some materialized views tools you may be using perform all aggregations in real time, thus making sure that organizational leaders always have up-to-date and totally accurate information to work from when making decisions.

How to Get Started With Materialized Views

To get a better understanding of how to get started with materialized views, one must first learn more about the actual flow of data across a materialized view to begin with.

After the users of your application or other system begin to trigger events and create transactions, that data will then be committed to the source table that you're working with. At that point, an internal trigger in the source table will populate the materialized view log table.

Then, a fast refresh will be executed and any changes that have been created since the last time this happened will be applied to the materialized view. All users can now query data from the materialized view using a variety of tools, all with the confidence that comes with knowing they're working with the latest information from the most recent version of that table's data.

To create a materialized view in the tool you're working with, you can use a DML statement to create a basic table, to load data into it, and to create a materialized view as a result. An example of an SQL command that could be used to create a materialized view is as follows:

CREATE [ OR REPLACE ] [ SECURE ] MATERIALIZED VIEW [ IF NOT EXISTS ] <name>
[ COPY GRANTS ]
( <column_list> )
[ COMMENT = '<string_literal>' ]
[ CLUSTER BY ( <expr1> [, <expr2> ... ] ) ]
AS <select_statement>

Of course, there are a number of potential limitations that you should be aware of. By its nature, materialized views are only as "current" as they were the last time you ran the query. This is why you should make an effort to do so as often as possible. Likewise, a materialized view will only ever be able to query a single table - so you should keep that in mind when making decisions moving forward.

Materialized Views Tools

There are a wide range of different materialized views tools you can choose to work from depending on your needs. They were first implemented by the Oracle Database, for example, and the "Query Rewrite" feature is available from version 8I onward. Any version of PostgreSQL after 9.3 also natively supports materialized views, and you can even use the "Concurrently" syntax to refresh the contents automatically.

SQL Server works with materialized views, although here they go by a different name - "Indexed Views." They're the same basic concept, but they don't require a refresh because they are always in full sync with the original data of the tables by design. This has been a part of every version of SQL Server released since the year 2000.

Just a few of the other materialized views tools you may use include but are not limited to Sybase SQL Anywhere, ClickHouse, Amazon DynamoDB, BigQuery and others. The one that makes the most sense given your use case will obviously vary depending on your needs.

Top comments (1)

Collapse
 
matthewdailey profile image
Matt Dailey

This is an awesome write up! I had one question about about querying and consistency. You say

By its nature, materialized views are only as "current" as they were the last time you ran the query

Is querying the materialized view different from running the original query? Do you need to do the two separately to get the benefit of the view, or does querying the view update it with the latest data? Thanks for any help!