Chapter 2. DB2 UDB’s materialized views 85
where fact1_subset.mesh_id=mesh.mesh_id and
fact1_subset.time_id=time.time_id and
fact1_subset.country_id=country.country_id
group by rollup(year), rollup(root_level), country_name) select *
from dt
order by country_name, year, root_level
Optimized Statement:
-------------------
SELECT Q1.COUNTRY_NAME AS "COUNTRY_NAME", Q1.COUNT AS "COUNT", Q1.YEAR AS
"YEAR", Q1.ROOT_LEVEL AS "ROOT_LEVEL"
FROM DB2ADMIN.AST6 AS Q1
WHERE (Q1.GPMONTH = 1) AND (Q1.GPFLEVEL = 1)
ORDER BY Q1.COUNTRY_NAME, Q1.YEAR, Q1.ROOT_LEVEL
Query 8:
The materialized view for this query looks like AST7 in Example 2-48. Note the
following generalization of local predicates for this query:
? The predicates on columns MESH.ROOT_LEVEL and MESH.FIRST_LEVEL
are added to the GROUP BY list, and removed from the predicates. Note that
the MESH.FIRST_LEVEL is an IN predicate.
? The COUNT(*) function in the CASE expression in the select list is replaced
by a COUNT(*), and the FACT1_SUBSET.DOC_ID column is added to the
GROUP BY list. The CASE expression is removed from the select list
altogether.
An estimate of the size of the materialized view was well within the limit of an
order of magnitude as compared to the base table. After creating this
materialized view, and populating it, we ran an EXPLAIN of Query 6 to confirm
that it was being routed to AST7 as shown in Example 2-49.
Example 2-48 Materialized view AST7
CREATE SUMMARY TABLE AST7 AS
(
SELECT ROOT_LEVEL, MESH.FIRST_LEVEL, AUTHOR_NAME, DOC_ID, COUNT(*) AS C
FROM FACT1_SUBSET, MESH, AUTHOR
WHERE FACT1_SUBSET.MESH_ID=MESH.MESH_ID AND
FACT1_SUBSET.AUTHOR_ID=AUTHOR.AUTHOR_ID
GROUP BY FIRST_LEVEL, ROOT_LEVEL, AUTHOR_NAME, DOC_ID
)
DATA INITIALLY DEFERRED REFRESH DEFERRED IN USERSPACE1