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.

For instance, in a data warehouse environment, data is usually extracted from other sources,<br />

trans<strong>for</strong>med, and loaded into data warehouse tables. In this case the referential integrity<br />

constraints can be maintained and en<strong>for</strong>ced by means other than the database manager to<br />

avoid the serialization and overhead of en<strong>for</strong>cing them by <strong>DB2</strong>.<br />

ALTER a base table to MQT<br />

We use the extended ALTER TABLE SQL statement to change an existing base table into an<br />

MQT. With this statement we:<br />

► Specify a fullselect associated with a table<br />

► Specify one of the mechanisms to refresh the table<br />

► Enable/disable an MQT <strong>for</strong> automatic query rewrite<br />

► Switch between system-maintained and user-maintained types<br />

See Example 3-4.<br />

Example 3-4 Alter a table to MQT<br />

ALTER TABLE TRANSCNT ADD MATERIALIZED QUERY AS (<br />

SELECT ACCTID, LOCID, YEAR, COUNT(*) AS CNT<br />

FROM TRANS<br />

GROUP BY ACCTID, LOCID, YEAR )<br />

DATA INITIALLY DEFERRED<br />

REFRESH DEFERRED<br />

MAINTAINED BY USER;<br />

3.2.2 Populating MQTs<br />

We have mentioned that an MQT can be maintained by system or user.<br />

► System-maintained MQTs (default) are populated with the new REFRESH TABLE option.<br />

This option:<br />

– Deletes all the rows in the MQT<br />

– Executes the fullselect associated with the MQT to recalculate the data<br />

– Inserts the calculated result into the MQT<br />

– Updates the catalog <strong>for</strong> the refresh timestamp and cardinality of the MQT<br />

– Cannot be updated by load, insert, update and delete<br />

You need to refresh materialized query tables periodically to maintain data currency with<br />

base tables. However, realize that refreshing materialized query tables can be an<br />

expensive process.<br />

► User-maintained MQTs can be populated by:<br />

– The REFRESH TABLE option<br />

– Load, insert, update and delete<br />

3.2.3 Controlling query rewrite<br />

Query rewrite is the process that can use an MQT if the query being optimized by <strong>DB2</strong> has<br />

common source tables and all the predicates of the fullselect that make up the MQT.<br />

<strong>DB2</strong> uses two special registers <strong>for</strong> dynamic SQL to control query rewrite:<br />

► CURRENT REFRESH AGE<br />

► CURRENT MAINTAINED TABLE TYPES<br />

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

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

Saved successfully!

Ooh no, something went wrong!