Creating Materialized Query Tables in DB2

By Tom Nonmacher

In the world of databases, sometimes we need to process complex queries that involve joining multiple tables, grouping data, or performing extensive computations. These operations can be time-consuming, especially if dealing with large volumes of data. For this reason, IBM introduced the concept of Materialized Query Tables (MQT) in DB2, a feature that also exists in other database systems such as SQL Server 2016, SQL Server 2017, MySQL 5.7, DB2 11.1, and Azure SQL.

Essentially, an MQT is a table that stores the result of a query. This means that instead of running the query every time it's needed, we can simply retrieve the data from the MQT, which can significantly enhance performance. It's important to note that the data in an MQT is static - it doesn't change when the underlying data changes. Therefore, it's crucial to refresh the MQT when necessary.

Creating an MQT in DB2 is straightforward. You use the CREATE TABLE statement, followed by the AS keyword and the query whose result you want to store. Let's consider a simple example:

CREATE TABLE sales_summary AS
(SELECT product_id, SUM(sales) AS total_sales
FROM sales
GROUP BY product_id)
DATA INITIALLY DEFERRED REFRESH DEFERRED;

In the above example, we create an MQT named sales_summary, which stores the total sales for each product. The DATA INITIALLY DEFERRED and REFRESH DEFERRED clauses indicate that the data is not loaded into the MQT when it's created and that the MQT is not automatically refreshed when the underlying data changes.

Refreshing an MQT in DB2 is done with the REFRESH TABLE statement. For example, to refresh the sales_summary MQT, you would use the following command:

REFRESH TABLE sales_summary;

The equivalent of MQTs in SQL Server 2016 and 2017 are Indexed Views. Similar to MQTs, Indexed Views store the result of a query, which can boost performance. Here's how to create an Indexed View that corresponds to our previous example:

CREATE VIEW sales_summary WITH SCHEMABINDING AS
SELECT product_id, SUM(sales) AS total_sales
FROM dbo.sales
GROUP BY product_id;

CREATE UNIQUE CLUSTERED INDEX IDX_sales_summary
ON sales_summary (product_id);

In MySQL 5.7, there's no direct equivalent of MQTs or Indexed Views. You can create a regular view with the CREATE VIEW statement, but the result is not stored - the query is run every time the view is accessed. However, you can simulate an MQT by creating a table and populating it with a query, similar to DB2.

Azure SQL, being a cloud-based version of SQL Server, supports Indexed Views, so the same approach applies as with SQL Server 2016 and 2017.

In conclusion, Materialized Query Tables in DB2 and their equivalents in other database systems can be powerful tools for improving query performance. However, they come with the trade-off of having to manage and refresh them manually, which should be considered when designing a database solution.

Check out the latest articles from all our sites:

Privacy Policy for sqlsupport.org

Last updated: Feb 03, 2026

sqlsupport.org respects your privacy and is committed to protecting any personal information you may provide while using this website.

This Privacy Policy document outlines the types of information that are collected and recorded by sqlsupport.org and how we use it.

Information We Collect

  • Internet Protocol (IP) addresses
  • Browser type and version
  • Pages visited
  • Time and date of visits
  • Referring URLs
  • Device type

Cookies and Web Beacons

sqlsupport.org uses cookies to store information about visitors preferences and to optimize the users experience.

How We Use Your Information

  • Operate and maintain our website
  • Improve user experience
  • Analyze traffic patterns
  • Prevent fraudulent activity

Contact

Email: admin@sqlsupport.org

DB2



FF61D6
Please enter the code from the image above in the box below.