Materialized views are an advanced feature of SQL that store the results of a query in a database. Unlike standard views, which always present up-to-date data from the underlying tables, materialized views capture data at a specific point in time. To ensure the accuracy and relevance of the data, it is necessary to refresh materialized views periodically or as required.
Refreshing a materialized view means updating its contents to reflect changes made in the underlying base tables. Refreshing ensures that the data remains accurate, consistent, and aligned with the source data.
Materialized views can be refreshed in several ways, depending on the requirements:
The syntax to refresh a materialized view varies by SQL dialect. Here is a typical syntax for Oracle SQL:
BEGIN
DBMS_MVIEW.REFRESH('materialized_view_name', 'REFRESH_MODE');
END;
materialized_view_name
: The name of the materialized view.REFRESH_MODE
: Options like 'COMPLETE'
, 'FAST'
, or 'FORCE'
.This method recomputes the entire query and replaces the contents of the materialized view. It is often slower but ensures consistency.
Consider a materialized view mv_sales_data
created as follows:
CREATE MATERIALIZED VIEW mv_sales_data
BUILD IMMEDIATE
REFRESH COMPLETE
AS
SELECT product_id, SUM(sales) AS total_sales
FROM sales
GROUP BY product_id;
BEGIN
DBMS_MVIEW.REFRESH('mv_sales_data', 'COMPLETE');
END;
sales
table.Fast refresh updates only the changed rows using a materialized view log, which tracks changes to the base table.
A materialized view log must be created on the base table:
CREATE MATERIALIZED VIEW LOG ON sales
WITH ROWID, SEQUENCE (product_id, sales)
INCLUDING NEW VALUES;
Materialized view mv_sales_data
is created to allow fast refresh:
CREATE MATERIALIZED VIEW mv_sales_data
BUILD IMMEDIATE
REFRESH FAST
AS
SELECT product_id, SUM(sales) AS total_sales
FROM sales
GROUP BY product_id;
BEGIN
DBMS_MVIEW.REFRESH('mv_sales_data', 'FAST');
END;
This mode automatically decides whether to use a fast or complete refresh, depending on the availability of a materialized view log.
BEGIN
DBMS_MVIEW.REFRESH('mv_sales_data', 'FORCE');
END;
To automate the refresh, define a refresh interval during view creation.
CREATE MATERIALIZED VIEW mv_sales_data
BUILD IMMEDIATE
REFRESH COMPLETE
START WITH SYSDATE
NEXT SYSDATE + 1
AS
SELECT product_id, SUM(sales) AS total_sales
FROM sales
GROUP BY product_id;
START WITH
: Defines when the refresh starts.NEXT
: Defines the interval between refreshes (e.g., every day with SYSDATE + 1
).You can also use triggers to refresh materialized views after specific events:
CREATE OR REPLACE TRIGGER trg_refresh_sales
AFTER INSERT OR UPDATE OR DELETE ON sales
BEGIN
DBMS_MVIEW.REFRESH('mv_sales_data', 'FAST');
END;
You can group multiple materialized views into a refresh group to synchronize their refreshes.
BEGIN
DBMS_REFRESH.MAKE(
name => 'sales_group',
list => 'mv_sales_data, mv_product_data',
next_date => SYSDATE,
interval => 'SYSDATE + 1'
);
END;
Refresh only when explicitly requested:
CREATE MATERIALIZED VIEW mv_sales_data
REFRESH ON DEMAND
AS
SELECT product_id, SUM(sales) AS total_sales
FROM sales
GROUP BY product_id;
BEGIN
DBMS_MVIEW.REFRESH('mv_sales_data');
END;
Base sales
table
Product_ID | Sales |
---|---|
1 | 100 |
2 | 200 |
mv_sales_data
contains:Product_ID | Total_Sales |
---|---|
1 | 100 |
2 | 200 |
UPDATE sales SET sales = 300 WHERE product_id = 1;
COMMIT;
BEGIN
DBMS_MVIEW.REFRESH('mv_sales_data', 'FAST');
END;
Product_ID | Total_Sales |
---|---|
1 | 300 |
2 | 200 |
Refreshing materialized views is a critical task for maintaining data accuracy and performance in SQL databases. Understanding when and how to use each method ensures you can leverage materialized views effectively for your data needs. Happy Coding!❤️