12.07.2015 Views

DB2 data manipulation

DB2 data manipulation

DB2 data manipulation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

developerWorks®ibm.com/developerWorksGROUP BY WORKDEPTHAVING MAX(SALARY) < (SELECT AVG(SALARY)FROM EMPLOYEEWHERE NOT WORKDEPT = EMP_COR.WORKDEPT)Work with MQTsThe definition of an MQT is based upon the result of a query. MQTs can significantlyimprove the performance of queries. This tutorial introduces you to MQTs, summarytables, and staging tables, and shows you, by way of working examples, how to getup and running with materialized query tables.An MQT is a table whose definition is based upon the result of a query. The <strong>data</strong>that is contained in an MQT is derived from one or more tables on which the MQTdefinition is based. Summary tables (or automatic summary tables [ASTs]), whichare familiar to IBM <strong>DB2</strong> for Linux, UNIX, and Windows users, are considered to be aspecialized type of MQT. The fullselect that is part of the definition of a summarytable contains a GROUP BY clause summarizing <strong>data</strong> from the tables that arereferenced in the fullselect.You can think of an MQT as a kind of materialized view. Both views and MQTs aredefined on the basis of a query. The query on which a view is based is run wheneverthe view is referenced. However, an MQT actually stores the query results as <strong>data</strong>,and you can work with the <strong>data</strong> that is in the MQT instead of the <strong>data</strong> that is in theunderlying tables. MQTs can significantly improve the performance of queries,especially complex queries. If the optimizer determines that a query or part of aquery could be resolved using an MQT, the query might be rewritten to takeadvantage of the MQT. An MQT can be defined at table creation time as maintainedby the system or maintained by the user.The <strong>data</strong> in this type of materialized query table is maintained by the system. Whenyou create this type of MQT, you can specify whether the table <strong>data</strong> is a REFRESHIMMEDIATE or REFRESH DEFERRED. The REFRESH keyword lets you specifyhow the <strong>data</strong> is to be maintained. DEFERRED means that the <strong>data</strong> in the table canbe refreshed at any time using the REFRESH TABLE statement. Neither REFRESHDEFERRED nor REFRESH IMMEDIATE system-maintained MQTs allow insert,update, or delete operations to be executed against them. However, REFRESHIMMEDIATE system-maintained MQTs are updated with changes made to theunderlying tables as a result of insert, update, or delete operations.Following section shows an example of creating a REFRESH IMMEDIATEsystem-maintained MQT. The table, which is named EMP, is based on theunderlying tables EMPLOYEE and DEPARTMENT in the SAMPLE <strong>data</strong>base.Because REFRESH IMMEDIATE MQTs require that at least one unique key fromeach table referenced in the query appear in the select list, you first define a uniqueconstraint on the EMPNO column in the EMPLOYEE table and on the DEPTNOcolumn in the DEPARTMENT table. The DATA INITIALLY DEFERRED clausesimply means that <strong>data</strong> is not inserted into the table as part of the CREATE TABLEstatement. After being created, the MQT is in check pending state and cannot be<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 6 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!