w3resource

Update Stored Data by Refreshing a Materialized View


Refresh a Materialized View after Underlying Updates

Write a PostgreSQL query to refresh a materialized view so that it reflects the most recent data changes.

Solution:

-- Refresh the materialized view to update aggregated sales data.
REFRESH MATERIALIZED VIEW SalesSummary;

Explanation:

  • Purpose of the Query:
    • The goal is to update the stored data in a materialized view after changes occur in the underlying tables.
    • This demonstrates that while materialized views aren’t directly updatable, they can be refreshed to reflect current data.
  • Key Components:
    • REFRESH MATERIALIZED VIEW SalesSummary; : Rebuilds the data stored in the materialized view.
  • Real-World Application:
    • Often used in reporting or dashboard scenarios where precomputed data needs periodic updating.

Notes:

  • Refreshing a materialized view can be resource-intensive; schedule during low-traffic periods if necessary.

For more Practice: Solve these Related Problems:

  • Write a PostgreSQL query to refresh a materialized view that aggregates monthly transaction data after a bulk update in the Transactions table.
  • Write a PostgreSQL query to refresh a materialized view that computes total inventory across warehouses following multiple stock updates.
  • Write a PostgreSQL query to refresh a materialized view summarizing customer order histories after recent order insertions.
  • Write a PostgreSQL query to refresh a materialized view that aggregates real-time financial data after updates in the MarketData table.


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous PostgreSQL Exercise: Update Data through a View Using an INSTEAD OF Trigger.

Next PostgreSQL Exercise: Update Data through a View with Column Aliases.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.