Presentation is loading. Please wait.

Presentation is loading. Please wait.

Optimizing queries using materialized views J. Goldstein, P.-A. Larson SIGMOD 2001.

Similar presentations


Presentation on theme: "Optimizing queries using materialized views J. Goldstein, P.-A. Larson SIGMOD 2001."— Presentation transcript:

1 Optimizing queries using materialized views J. Goldstein, P.-A. Larson SIGMOD 2001

2 2 Background: materialized views Main differences between virtual and materialized views (Design), Definition & Storage Maintenance Exploitation at Query Time

3 3 Roadmap Preliminaries SPJ queries, view & query over the same relations in the FROM clause SPJ queries, view & query over different relations in the FROM clause SPJG queries/views Fast filtering of views

4 4 Problem Formulation Given a relational expression in SPJG form, find all materialized (SPJG) views from which the expression can be computed and, for each view found, construct a substitute expression equivalent to the given expression.

5 5 Materialized views in SQL Server Known as indexable views A single-level SQL statement containing selections, (inner) joins, and an optional group-by (SPJG queries). The FROM clause cannot contain derived tables, i.e. it must reference base tables, and subqueries are not allowed. The output of an aggregation view must include all grouping columns as output columns (because they define the key) and a count column. Aggregation functions are limited to sum and count.

6 6 Example

7 7 Simple Rules for SPJ queries Assume an SPJ query over a set of relations & an SPJ materialized view (both in CNF form). Can the view answer the query? You need to obtain a result with All the columns Exactly the same tuples as if the query had been answered by the original set of relations, independently of the contents of the relations and view.

8 8 Roadmap Preliminaries SPJ queries, view & query over the same relations in the FROM clause SPJ queries, view & query over different relations in the FROM clause SPJG queries/views Fast filtering of views

9 9 Simple Rules for SPJ queries 1. The view contains all rows needed by the query expression. Obvious; however, this is not required if substitutes containing unions of views are considered. 2. All required rows can be selected from the view. If one of the columns required by a query’s predicate is missing from the view output, the required rows cannot be selected. 3. All output expressions can be computed from the output of the view. 4. All output rows occur with the correct duplication factor. SQL is based on bag semantics, that is, a base table or the output of a SQL expression may contain duplicate rows. Hence, it is not sufficient that two expressions produce the same set of rows but any duplicate rows must also occur exactly the same number of times.

10 10 Column Equivalence Classes Column equivalence: R.A=S.B ^ S.B R.A=B ^ S.B<4 ^ R.A<4 Necessary in many occasions during query rewriting (e.g., view contains attribute A and query asks for attribute B) Equivalence class: all columns in a query/view linked via R.A=S.B equality predicates

11 11 Simple Rules for SPJ queries 1. The view contains all rows needed by the query expression. 2. All required rows can be selected from the view. 3. All output expressions can be computed from the output of the view. 4. All output rows occur with the correct duplication factor.

12 12 Q1: Are all the query results in the view? Show that the output of the expression Q: select * from T1,T2,…,Tm where Wq produces a subset of the output of V: select * from T1,T2, …,Tm where Wv for all valid instances of tables T1, T2, …, Tm. This is guaranteed to hold if Wq => Wv, where => denotes logical implication & we need an algorithm to decide it

13 13 Q1: Are all the query results in the view? Write query & view as conjuncts of the form PE Q/V : column equality predicates ( R.A=S.B ) PR Q/V : column range predicates ( R.A θ value ) PU Q/V : any other ( R.A like ‘%xxx%’ ) If PE Q ^ PR Q ^ PU Q => PE V ^ PR V ^ PU V We are OK. Still, this is too long to generate & the authors show the simpler

14 14 Q1: Are all the query results in the view? PE Q => PE V PE Q ^ PR Q => PR V PE Q ^ PU Q => PU V A.k.a equijoin, range and residual subsumption tests – explain PE Q everywhere See the paper for missed opportunities Remember: query optimization must be completed very quickly

15 15 Example

16 16 1. Compute Equivalence classes 2. Check their containment (equijoin test) For each equivalence class of the view, there exists an equivalence class of the query that contains it. PE Q => PE V But not vice versa… (why?)

17 17 Compute Equivalence classes & check their containment {l_orderkey, o_orderkey}, {l_partkey, p_partkey}, {o_orderdate}, {l_shipdate} {l_orderkey, o_orderkey}, {l_partkey, p_partkey}, {o_orderdate, l_shipdate}

18 18 3. Range test For each equivalence class involved in range predicates, introduce a range of values Check if the view’s ranges for eq. classes are larger than the query’s ranges for the respective eq. classes PE Q ^ PR Q => PR V Also, this helps to determine what extra, “compensating” predicates need to be applied to the view to obtain the query’s result

19 19 3. Range test

20 20 4. Range test => compensating predicates {l_partkey, p_partkey}<= 160 o_custkey = 123

21 21 3. Range test If the predicate is of type (Ti.Cp <= c), we set the upper bound to the minimum of its current value and c. If is of type (Ti.Cp >= c), we set the upper bound to the maximum of its current value and c. Predicates of the form (Ti.Cp < c) are treated as (Ti.Cp <= c-Δ) where c-Δ denotes the smallest value preceding c in the domain of column Ti.Cp. Predicates of the form (Ti.Cp > c) are treated as (Ti.Cp <= c+Δ). Finally, predicates of the form (Ti.Cp = c) are treated as (Ti.Cp >= c) ^ (Ti.Cp <= c).

22 22 5. Residual predicates Simple matching: must be identical (except for column equivalences) PE Q ^ PU Q => PU V Again, query’s predicates can be a superset of the respective view’s predicates

23 23 5. Residual predicates

24 24 Simple Rules for SPJ queries 1. The view contains all rows needed by the query expression. 2. All required rows can be selected from the view. 3. All output expressions can be computed from the output of the view. 4. All output rows occur with the correct duplication factor.

25 25 Select the appropriate tuples Use the compensating predicates. 3 types: 1. Column equality predicates obtained while comparing view and query equivalence classes. Example : (o_orderdate = l_shipdate) 2. Range predicates obtained while checking query ranges against view ranges. Example: ({p_partkey, l_partkey} <= 160) and (o_custkey = 123). 3. Unmatched residual predicates from the query. Example: (l_quantity*l_extendedprice >100).

26 26 Summary Algorithm 1. Construct compensating column equality predicates while comparing view equivalence classes against query equivalence classes as described in the previous section. Try to map every column reference to an output column (using the view equivalence classes). If this is not possible, reject the view. 2. Construct compensating range predicates by comparing column ranges as described in the previous section. Try to map every column reference to an output column (using the query equivalence classes). If this is not possible, reject the view. 3. Find the residual predicates of the query that are missing in the view. Try to map every column reference to an output column (using the query equivalence classes). If this is not possible, reject the view.

27 27 Simple Rules for SPJ queries 1. The view contains all rows needed by the query expression. 2. All required rows can be selected from the view. 3. All output expressions can be computed from the output of the view. 4. All output rows occur with the correct duplication factor.

28 28 Simple checks for the SELECT clause If the output expression is a constant, just copy the constant to the output. If the output expression is a simple column reference, check whether it can be mapped (using the query equivalence classes) to an output column of the view. For other expressions, we first check whether the view output contains exactly the same expression (taking into account column equivalences). If so, the output expression is just replaced by a reference to the matching view output column. If not, we check whether the expression’s source columns can all be mapped to view output columns, i.e. whether the complete expression can be computed from (simple) output columns. If the view fails these tests, the view is rejected.

29 29 Simple Rules for SPJ queries 1. The view contains all rows needed by the query expression. 2. All required rows can be selected from the view. 3. All output expressions can be computed from the output of the view. 4. All output rows occur with the correct duplication factor.

30 30 Duplicates Trivial when the view and the query have exactly the same relations in the FROM clause. Harder when different sets of relations are involved

31 31 Why extra tables? Extra relations in the query => query has more joins than the view => more restricted result in terms of rows & more extended result in terms of columns (in general) The inverse (extra relations in the view) typically holds, too, except for some cases (see next) Assume View: T1,T2,…, Tn, Tn+1, Tn+2,…, Tn+m Query: T1,T2,…, Tn

32 32 Roadmap Preliminaries SPJ queries, view & query over the same relations in the FROM clause SPJ queries, view & query over different relations in the FROM clause SPJG queries/views Fast filtering of views

33 33 Cardinality-preserving joins A join between tables T and S is cardinality preserving if every row in T joins with exactly one row in S. If so, we can view S as simply extending T with the columns from S. An equijoin between all columns in a non-null foreign key in T and a unique key in S has this property. A foreign key constraint guarantees that, for every row t of T, there exists at least one row s in S with matching column values for all non-null foreign-key columns in t. All columns in t containing a null are ignored when validating the foreign-key constraint. It can be shown that all requirements (equijoin, all columns, non-null, foreign key, unique key) are essential.

34 34 When the view has several extra relations… Foreign-key join graph: The nodes in the graph represent tables T1,T2,…, Tn, Tn+1, Tn+2,…, Tn+m. There is an edge from table Ti to table Tj if the view specifies, directly or transitively, a join between tables Ti and Tj and the join satisfies all the five requirements listed above (equijoin, all columns, non-null, foreign key, unique key). LineitemOrdersCustomer

35 35 Elimination We try to eliminate nodes Tn+1, Tn+2,…, Tn+m by a sequence of deletions. We repeatedly delete any node that has no outgoing edges and exactly one incoming edge. If we succeed in eliminating nodes Tn+1, Tn+2,…, Tn+m, the extra tables in the view can be eliminated through cardinality-preserving joins and the view passes this test. LineitemOrdersCustomer

36 36 Extra relations

37 37 Eq. classes & ranges LineitemOrdersCustomer

38 38 Updated eq. classes for the query Remember: For each equivalence class of the view, there exists an equivalence class of the query that contains it && query ranges are subsets of view ranges

39 39 Roadmap Preliminaries SPJ queries, view & query over the same relations in the FROM clause SPJ queries, view & query over different relations in the FROM clause SPJG queries/views Fast filtering of views

40 40 SPJG queries/views SPJ queries/views with aggregation on top of the SPJ query/view.

41 41 Simple check 1. The SPJ part of the view produces all rows needed by the SPJ part of the query and with the right duplication factor. 2. All columns required by compensating predicates (if any) are available in the view output. 3. (Q’s GB clause) The view contains no aggregation or is less aggregated than the query, i.e, the groups formed by the query can be computed by further aggregation of groups output by the view. 4. (Q’s SELECT clause) All columns required to perform further grouping (if necessary) are available in the view output. Nice usage of count(*) attribute when γ count(Α,Β ) <- sum(γ count (ABC)) 5. All columns required to compute output expressions are available in the view output.

42 42 Roadmap Preliminaries SPJ queries, view & query over the same relations in the FROM clause SPJ queries, view & query over different relations in the FROM clause SPJG queries/views Fast filtering of views

43 43 Problem Hundreds of views to check for each incoming query. Must decide candidate views for a query fast without taking up too much space in M/M

44 44 Filter tree A filter tree is a multiway search tree where all the leaves are on the same level. A node in the tree contains a collection of (key,pointer) pairs. A key consists of a set of values, not just a single value. A pointer in an internal node points to a node at the next level while a pointer in a leaf node points to a list of view descriptions. A filter tree subdivides the set of views into smaller and smaller partitions at each level of the tree.

45 45 Lattice index

46 46 Lattice indexes for all levels of the filter tree Source table condition Hub condition Output column condition Grouping columns Range constrained cols Residual predicate Output expression

47 47 Within each level, lattices… Source table condition: A query cannot be computed from a view unless the view’s set of source tables is a superset of the query’s set of source tables. We build a lattice index using the view’s set of source tables as the key. Given a query’s set of source tables, we search the lattice index for partitions containing views that satisfy this condition. Hub condition: A query cannot be computed from a view unless the hub of the view (remainder after eliminations) is a subset of the query’s set of source tables. We again use a lattice index structure but this time with view hubs as the key instead of the complete set of source tables

48 48 Within each level, lattices… Output column condition: A view cannot provide all required output columns unless, for each equivalence class in the query’s output list, at least one of its columns is available in the view’s extended output list. We build lattice indexes using the extended output lists of the views as keys. Grouping column condition: An aggregation query cannot be computed from an aggregation view unless, for each equivalence class in the query’s grouping list, at least one of its columns is present in the view’s extended grouping list. We build lattice indexes on the extended grouping lists.

49 49 Within each level, lattices… Range constraint condition: A query cannot be computed from a view unless, for each equivalence class in the view’s range constraint list, at least one of its columns is present in the query’s extended range constraint list. Too slow to compute extended ranges per view for every extra range predicate of the query! However, we can build lattice indexes based on a weaker condition involving a reduced range constraint list. The reduced range constraint list contains only columns that reference trivial equivalence classes, i.e. columns that are not equivalent to any other columns. Weak range constraint condition: A query cannot be computed from a view unless the view’s reduced range constraint list is a subset of the query’s extended range constraint list.

50 50 Within each level, lattices… Residual predicate condition: A query cannot be computed from a view unless the view’s residual predicate list is a subset of the query’s residual predicate list. We build lattice indexes using the residual predicate lists of the views as keys. Output expression condition: A query cannot be computed from a view unless its (textual) output expression list is a subset of the view’s (textual) output expression list. We can then build lattice indexes based on the above condition.

51 51 Within each level, lattices… Grouping expression condition: An aggregation query cannot be computed from an aggregation view unless its (textual) grouping expression list is a subset of the view’s (textual) grouping expression list. We can then build lattice indexes based on the above condition.

52 52 Summary The main contributions of this paper are (a) an efficient view matching algorithm for views composed of selections, joins and a final group-by (SPJG views) and (b) a novel index structure (on view definitions, not view data) that quickly narrows the search to a small set of candidate views on which view-matching is applied.


Download ppt "Optimizing queries using materialized views J. Goldstein, P.-A. Larson SIGMOD 2001."

Similar presentations


Ads by Google