which is best materialized view or table with SP? and why?
Loading
which is best materialized view or table with SP? and why?
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
Jaish MathewsRank #225 · 11K reputationPosted Feb 5, 2025, 7:03 AM EST
The choice between a Materialized View and a Table with a Stored Procedure (SP) depends on the use case, performance requirements, and how frequently the data needs to be updated.
1. Materialized View
A Materialized View (MV) is a precomputed result set that is stored physically and can be refreshed periodically.
Pros:
ON COMMITorON DEMAND).Cons:
FAST,COMPLETE, orINCREMENTALrefresh).2. Table with Stored Procedure
A Table with an SP means creating a table to store precomputed results and using a stored procedure to update/populate the table.
Pros:
Cons:
Which One is Best?
When to Use What?
Use Materialized View if:
Use Table with SP if:
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 5, 2025, 9:56 AM EST
Both Materialized Views and Tables with Stored Procedures (SP) have their own use cases, advantages, and disadvantages. The choice between them depends on the specific requirements of your application, such as performance, data freshness, and complexity.
There is a common case for example a retail company wants to generate a daily sales report for all stores. The report includes:
The company needs to decide whether to use a Materialized View or a Table with a Stored Procedure to generate this report.
Why Use a Materialized View?
Implementation
Create the Materialized View
CREATE MATERIALIZED VIEW DailySalesReport AS SELECT StoreID, SUM(SalesAmount) AS TotalSales, AVG(SalesAmount) AS AverageSales, COUNT(TransactionID) AS TotalTransactions FROM Sales GROUP BY StoreID;Refresh the Materialized View Schedule a daily refresh using a database job (e.g., in PostgreSQL or Oracle):
REFRESH MATERIALIZED VIEW DailySalesReport;Query the Materialized View
SELECT * FROM DailySalesReport WHERE StoreID = 101;Advantages
Disadvantages
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 5, 2025, 10:41 AM EST
Why Use a Table with a Stored Procedure?
Implementation
Create the Table
CREATE TABLE DailySalesReport ( StoreID INT, TotalSales DECIMAL(18, 2), AverageSales DECIMAL(18, 2), TotalTransactions INT );Create the Stored Procedure
CREATE PROCEDURE UpdateDailySalesReport AS BEGIN TRUNCATE TABLE DailySalesReport; INSERT INTO DailySalesReport (StoreID, TotalSales, AverageSales, TotalTransactions) SELECT StoreID, SUM(SalesAmount) AS TotalSales, AVG(SalesAmount) AS AverageSales, COUNT(TransactionID) AS TotalTransactions FROM Sales GROUP BY StoreID; END;Execute the Stored Procedure Run the stored procedure manually or schedule it using a job scheduler:
EXEC UpdateDailySalesReport;Query the Table
SELECT * FROM DailySalesReport WHERE StoreID = 101;Advantages
Disadvantages
Choose Materialized View If:
Choose Table with Stored Procedure If: