52 High-Function Business Intelligence in e-business
Figure 2-9 provides additional examples of matching conditions. It shows that the
GROUP BY expressions
must be derivable from the materialized view.
Figure 2-9 Matching GROUP BY and aggregate functions
3. Extra tables in the query:
DB2 is able to match user queries that contain more tables than those defined
in the materialized view, when the join predicates to the base tables can be
replaced by join predicates between the materialized view and the additional
tables.
Consider the materialized view shown in Example 2-21:
Example 2-21 Extra tables in the query materialized view
CREATE SUMMARY TABLE dba.trans_agg AS
(
SELECT ti.pgid, t.locid, t.acctid, t.status, YEAR(pdate) AS year,
MONTH(pdate) AS month, SUM(ti.amount) AS amount, COUNT(*) AS count
FROM transitem AS ti, trans AS t
WHERE ti.transid = t.transid
GROUP BY YEAR(pdate), MONTH(pdate), ti.pgid, t.locid, t.acctid, t.status
)
DATA INITIALLY DEFERRED REFRESH IMMEDIATE
A user query that looks like the one shown in Example 2-22 can be
considered matching the above materialized view even though it has an
additional location table
loc
included.
Example 2-22 Extra tables in the query — matching query
SELECT YEAR(pdate) AS year, loc.country, SUM(ti.amount) AS amount, COUNT(*)
AS count
FROM transitem AS ti, trans AS t, loc AS loc
WHERE ti.transid = t.transid AND t.locid = loc.locid
AND YEAR(pdate) BETWEEN 1990 and 1999
GROUP BY YEAR(pdate), loc.country
QUERY Materialized View
Valid ?
... GROUP BY store_id ... GROUP BY cust_id, store_id
... GROUP BY ((cust_age + 5) / 10) ... GROUP BY cust_age / 10
... CUBE(cust_id, store_id) ... GROUPING SETS (cust_id, store_id)
... GROUPING SETS (cust_id, store_id) ... CUBE(cust_id, store_id)
... AVG(sales) ... ... SUM(sales), COUNT(*) ...
... COUNT(DISTINCT cust_id) ... ... cust_id, COUNT(*) ....
Yes
No
No
No
Yes
Yes