Database Management
Materialized Views
Master Table
SQL
Data Updating

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.

Practice system design

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

  1. Update the Master Table: Start by updating the data in the master table as per the requirements using SQL UPDATE statements. For instance:
sql
   UPDATE master_table 
   SET column_name = 'new_value'
   WHERE condition;

This operation modifies the master table but does not automatically reflect in the materialized view.

  1. 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:
sql
   REFRESH MATERIALIZED VIEW my_materialized_view;

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:

  1. Update the products table:
sql
   UPDATE products 
   SET price = price * 1.1
   WHERE product_id = 101;
  1. Refresh the product_summary materialized view:
sql
   REFRESH MATERIALIZED VIEW product_summary;

Summary Table

ActionSQL CommandDescription
Update the Master TableUPDATE master_table SET...Directly modifies the data in the table.
Refresh Materialized ViewREFRESH 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.