What’s the difference between materialized views and normal views in SQL?
Loading
What’s the difference between materialized views and normal views in SQL?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 1, 2025, 10:29 AM EST
Normal Views (Standard Views):
A normal view is a virtual table based on a SELECT query. It does not store data but dynamically retrieves it when queried. No physical storage, slower compared to materialized views and always shows up-to-date data from the base tables.
-- Example CREATE VIEW ActiveUsers AS SELECT UserID, UserName FROM Users WHERE IsActive = 1;Materialized Views:
A materialized view stores the result of the query physically in the database. Requires disk space since it stores the computed data, faster for read-heavy workloads and needs to be refreshed (manually or automatically) to reflect changes in base tables.
-- Example: CREATE MATERIALIZED VIEW SalesSummary AS SELECT Region, SUM(SalesAmount) AS TotalSales FROM Sales GROUP BY Region; -- To refresh it, you might run: REFRESH MATERIALIZED VIEW SalesSummary;Key Differences Summary:
Shubham SidnaleRank #968 · 2.1K reputationPosted Feb 1, 2025, 11:50 AM EST
The key difference between materialized views and normal views in SQL lies in how they store and retrieve data:
1. Normal Views
CREATE VIEW EmployeeView AS
SELECT EmployeeID, Name, Department FROM Employees;
2. Materialized Views
CREATE MATERIALIZED VIEW SalesSummary AS
SELECT CategoryID, SUM(SalesAmount) AS TotalSales FROM Sales GROUP BY CategoryID;
REFRESH MATERIALIZED VIEW SalesSummary;
When to Use Which?
Feature
Normal View
Materialized View
Storage
No storage (virtual)
Stored physically
Data Freshness
Always up-to-date
Stale until refreshed
Performance
Slower for complex queries
Faster for read-heavy operations
Use Case
Frequently updated data
Precomputed aggregates, reporting
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 1, 2025, 11:27 PM EST
Part - 1
Normal (Virtual) Views:
Definition:
A normal view is a virtual table that is defined by a SQL query. It does not store data physically; rather, it dynamically generates results when the view is queried.
Characteristics:
Materialized Views:
Definition:
A materialized view is similar to a normal view but stores the precomputed result set physically on disk. It is refreshed periodically or on-demand.
Characteristics:
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 1, 2025, 11:44 PM EST
Part -2
SQL code that creates both a normal view and a materialized view for active policies
-- Create a normal (virtual) view for active policies with joined customer, vehicle, and claim details. CREATE VIEW vw_active_policies AS SELECT c.CustomerID, c.Name AS CustomerName, c.Email AS CustomerEmail, p.PolicyID, p.PremiumAmount, p.StartDate, p.EndDate, v.VIN, v.Make, v.Model, v.Year, cl.ClaimID, cl.ClaimStatus, cl.ClaimAmount, cl.ClaimDate FROM CustomerDB.dbo.Customers AS c JOIN PolicyDB.dbo.Policies AS p ON c.CustomerID = p.CustomerID JOIN VehicleDB.dbo.Vehicles AS v ON p.VehicleID = v.VehicleID LEFT JOIN ClaimsDB.dbo.Claims AS cl ON p.PolicyID = cl.PolicyID WHERE p.EndDate >= GETDATE(); -- Only active policies -- Create a materialized view for active policies. -- (Note: The exact syntax for materialized views may vary by RDBMS. The following is an example.) CREATE MATERIALIZED VIEW mv_active_policies AS SELECT c.CustomerID, c.Name AS CustomerName, c.Email AS CustomerEmail, p.PolicyID, p.PremiumAmount, p.StartDate, p.EndDate, v.VIN, v.Make, v.Model, v.Year, cl.ClaimID, cl.ClaimStatus, cl.ClaimAmount, cl.ClaimDate FROM CustomerDB.dbo.Customers AS c JOIN PolicyDB.dbo.Policies AS p ON c.CustomerID = p.CustomerID JOIN VehicleDB.dbo.Vehicles AS v ON p.VehicleID = v.VehicleID LEFT JOIN ClaimsDB.dbo.Claims AS cl ON p.PolicyID = cl.PolicyID WHERE p.EndDate >= GETDATE() WITH DATA; -- Precompute and store the result