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 #
View | Materialized 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 #
View | Materialized 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 #
View | Materialized 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 #
View | Materialized 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 #
Feature | View | Materialized View |
---|---|---|
Storage | No | Yes |
Performance | Slower for complex queries | Faster for precomputed queries |
Data Freshness | Always up-to-date | Requires refresh |
Use Case | Dynamic, real-time queries | Precomputed, heavy queries |
Refresh | Not applicable | Manual/Automatic |