Obafemi Emmanuel

Understanding Views and Materialized Views in SQL

Published 3 months ago

What are Views?

Views in SQL are virtual tables that represent the result of a stored query. Unlike physical tables, views do not store data themselves; instead, they provide a way to structure and simplify data retrieval by encapsulating complex queries into a single entity. Views can be used for security purposes, data abstraction, and simplifying queries.


Benefits of Using Views

  • Data Abstraction: Users can access only the required data without knowing the underlying table structures.
  • Security: Restricts access to sensitive columns while still allowing access to necessary information.
  • Simplicity: Reduces complexity in queries by encapsulating joins, filters, and aggregations.
  • Reusability: Queries defined in views can be used multiple times without rewriting them.

Creating and Managing Views

Creating a View

A view is created using the CREATE VIEW statement. The syntax is as follows:

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

Example

CREATE VIEW EmployeeView AS
SELECT employee_id, name, department
FROM Employees
WHERE status = 'Active';

This view presents only active employees with selected columns from the Employees table.


Querying a View

Once created, a view can be queried just like a regular table:

SELECT * FROM EmployeeView;

Updating a View

Views based on a single table with no aggregations or complex joins can often be updated:

UPDATE EmployeeView SET department = 'HR' WHERE employee_id = 101;

However, not all views are updatable. Views that include joins, aggregations, or subqueries may be read-only.


Modifying a View

To modify an existing view, use the CREATE OR REPLACE VIEW statement:

CREATE OR REPLACE VIEW EmployeeView AS
SELECT employee_id, name, department, salary
FROM Employees
WHERE status = 'Active';

Dropping a View

To remove a view:

DROP VIEW EmployeeView;

Differences Between Views and Tables



Using Materialized Views for Performance

What are Materialized Views?

Unlike regular views, materialized views store the result set physically in the database. This improves performance for complex queries, especially those involving aggregations and joins. However, materialized views require periodic refreshes to stay updated with the base tables.


Creating a Materialized View

CREATE MATERIALIZED VIEW SalesSummary AS
SELECT region, SUM(sales) AS total_sales
FROM Sales
GROUP BY region;

Refreshing a Materialized View

Materialized views can be refreshed manually or automatically:

  • Manual Refresh
REFRESH MATERIALIZED VIEW SalesSummary;
  • Automatic Refresh (Using ON COMMIT)
CREATE MATERIALIZED VIEW SalesSummary
AS SELECT region, SUM(sales) AS total_sales
FROM Sales
GROUP BY region
WITH DATA
REFRESH FAST ON COMMIT;

Benefits of Materialized Views

  • Improves Performance: Precomputed results make querying faster.
  • Efficient Aggregations: Ideal for reporting and data warehousing.
  • Reduces Query Complexity: Eliminates the need for repeated calculations.

When to Use Materialized Views?

  • When dealing with large datasets where repeated complex queries impact performance.
  • For caching aggregated data in analytical and reporting applications.
  • When real-time updates are not critical, and periodic refreshes are acceptable.

Conclusion

Views and materialized views serve distinct purposes in SQL. Regular views simplify query logic, enhance security, and provide a virtual representation of data without storing it. Materialized views, on the other hand, store query results physically, improving performance at the cost of storage and refresh overhead. Choosing between them depends on the specific use case, whether it’s query simplification or performance optimization in large-scale data processing.


Leave a Comment


Choose Colour