Techniqueadvanced
Deferred view maintenance
Also called lazy view maintenance, deferred refresh.
Record base changes in logs and bring a materialized view up to date later, at query time or on a schedule, not in each update.
Deferred view maintenance is the technique of recording the changes to the base tables in logs and bringing a materialized view up to date later, at query time or on a schedule, in place of maintaining it inside each updating transaction. The refresh applies delta rules to the logged changes, as incremental view maintenance does, but at refresh time the base tables already hold their new values. So the rules, which are written for the state before the update, must read that old state as a query over the new state and the logs. Colby, Griffin, Libkin, Mumick and Trickey (1996) showed the errors that come from skipping this step and gave correct algorithms.
In plain words
Keeping a summary table exact after every change makes every change slower. Instead, you can write each change in a side list and fix the summary only when someone reads it, or once an hour.
The catch is that the fix uses rules that ask "what did the tables look like before these changes?" When you finally do the fix, the tables already show the after picture. If you run the rules on the after picture, you get wrong answers.
So you rebuild the before picture from the after picture and the side list: take the after picture, remove what the list says was added, and put back what the list says was removed. Run the rules on that, apply the result to the summary, and empty the list.
When it applies
- Updates are frequent and reads of the view are rare, or the readers accept a view that is a little out of date. Deferred maintenance moves the work out of the updating transactions.
- Many changes cancel or merge between refreshes, for example a tuple that is inserted and then deleted. A log of net changes makes the refresh do no work for it.
- The view is not self-maintainable, so the refresh needs the base tables. The base tables hold the new state, and the logs give the way back to the old state.
- A change is a signed bag: each tuple has an integer weight, positive for insertions and negative for deletions. A set is a Z-set with weights 0 and 1, and distinct maps a Z-set to a set.
- Late data is not a special case. A base change that commits after a refresh goes into the log and reaches the view at the next refresh. The view is stale by at most the time between two refreshes, and every committed change reaches it.
The template
The view is over base tables , and is its stored copy. Each base table has a log , a signed bag of the net change since the last refresh. The past query for is : the current table minus its log, which is the table at the last refresh. DELTA is the procedure of the delta rules page, which reads the old value of each base table and its change.
UPDATE(T) ▷ an ordinary transaction- 1
apply the changes of T to the base tables - 2
for each changed table R_i: L_i ← L_i + (the change of T to R_i) ▷ in the same transaction REFRESH() ▷ on a schedule, or before a read- 3
for each i: P_i ← R_i - L_i ▷ the past query: the state at the last refresh - 4
ΔV ← DELTA(V), with old values P_i and changes L_i - 5
MV ← MV + ΔV - 6
for each i: L_i ← ∅ READ() ▷ lazy maintenance- 7
if some L_i is not empty then REFRESH() - 8
return MV
Lines L3 to L6 run as one transaction, or under snapshot isolation, so that the base tables and the logs that they read belong to the same state. Line L3 does not have to be materialized: each occurrence of in the expression of line L4 can be replaced by the expression . With separate logs of insertions and deletions, as in Colby et al., the past query is , where is the bag monus and the bag union.
Why it works. The invariant is : the stored view is the view of the state at the last refresh. It holds at the start, with empty logs. Line L2 adds the same change to and to , so does not change, and neither does the right side. After line L4, by the correctness of the delta rules, , since . After lines L5 and L6, and the logs are empty, so the invariant holds again, and READ returns of the current state. The reasoning needs line L2 to be atomic with line L1, and lines L3 to L6 to be atomic with respect to updates.
Cost. Line L2 costs expected time per changed tuple with a hash map from tuples to weights, and the logs take memory for each tuple with a nonzero net weight, at most the number of distinct changed tuples. A refresh costs the evaluation of DELTA over logs of size , which grows with the number of changes since the last refresh, not with the size of the tables. Compared with maintenance in each transaction, the total work is at most the same, because changes that cancel are never processed, and each read may wait for one refresh.
Example. This is Example 1.3 of Colby et al. The view is under set semantics, with and , so . A transaction moves from to , so the logs are and , and now and . The correct view is .
| step | what it reads | deletions from | after |
|---|---|---|---|
| the delete rule on the state before | , | ||
| the same rule on the current state | , | , wrong | |
| line L3, then lines L4 and L5 | , |
The past query rebuilds the old state exactly, so the delta rules give .
Classic examples
- Refresh on demand or on a schedule. Database systems that offer materialized views often keep a change log per base table and refresh the view when asked or at fixed times. This is lines L1 to L6.
- Data warehouses. The sources send their changes in batches, and the warehouse views are refreshed at night. The views are often made self-maintainable so that the refresh reads only the batches.
- The differential file. Severance and Lohman keep the changes to a file apart from the file and merge them later. A base log of this page is a differential file of the base table, and Colby et al. note that the two give different equations for the old state.
- Batching in streams. A stream system that updates its results once per micro-batch, not per event, is doing deferred maintenance with a fixed schedule.
Variations
- Three scenarios. Colby et al. study three scenarios that differ in the auxiliary tables that the transactions keep, such as logs of the base changes and tables of changes to the view. In each scenario a procedure makesafe, run in each transaction, keeps an invariant, and refresh brings the view up to date from the auxiliary tables. The more the transactions keep, the less a refresh has to do. The choice trades the cost of each update against the time of a refresh.
- Partial refresh. Apply only the part of the logs up to a given time, so that the view shows a consistent earlier state. The logs then need timestamps, and lines L3 and L6 act on a prefix.
- Lazy refresh of one group. For a grouped view, a read that asks for one group can apply only the log entries of that group.
- Shared logs. Several views over the same base tables read the same logs. Each view keeps a position in the log in place of line L6, and the log is truncated after all views have passed.
Pitfalls
- Evaluating old-state rules on the new state. This is the state bug of the example. In Example 1.2 of Colby et al., a projection of a join counts some view tuples twice for the same reason.
- Reading logs and tables from different states. If an update commits between the read of and the read of at line L3, the past query is wrong. Use one transaction or one snapshot.
- Logging the change outside the update. If line L2 runs after the commit of line L1 and the system crashes in between, the change is lost from the log and the view drifts forever. Keep the log in the same transaction, protected by the same write-ahead log.
- Gross logs in place of net logs. A log that records each insertion and each deletion apart gives a correct past query only if it is read with the matching equation. Merging into net weights is simpler and smaller.
- Unbounded logs. With no refresh, the logs grow without bound. A schedule, or a refresh when the logs reach a size, keeps them small.
Exercises
- Prove that equals the old when and are the net insertions and deletions, and give a log of gross changes for which it fails.
- Write the past query for and expand the delta rules with it. Which terms read only the current tables?
- A view is refreshed every updates. Each update changes one tuple, and half of the updates undo an earlier one. Give the best and the worst number of log entries at a refresh.
- Show that the invariant fails if line L6 runs before line L5, and give a crash point that makes it visible.
References
- Algorithms for deferred view maintenance, Examples 1.2 and 1.3 (the state bug), Section 2.3 (logs and the old state as a query on the new state), Section 2.5, Definition 1 (past queries), Section 3.1 (the invariants, makesafe and refresh) and Section 5 (the algorithms for three scenarios)
- Incremental maintenance of views with duplicates, the delta rules for the bag algebra, written for the state before the update, which the deferred algorithms reuse
- Differential files, their application to the maintenance of large databases, the differential file that holds the changes apart from the main file (general locator)
- Maintenance of materialized views, problems, techniques, and applications, immediate and deferred maintenance as a choice of when the view is brought up to date
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag XHDB2T Edit this page History Report an error Markdown