Docsity

Database Management Systems: Materialized Views and Maintenance for Decision Support, Slides of Database Management Systems (DBMS)

Toggle sidebar
bg1
1
Database Management Systems, 2ndEdition. R. Ramakrishnan and J. Gehrke 1
Data Warehousing and Decision
Support
Chapter 23, Part B
Database Management Systems, 2ndEdition. R. Ramakrishnan and J. Gehrke 2
Views and Decision Support
OLAP queries are typically aggregate queries.
Precomputation is essential for interactive response
times.
The CUBE is in fact a collection of aggregate queries,
and precomputation is especially important: lots of
work on what is best to precompute given a limited
amount of space to store precomputed results.
Warehouses can be thought of as a collection of
asynchronously replicated tables and
periodically maintained views.
Has renewed interest in view maintenance!
Database Management Systems, 2ndEdition. R. Ramakrishnan and J. Gehrke 3
View Modification (Evaluate On Demand)
CREATE VIEW RegionalSales(category,sales,state)
AS SELECT P.category, S.sales, L.state
FROM Products P, Sales S, Locations L
WHERE P.pid=S.pid AND S.locid=L.locid
SELECT R.category, R.state, SUM(R.sales)
FROM RegionalSales AS R GROUP BY R.category, R.state
SELECT R.category, R.state, SUM(R.sales)
FROM (SELECT P.category, S.sales, L.state
FROM Products P, Sales S, Locations L
WHERE P.pid=S.pid AND S.locid=L.locid) AS R
GROUP BY R.category, R.state
View
Query
Modified
Query
pf3
pf4
pf5

Partial preview of the text

Download Database Management Systems: Materialized Views and Maintenance for Decision Support and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 1

Data Warehousing and Decision

Support

Chapter 23, Part B

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 2

Views and Decision Support

™ OLAP queries are typically aggregate queries.

ƒ Precomputation is essential for interactive response times. ƒ The CUBE is in fact a collection of aggregate queries, and precomputation is especially important: lots of work on what is best to precompute given a limited amount of space to store precomputed results.

™ Warehouses can be thought of as a collection of

asynchronously replicated tables and

periodically maintained views.

ƒ Has renewed interest in view maintenance!

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 3

View Modification (Evaluate On Demand)

CREATE VIEW RegionalSales(category,sales,state) AS SELECT P.category, S.sales, L.state FROM Products P, Sales S, Locations L WHERE P.pid=S.pid AND S.locid=L.locid

SELECT R.category, R.state, SUM (R.sales) FROM RegionalSales AS R GROUP BY R.category, R.state

SELECT R.category, R.state, SUM (R.sales) FROM ( SELECT P.category, S.sales, L.state FROM Products P, Sales S, Locations L WHERE P.pid=S.pid AND S.locid=L.locid) AS R GROUP BY R.category, R.state

View

Query

Modified Query

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 4

View Materialization

(Precomputation)

™ Suppose we precompute RegionalSales and store

it with a clustered B+ tree index on

[category,state,sales].

ƒ Then, previous query can be answered by an index- only scan. SELECT R.state, SUM(R.sales) FROM RegionalSales R WHERE R.category=“Laptop” GROUP BY R.state

SELECT R.state, SUM(R.sales) FROM RegionalSales R WHERE R. state=“Wisconsin” GROUP BY R.category Index on precomputed view is great!

Index is less useful (must scan entire leaf level).

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 5

Materialized Views

™ A view whose tuples are stored in the database

is said to be materialized.

ƒ Provides fast access, like a (very high-level) cache. ƒ Need to maintain the view as the underlying tables change. ƒ Ideally, we want incremental view maintenance algorithms.

™ Close relationship to data warehousing, OLAP,

(asynchronously) maintaining distributed

databases, checking integrity constraints, and

evaluating rules and triggers.

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 6

Issues in View Materialization

™ What views should we materialize, and what

indexes should we build on the precomputed

results?

™ Given a query and a set of materialized

views, can we use the materialized views to

answer the query?

™ How frequently should we refresh

materialized views to make them consistent

with the underlying tables? (And how can we

do this incrementally?)

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 10

Issues in View Maintenance (1)

™ What information is available? (Base relations,

materialized view, ICs). Suppose parts(p5,5000)

is inserted:

ƒ Only materialized view available: Add p5 if it isn’t there. ƒ Parts table is available: If there isn’t already a parts tuple p5 with cost >1000, add p5 to view.

  • May not be available if the view is in a data warehouse! ƒ If we know pno is key for parts: Can infer that p5 is not already in view, must insert it.

expensive_parts(pno) :- parts(pno, cost), cost > 1000

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 11

Issues in View Maintenance (2)

™ What changes are propagated? (Inserts, deletes,

updates). Suppose parts(p1,3000) is deleted:

ƒ Only materialized view available: If p1 is in view, no way to tell whether to delete it. (Why?)

  • If count(#derivations) is maintained for each view tuple, can tell whether to delete p1 (decrement count and delete if = 0). ƒ Parts table is available: If there is no other tuple p with cost >1000 in parts, delete p1 from view. ƒ If we know pno is key for parts: Can infer that p1 is currently in view, and must be deleted.

expensive_parts(pno) :- parts(pno, cost), cost > 1000

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 12

Issues in View Maintenance (3)

™ View definition language? (Conjunctive queries,

SQL subset, duplicates, aggregates, recursion)

™ Suppose parts(p5,5000) is inserted:

ƒ Can’t tell whether to insert p5 into view if we’re only given the materialized view.

Supp_parts(pno) :- suppliers(sno, pno), parts(pno, cost)

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 13

Incremental Maintenance Alg: One Rule, Inserts

™ Step 0: For each tuple in the materialized view, store a “derivation count”. ™ Step 1: Rewrite this rule using Seminaive rewriting, set “delta_old” relations for Rel1 and Rel2 to be the inserted tuples. ™ Step 2: Compute the “delta_new” relations for the view relation. ƒ Important: Don’t remove duplicates! For each new tuple, maintain a “derivation count”. ™ Step 3: Refresh the stored view by doing “multiset union” of the new and old view tuples. (I.e., update the derivation counts of existing tuples, and add the new tuples that weren’t in the view earlier.)

View(X,Y) :- Rel1(X,Z), Rel2(Z,Y)

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 14

Incremental Maintenance Alg: One Rule, Deletes

™ Steps 0 - 2: As for inserts.

™ Step 3: Refresh the stored view by doing

“multiset difference ” of the new and old

view tuples.

ƒ To update the derivation counts of existing tuples, we must now subtract the derivation counts of the new tuples from the counts of existing tuples.

View(X,Y) :- Rel1(X,Z), Rel2(Z,Y)

Database Management Systems, 2 nd^ Edition. R. Ramakrishnan and J. Gehrke 15

Incremental Maintenance Alg: General

™ The “counting” algorithm can be generalized

to views defined by multiple rules. In fact, it

can be generalized to SQL queries with

duplicate semantics, negation, and

aggregation.

ƒ Try and do this! The extension is straightforward.