Skip to content
MasterData
  • Home
  • All Courses
  • Blog
    • Apache Spark
Start Learning
Start Learning
MasterData
  • Home
  • All Courses
  • Blog
    • Apache Spark

Apache Airflow

2
  • Apache Airflow: What, Why, and How?
  • How to Deploy Apache Airflow on Kubernetes

Apache Iceberg

3
  • [01] – Introduction to Apache Iceberg
  • [02] – Getting Started with Apache Iceberg
  • [03] – Apache Iceberg Architecture

Apache Spark

4
  • [00] – Apache Spark By Example Course
  • [01] – What is Apache Spark?
  • [02] – Installing Apache Spark Locally
  • [03] – Deploy Apache Spark with Kubernetes (K8s)

Data Build Tool (DBT)

7
  • [00] – dbt by Example Course
  • [01] – dbt : What it is, Why and How?
  • [02] – Install dbt in local
  • [03] – Explore dbt Models
  • [04] – Sources in dbt
  • [05] – Seeds in dbt
  • [06] – Jinja Templates and Macros in dbt

SQL - Advanced

2
  • [02] – View vs Materialized View
  • [03] – Window function in SQL

SQL - Basics

1
  • 02 – Understanding SQL Operations (DML, DDL, DCL, and TCL)

SQL - Intermediate

1
  • SQL Joins: Understanding INNER, LEFT, RIGHT, and FULL Joins
  • Home
  • Docs
  • SQL
  • SQL - Advanced
  • [02] – View vs Materialized View
View Categories

[02] – View vs Materialized View

kerrache.massipssa

In this tutorial, you’ll discover the fundamental differences between Views and Materialized Views, including how they handle data storage, retrieval, and performance. By the end, you’ll have a clear understanding of their advantages, limitations, and ideal use cases.

Definition #

ViewMaterialized View
A logical, virtual table that is based on a query. It does not store any data itself; rather, it fetches the data from the base tables whenever it is queried.A physical copy or snapshot of the result of a query. It stores the data on disk, independent of the base tables.

Data Storage #

ViewMaterialized View
Does not store data. Data is fetched dynamically from the base tables each time the view is queried.Stores the query result as a physical table, and it needs storage space.

Performance #

ViewMaterialized View
Slower in performance for large and complex queries because the query runs every time the view is accessed.Faster performance because the data is precomputed and stored. Useful for heavy, expensive queries.

Data Freshness #

ViewMaterialized View
Always up-to-date because it fetches the data directly from the base tables.Can become outdated since it is a snapshot of the data. Needs to be refreshed to stay updated.

Use Cases #

  • View:
    • Simplifies complex queries by abstracting them into a single, reusable structure.
    • Provides security by restricting access to specific rows or columns through the view definition.
    • Suitable for scenarios where up-to-date data is crucial.
  • Materialized View:
    • Used to optimize performance for expensive queries that involve joins, aggregations, or large datasets.
    • Helpful in scenarios where real-time data isn’t required, and periodic updates are acceptable.
    • Ideal for data warehousing and reporting systems.

Refresh Mechanism #

  • View: No need to refresh; always reflects the current state of the base tables.
  • Materialized View:
    • Can be refreshed manually or automatically at specified intervals.
    • Refresh modes:
      • Complete: Recomputes the entire view.
      • Incremental (a.k.a. Fast Refresh): Updates only the changes since the last refresh.

Example #

  • View Example
CREATE VIEW employee_view AS
SELECT id, name, department FROM employees WHERE status = 'active';

Fetches live data from the employees table every time employee_view is queried.

  • Materialized View Example
CREATE MATERIALIZED VIEW employee_mv
BUILD IMMEDIATE
REFRESH FAST ON DEMAND
AS
SELECT id, name, department, SUM(salary) AS total_salary
FROM employees
WHERE status = 'active'
GROUP BY id, name, department;

Stores the result of the query and can be refreshed on demand or at scheduled intervals.

Summary Table #

FeatureViewMaterialized View
StorageNoYes
PerformanceSlower for complex queriesFaster for precomputed queries
Data FreshnessAlways up-to-dateRequires refresh
Use CaseDynamic, real-time queriesPrecomputed, heavy queries
RefreshNot applicableManual/Automatic

Updated on February 27, 2025

Leave a Reply Cancel reply

You must be logged in to post a comment.

Table of Contents
  • Definition
  • Data Storage
  • Performance
  • Data Freshness
  • Use Cases
  • Refresh Mechanism
  • Example
  • Summary Table

Copyright © 2025 MasterData

Powered by MasterData

Manage Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}