← All topics

Learn free · topic 331

Materialized View

Materialized View is a database object that stores the output of a query. Unlike a regular view, which is a virtual table derived from the result of a SELECT query, a materialized view physically stores the data in a structured form. This precomputed data can be periodically refreshed to reflect changes in the underlying tables.

It is a snapshot of the data at the time the materialized view was last refreshed. You create a materialized view by defining the structure of the view and specifying the query that populates it.

CREATE MATERIALIZED VIEW EmployeeProfile

AS

SELECT Employee.*, Department.*

FROM Employee, Department

WHERE Employee.DeptID = Department.DeptID

In this example, we create a materialized view named " EmployeeProfile " that summarizes Employees Details based on Employee and Department tables.

Unlike regular views, materialized views store the data physically. The result set of the query materialized, meaning it's stored in a table-like structure within the database.

Materialized views are not automatically updated whenever the underlying data changes. You typically need to refresh them periodically to synchronize the data with the latest changes in the base tables.

REFRESH MATERIALIZED VIEW EmployeeProfile;

This command updates the materialized view with the latest data from the underlying tables.

Once a materialized view is created, you can query it just like any other table or view.

SELECT * FROM EmployeeProfile WHERE Age > 25;

A screenshot of a computer

Description automatically generatedThis query retrieves data from the materialized view, allowing you to leverage the precomputed results.

Advantages of Materialized Views:

  • Performance Optimization: Materialized views can significantly improve query performance because they store precomputed results, reducing the need to perform complex calculations on the fly.
  • Offline Analysis: Materialized views are particularly useful for scenarios where you need to perform offline analysis on a specific snapshot of the data without querying the live database.
  • Aggregation and Summary: They are effective for aggregating and summarizing data, especially in scenarios where the underlying data changes infrequently compared to the frequency of querying.

Materialized views can be thought of as an extension of regular views. While regular views provide a virtual representation of data, materialized views go a step further by storing the actual results of the associated query.

In some cases, you might use a regular view for real-time querying and a materialized view for periodic reporting or analytical purposes, depending on the requirements of your application.

In summary, materialized views offer a way to store and quickly retrieve precomputed results, providing a balance between real-time data access and performance optimization in scenarios where frequent querying of large datasets is a concern.

Finished reading? Test yourself with 10 questions on this topic.

Go to the questions →

From I Am Datapedia! by Mustafa Qizilbash, published here free by the author. Nothing about your reading is stored.