DB2 materialized view (materialized Query Tables, Mqts)

Source: Internet
Author: User
Tags db2

DB2 materialized view Mqts are based on a table defined by the query result, and the MQT contains data from one or more tables on which the MQT definition is based, and using MQTS can significantly improve the operation performance of the query.

The views and mqts for the database are defined based on a query. The query on which the view is based will run whenever the view is referenced. However, Mqts actually store the results of the query as data, and you can use the data in the MQT instead of using the data in the underlying table.

Mqts can significantly improve the performance of queries, especially for complex queries. If the optimizer determines that a part of a query or query can be resolved with an MQT, the query can be overridden to take advantage of MQTS. MQTS can be defined when a table is created, either by system maintenance or by user maintenance.

The data initially DEFERRED clause means that when you execute a CREATE table statement, you do not insert it into the table.
After the MQT is created, it is in the check pending state and cannot be queried until the SET INTEGRITY statement is executed on it. The IMMEDIATE CHECKED clause specifies that the data must be checked against the query used to define the MQT and that the data be refreshed. The NOT incremental clause specifies an integrity check for the entire table.

The data in this MQT is maintained by the system. When creating this type of MQT, you can specify whether the table data is either refresh IMMEDIATE or refresh DEFERRED. You can specify how data is maintained by using the REFRESH keyword. DEFERRED means that the data in the table can be refreshed at any time through the Refresh table statement.

System-maintained Mqts, either the Refresh DEFERRED type or the refresh IMMEDIATE type, are not allowed for INSERT, UPDATE, or delete operations. However, for a system-maintained MQT of the REFRESH IMMEDIATE type, you can update the underlying table's changes (that is, insert, UPDATE, or delete operations).

Let's look at an example

First create the original table

CREATE TABLE T (
   ID                INTEGER not  NULL,
   COL1              VARCHAR (128),
   COL2              VARCHAR (128),
   COL3              VARCHAR (128),
   COL4              VARCHAR (128),
   COL5              VARCHAR (128),
   PRIMARY KEY (ID)
ORGANIZE by ROW;

Create an MQT table

CREATE TABLE t_mqt (ID, COL1, COL2, COL3) 
as (select ID, COL1, COL2, COL3 from  T) 
DATA initially Deferr ED REFRESH IMMEDIATE maintained by SYSTEM;
SET INTEGRITY for T_MQT IMMEDIATE CHECKED full ACCESS;


Write data to the original table

Insert into T (ID, COL1, COL2, COL3, COL4, COL5) VALUES (1, ' col1 ', ' col2 ', ' col3 ', ' col4 ', ' col5 ');
Insert into T (ID, COL1, COL2, COL3, COL4, COL5) VALUES (2, ' col1 ', ' col2 ', ' col3 ', ' col4 ', ' col5 ');

Querying the original table and the MQT table will show that there is data in the MQT list.

SELECT * from T;
SELECT * from T_MQT;

Original link: DB2 materialized view (materialized Query Tables, Mqts)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.