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.

the materialized query table. If it is, then the materialized query table remains a candidate <strong>for</strong><br />

query rewrite.<br />

If the query Q contains predicates that are not identical to predicates in the materialized query<br />

table subselect, they are examined to determine if column references to base table columns<br />

can derive values from materialized query table columns instead. This takes place during the<br />

expression derivation step.<br />

If the contents of a materialized query table overlap with the contents of a query, the query<br />

and the materialized query table are said to match.<br />

If there are multiple materialized query tables that match the query but cannot be used<br />

simultaneously, heuristic rules are used to select one of them. If the query rewrite process is<br />

successful, <strong>DB2</strong> determines the cost and access path of the new query. Only if the cost of the<br />

new query is less than the cost of the original query is the new query substituted.<br />

Query rewrite example<br />

A simple example of query rewrite is depicted in Figure 3-8. If MQT1 is selected by automatic<br />

query rewrite <strong>for</strong> query RefQ, this means that:<br />

► MQT1 is enabled <strong>for</strong> query optimization by CURRENT REFRESH AGE and CURRENT<br />

MAINTAINED TABLE TYPES<br />

► MQT1 is populated as discussed be<strong>for</strong>e<br />

► Query optimization is enabled<br />

► Tables referenced in the query RefQ match (same tables) the source tables in MQT1<br />

► The predicates in MQT1 match (same predicates) with predicates in the query RefQ<br />

► The GROUP BY clause in the query RefQ is a subset (same GROUP BY) of the rows that<br />

are in MQT.<br />

Fact<br />

Store<br />

RefQ<br />

Figure 3-8 Query rewrite example<br />

44 <strong>DB2</strong> <strong>UDB</strong> <strong>for</strong> z/<strong>OS</strong> <strong>Version</strong> 8 Per<strong>for</strong>mance <strong>Topics</strong><br />

Query rewrite example<br />

prod_id time_id store_id ... ... sale_amt<br />

( reference query)<br />

select S.city_id,<br />

sum(F.sale_amt)<br />

from Fact F, Store S<br />

where F.store_id = S.store_id<br />

group by S.city_id<br />

QRW<br />

(query rewrite)<br />

store_id .... city_id city_name<br />

MQT1<br />

select A.cityid,<br />

A.sm_sale<br />

from MQT1 A<br />

select S.city_id,<br />

sum(F.sale_amt) as sm_sale<br />

from Fact F, Store S<br />

where F.store_id = S.store_id<br />

group by S.city_id

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

Saved successfully!

Ooh no, something went wrong!