Incremental View Maintenance
Introduction
We can treat views as functions in programming. It essentially summarizes a query operation. There are however, some differences between virtual view and materialized view.
The syntax for creating view is very similar to that of a table
1 2 3 4 | |
1 2 3 | |
Virtual View vs Materialized View
Virtual view is simply a shortcut for writing out a specific query, and therefore, its results are not stored on persistent storage (i.e. materialized). On the other hand, materialized views are actually be written onto disks, and therefore has actual performance implication (improvements). However, this means that whenever there are updates done to the source tables (or associated tables), we must also update materialized view to keep their data consistent. This is not an issue for virtual views.
Materialized View Maintenance
To address the problem mentioned in this section. We have to update the materialized view whenever a source table is updated.
Notation
- V: The materialized view
- R: The source table
- \(\Delta R^{+}\): The set of tuples inserted into R
- \(\Delta R^{-}\): The set of tuples removed from R
- \(\Delta V^{+}\): The set of tuples to be inserted into V
- \(\Delta V^{-}\): The set of tuples to be deleted from V violate
Delete and Recompute V
A very intuitive approach would be to remove everything from V and recompute V with new state of R. This however, is very expensive because the runtime is not proportional to the size of updates being made to R. Instead, the runtime is proportional to the size of table R.
Example
View Creation
1 2 3 4 | |
Table Update Operation
- INSERT INTO R VALUES (...)
- INSERT INTO R VALUES (...)
- DELETE FROM R WHERE (...)
- COMMIT
View Update Operation
1 2 3 4 5 6 7 | |
Incremental Maintanence
Definition
Self maintained materialized view: a self maintained materialized view is called self maintained if it can compute the next state of the view with just the current state of the view
Example
Note
- \(choose(a,b)\) returns a if a is not null, and returns b is a is null
- \(n0(a)\) returns a if a is not null, and returns 0 if a is null
Assume we have the following materilized view.
1 2 3 4 | |
The computation of \(\Delta V^{+}\) and \(\Delta V^{-}\) is as followed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
Eager vs Deferred
| Eager | Deferred | |
|---|---|---|
| Snapshop 0 | \(V^{0} = V(R_{1}^{0}, ..., R_{n}^{0})\) | |
| \(\Delta V^{+}\) | \(\Delta V^{+} = f^{+}(\Delta R_{1}^{+}, ..., \Delta R_{n}^{-}, \Delta R_{1}^{-}, ..., \Delta R_{n}^{-}, ... V^{0}, R_{1}^{0}, ..., R_{n}^{0})\) | \(\Delta V^{+} = f^{+}(\Delta R_{1}^{+}, ..., \Delta R_{n}^{-}, \Delta R_{1}^{-}, ..., \Delta R_{n}^{-}, ... V^{0}, R_{1}^{1}, ..., R_{n}^{1})\) |
| \(\Delta V^{-}\) | \(\Delta V^{-} = f^{-}(\Delta R_{1}^{+}, ..., \Delta R_{n}^{-}, \Delta R_{1}^{-}, ..., \Delta R_{n}^{-}, ... V^{0}, R_{1}^{0}, ..., R_{n}^{0})\) | \(\Delta V^{-} = f^{-}(\Delta R_{1}^{+}, ..., \Delta R_{n}^{-}, \Delta R_{1}^{-}, ..., \Delta R_{n}^{-}, ... V^{0}, R_{1}^{1}, ..., R_{n}^{1})\) |
| State used for computation | Prior State (State 0) | Current State (State 1) |
| Maintenance timing | Prior to changes made to the table | Anytime we want |
Incremental View Maintenance Algebra
| V | \(\Delta V^{+}\) | \(\Delta V^{-}\) |
|---|---|---|
| \(V = \sigma_{C} R\) | \(V^{+} = \sigma_{C} \Delta R^{+}\) | \(V^{-} = \sigma_{C} \Delta R^{-}\) |
| \(V = R \Join S\) | \(\Delta V^{+} = ((\Delta R^{+} \Join S) \cup (R \Join \Delta S^{+})) - (\Delta R^{+} \Join \Delta S^{+})\) |