DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Views and Materialized View in SQL

Views in SQL

A view in SQL is a virtual table based on a result set of a SQL query. It doesn't store data itself but dynamically retrieves data from the underlying tables whenever it is queried. Views are mainly used to simplify complex queries, present data in a specific way, and secure sensitive data by allowing users to access only certain parts of a table.

Key Characteristics of Views:

  1. Virtual Table: A view doesn’t store data physically; it retrieves data from underlying tables on the fly.

  2. Simplifies Complex Queries: Views simplify complex queries by encapsulating them in a reusable structure.

  3. Data Security: Views can restrict access to specific columns or rows, providing a layer of security.

  4. Updatability: Some views are updatable, meaning you can perform INSERT, UPDATE, and DELETE operations on them (if certain conditions are met).

Syntax:

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example:

CREATE VIEW employee_view AS
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE department_id = 10;

This view, employee_view, will display only employees from department 10.

Advantages:

Simplification: Reduces the complexity of writing and maintaining complex queries.

Security: Provides restricted access to the underlying tables.

Reusability: The view can be used in multiple queries, ensuring consistency.

Limitations:

Performance: Since the view fetches data dynamically, it may not perform well with very large datasets or complex queries.

Non-Persistent Data: The data in a view is not stored; it's dynamically retrieved, so it can be slower compared to stored tables.


Materialized Views

A materialized view is similar to a regular view, but unlike standard views, it physically stores the data in the database. Materialized views are mainly used to improve query performance in scenarios where data does not change frequently. Instead of re-running the SQL query every time the view is accessed, materialized views store the results, which can be refreshed periodically or on demand.

Key Characteristics of Materialized Views:

  1. Physically Stored Data: Materialized views store the result of the query, which improves query performance.

  2. Refreshable: Materialized views need to be refreshed periodically to reflect changes in the underlying tables.

Complete Refresh: Rebuilds the entire materialized view.

Fast Refresh: Only the changes made to the underlying data are applied.

  1. Performance Improvement: Since data is precomputed and stored, materialized views are faster for complex queries and large datasets.

Syntax:

CREATE MATERIALIZED VIEW materialized_view_name
AS
SELECT column1, column2, ...
FROM table_name
WHERE condition
REFRESH [FAST | COMPLETE | FORCE] [ON DEMAND | ON COMMIT];

Example:

CREATE MATERIALIZED VIEW sales_summary
AS
SELECT product_id, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY product_id
REFRESH FAST ON DEMAND;

This materialized view, sales_summary, stores the total sales for each product and can be refreshed as needed.

Advantages:

Improves Query Performance: Since the data is stored, queries on large and complex datasets execute much faster.

Useful in Data Warehousing: Materialized views are common in data warehousing environments where queries on large datasets need to run efficiently.

Periodic Refresh: You can control how often the view is refreshed to balance performance and data freshness.

Limitations:

Storage Overhead: Materialized views require additional storage since they store the data.

Staleness of Data: The data in a materialized view can become outdated between refreshes, leading to potentially stale data unless frequently refreshed.


Comparison: View vs. Materialized View

When to Use a View:

When you need a virtual table to present data in a specific format or to simplify complex queries.

When you want to restrict user access to sensitive data in the underlying tables.

When real-time data is required and performance is not an issue.

When to Use a Materialized View:

When query performance is critical, especially on large datasets or complex queries.

When you need to precompute and store results for faster retrieval in data-intensive applications like data warehousing.

When the data in the underlying tables does not change frequently, and staleness between refreshes is acceptable.

Conclusion:

Views are best for dynamically presenting data in a structured way, providing a security layer, and simplifying query complexity.

Materialized views are suited for improving query performance by storing precomputed results and are commonly used in scenarios where data is queried frequently but doesn’t change often.

Top comments (0)