Oracle Database / Advanced: secure and recoverable evolution / Lesson 21
Materialized View Refresh and Freshness
Define a materialized view’s refresh contract and reconcile it with source data in an Oracle 19c lab.
On this page
Lesson goals
- Distinguish complete and fast refresh at a conceptual level.
- Create an illustrative on-demand complete-refresh view.
- Verify freshness and operational prerequisites before use.
Define the freshness requirement
A materialized view stores the result of a defining query and can require refresh as source data changes. Define the acceptable lag, source transaction boundaries, owner, refresh method, and failure response before using it in a report. A successful prior refresh does not prove that the stored result includes later source changes.
For this bounded Oracle Database 19c lab, assume a synthetic LAB_ORDERS table with CUSTOMER_ID and AMOUNT and a dedicated schema with required privileges. The following DDL and PL/SQL are illustrative and runnable:false. DDL has transaction implications; use an isolated environment and follow the course’s transaction guidance.
Create an on-demand aggregate
The first example defines a customer-level sum, builds it immediately, and specifies complete refresh on demand. It is a lab definition, not a deployment recommendation. Confirm schema privileges, storage, source dependencies, and applicable product terms with the database owner before creating an object in any real environment.
CREATE MATERIALIZED VIEW lab_customer_totals
BUILD IMMEDIATE
REFRESH COMPLETE ON DEMAND
AS
SELECT customer_id, SUM(amount) AS total_amount
FROM lab_orders
GROUP BY customer_id;
The defining query groups synthetic order amounts by customer and the refresh clause requests complete on-demand refresh.
Refresh and reconcile
The second example requests a complete refresh through DBMS_MVIEW in the lab. After the operation, compare the view with the defining query at a documented source cutoff and inspect refresh status using authorized monitoring. The example does not claim an actual refresh, duration, or lock behavior in a particular environment.
BEGIN
DBMS_MVIEW.REFRESH('LAB_CUSTOMER_TOTALS', 'C');
END;
/
The C method requests a complete refresh of the named lab materialized view.
Assess alternatives and dependencies
Complete refresh recalculates the defining query. Fast refresh can apply changes when the view and supporting configuration satisfy Oracle’s requirements; its availability must be checked for the specific definition. Dependencies, concurrent changes, refresh order, resource use, and product entitlements require an explicit operational review rather than assumptions from syntax alone.
Test failure and recovery
In an isolated test, change a synthetic source row, record the expected aggregate, run the approved refresh, and reconcile counts and totals. Test a failed or delayed refresh and define how consumers see freshness and whom to notify. Preserve recovery and rollback procedures, and measure the actual workload before setting a schedule.
Common errors and fixes
A report shows stale totals.
- Why it happens
- Source data changed after the last successful refresh.
- How to fix it
- Check refresh status and run the approved refresh or recovery procedure.
- Prevent it next time
- Publish a freshness contract and monitor failures.
Fast refresh is assumed available for every definition.
- Why it happens
- The view’s eligibility and supporting configuration were not checked.
- How to fix it
- Inspect the specific definition and Oracle’s documented requirements.
- Prevent it next time
- Test refresh capability before scheduling.
Creating a view unexpectedly affects a transaction.
- Why it happens
- DDL transaction behavior was ignored.
- How to fix it
- Stop and assess the transaction state in the isolated lab.
- Prevent it next time
- Plan DDL boundaries and use a dedicated environment.
2 practice exercises
1. A source amount changes after the last on-demand refresh. Can the stored aggregate be assumed current?
Show worked solution
No. It may still reflect the earlier source state. Check refresh evidence and reconcile with the defining query at an agreed cutoff before reporting it as current.
2. Why is a complete-refresh lab not proof that fast refresh is supported?
Show worked solution
Fast refresh has definition and configuration requirements. Evaluate the specific view, source changes, privileges, and applicable product terms using Oracle’s documentation and a controlled test.
Key takeaways
- A materialized view needs an explicit freshness and ownership contract.
- Refresh methods have different prerequisites and costs.
- Reconcile source and stored results and test recovery.