How to update master table while updating materialized view
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In database management, particularly when dealing with PostgreSQL or Oracle databases, you often encounter the need to manage and update materialized views as well as the master tables from which these views are derived. A materialized view, unlike a standard view, is a database object that contains the results of a query. It can be considered as a snapshot, storing the current state of data retrieved from one or many tables, hence it can improve the performance of complex queries while involving a trade-off in terms of data freshness.
Understanding Materialized Views
Materialized views are used to optimize query performance by pre-calculating expensive join or aggregation operations and storing the result in the database. However, because the data is static, it does not change until the view is manually refreshed. This characteristic introduces challenges when there are updates in the underlying master table(s) upon which the materialized view is based.
How to Update Master Table and Synchronize Materialized View
Step-by-Step Process
- Update the Master Table: Start by updating the data in the master table as per the requirements using SQL
UPDATEstatements. For instance:
This operation modifies the master table but does not automatically reflect in the materialized view.
- Refresh the Materialized View: After updating the master table, you need to refresh the materialized view to make it reflect the changes. This can be done with:
Depending on the database system, this command may vary. For instance, in PostgreSQL, you could use the CONCURRENTLY keyword to allow reads of the materialized view to continue while it is being refreshed.
Best Practices
- Transactional Control: Ensure that the update and refresh are performed in a transactional scope to maintain data consistency.
- Concurrency and Locking: Be aware of potential locking issues and concurrency. If the refresh operations take a long time due to the size of the data, it might block other operations.
- Incremental Refresh: If supported, consider using incremental refresh mechanisms whereby only the changed data is refreshed in the view, making the operation faster and more efficient.
Advanced Techniques
- Trigger-based Automation: In some databases like Oracle, you can use triggers on the master table to automatically initiate a refresh of the materialized view when data changes.
- Asynchronous Background Refresh: Depending on the use-case and data requirements, consider setting up asynchronous processes that periodically refresh the materialized view, minimizing the impact on the overall database performance.
Example Scenario
Suppose you have a database with a master table products and a materialized view product_summary that aggregates data about the products. If a new product is added or existing products are updated, you need to:
- Update the
productstable:
- Refresh the
product_summarymaterialized view:
Summary Table
| Action | SQL Command | Description |
| Update the Master Table | UPDATE master_table SET... | Directly modifies the data in the table. |
| Refresh Materialized View | REFRESH MATERIALIZED VIEW view_name; | Updates the view to reflect table changes. |
Conclusion
Updating a master table along with its associated materialized view requires careful handling to ensure data consistency and system performance. By using proper transaction control, utilizing incremental refreshes when possible, and setting up automatic triggers or asynchronous processes, one can effectively manage the synchronization between the master tables and their dependent materialized views, thus maintaining optimal database performance and data accuracy.
Related reading
- How to update multiple items in a DynamoDB table at once
- How to update SQLAlchemy row entry?
- How to update the _id of one MongoDB Document?
- How to update values using pymongo?
- How to upgrade AWS RDS Aurora MySQL 5.6 to 5.7
- How to upload and retrieve file in mongodb in spring boot application without using GridFSTemplate?
- How to use 2 or more databases with spring?
- How to use aggregate functions in Amazon Dynamodb

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.