19.06.2013 Views

DB2 UDB for z/OS Version 8 Performance Topics - IBM Redbooks

DB2 UDB for z/OS Version 8 Performance Topics - IBM Redbooks

DB2 UDB for z/OS Version 8 Performance Topics - IBM Redbooks

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Rules <strong>for</strong> query rewrite<br />

If the query references tables that are also referenced in MQTs, the optimizer can decide to<br />

select the qualified MQT that gives the best per<strong>for</strong>mance <strong>for</strong> the query.<br />

Figure 3-7 summarizes the checking that is per<strong>for</strong>med <strong>for</strong> a query, if automatic query rewrite<br />

is generally enabled in your <strong>DB2</strong> subsystem at installation time.<br />

The automatic query rewrite is supported <strong>for</strong> dynamically prepared queries that are read-only<br />

and have some other restrictions.<br />

The automatic query rewrite is a general process that can use a materialized query table M if<br />

the fullselect F that makes up the MQT has common source tables as the submitted query Q<br />

that is being optimized by <strong>DB2</strong>.<br />

Other conditions are checked during table mapping to verify if the result of the submitted<br />

query can be derived from or can directly use the result of one or more materialized query<br />

tables.<br />

During a predicate matching process, the predicates in the materialized query table fullselect<br />

are compared, one by one, to the predicates in the query Q.<br />

Diagrammatic Overview of Automatic Query Rewrite<br />

Table mapping<br />

Common table: table in both Q and F<br />

Residual table: table in Q only<br />

Extra table: table in F only<br />

Rejoin table: common table joins with<br />

M to derive non-key columns<br />

Predicate matching<br />

Join predicates: exact match in both Q and M<br />

Local predicates: P in Q subsumes P in M<br />

C > 5 subsumes C > 0;<br />

C in ('A', 'B') subsumes C in ('A', 'B', 'C')<br />

In other words, M contains data that Q needs.<br />

Figure 3-7 Rules <strong>for</strong> query rewrite<br />

Table<br />

mapping<br />

Restrictions on Query<br />

Subselect that can be rewritten:<br />

Read-only query<br />

Contains base tables & table<br />

functions, not outer joins only<br />

No ROWID, LOB types<br />

Heuristics of Selections<br />

Match with no regrouping, no residual<br />

joins, and no rejoins.<br />

Match with no regrouping and no<br />

residual joins<br />

Match with the largest reduction ratio:<br />

|T1|*...*|Tn|/|M|, where T1, ..., Tn are<br />

base tables in F<br />

Predicate<br />

matching<br />

Grouping<br />

matching<br />

Expression<br />

derivation<br />

Restrictions on Fullselect<br />

Base tables & table functions<br />

Single SELECT after view and table<br />

expression merge<br />

No ROWID, LOB types<br />

No outer joins<br />

.<br />

Grouping matching<br />

Functional dependency: column K determines C<br />

Matching requirement: Grouping column in F<br />

determines grouping column in Q<br />

No regrouping requirement: Grouping column in<br />

Q determines grouping column in F<br />

Expression derivation<br />

Column equivalence: T1.C1=T2.C1, T2.C1 can be used<br />

<strong>for</strong> T2.C1<br />

Arithmetic expressions: C*(A+B) can be derived from<br />

B+A and C<br />

Scalar functions: substr(C, 5,10) can be derived from C<br />

Set functions:<br />

AVG(C) = SUM(C)/COUNT(*) if C NOT NULL<br />

VAR(C) = ...<br />

Rejoin table columns: From key column T.K derive<br />

non-key column T.C<br />

If there are any predicates that are in the materialized query table subselect, but are not in the<br />

query, then it can be assumed that these predicates may have resulted in discarded rows as<br />

the materialized query table was refreshed, and that any rewritten query that makes use of<br />

the materialized query table does not give the correct results.<br />

The GROUP BY clauses, if any, are compared during grouping matching, to determine if a<br />

query GROUP BY clause results in a subset (not necessarily proper) of the rows that are in<br />

Chapter 3. SQL per<strong>for</strong>mance 43

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!