102 lines
5.1 KiB
HTML
102 lines
5.1 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="MQT examples" />
|
||
|
<meta name="abstract" content="The following are examples of using MQTs." />
|
||
|
<meta name="description" content="The following are examples of using MQTs." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzajqmqt.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="rzajqmqtexample" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>MQT examples</title>
|
||
|
</head>
|
||
|
<body id="rzajqmqtexample"><a name="rzajqmqtexample"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">MQT examples</h1>
|
||
|
<div><p>The following are examples of using MQTs.</p>
|
||
|
<div class="section"><h4 class="sectiontitle">Example 1</h4><div class="p">The first example is a query that returns
|
||
|
information about employees whose job is DESIGNER. The original query looks
|
||
|
like this: <pre>Q1: SELECT D.deptname, D.location, E.firstnme, E.lastname, E.salary+E.comm+E.bonus as total_sal
|
||
|
FROM Department D, Employee E
|
||
|
WHERE D.deptno=E.workdept
|
||
|
AND E.job = 'DESIGNER'</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><div class="p">Create a table, MQT1, that uses this query: <pre>CREATE TABLE MQT1
|
||
|
AS (SELECT D.deptname, D.location, E.firstnme, E.lastname, E.salary, E.comm, E.bonus, E.job
|
||
|
FROM Department D, Employee E
|
||
|
WHERE D.deptno=E.workdept)
|
||
|
DATA INITIALLY IMMEDIATE REFRESH DEFERRED
|
||
|
ENABLE QUERY OPTIMIZATION
|
||
|
MAINTAINED BY USER</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><div class="p">Resulting new query after replacing the specified tables with
|
||
|
the MQT. <pre>SELECT M.deptname, M.location, M.firstnme, M.lastname, M.salary+M.comm+M.bonus as total_sal
|
||
|
FROM MQT1 M
|
||
|
WHERE M.job = 'DESIGNER'</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><p>In this query, the MQT matches part of the user's query. The
|
||
|
MQT is placed in the FROM clause and replaces tables DEPARTMENT and EMPLOYEE.
|
||
|
Any remaining selection not done by the MQT query (M.job= 'DESIGNER') is
|
||
|
done to remove the extra rows and the result expression, M.salary+M.comm+M.bonus,
|
||
|
is calculated. Note that JOB must be in the select-list of the MQT so that
|
||
|
the additional selection can be performed.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>Visual Explain diagram of the query when using the MQT:<br /><img src="rzajq554.gif" alt="" /><br /></p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Example 2</h4><div class="p">Get the total salary for all departments
|
||
|
that are located in 'NY'. The original query looks like this: <pre>SELECT D.deptname, sum(E.salary)
|
||
|
FROM DEPARTMENT D, EMPLOYEE E
|
||
|
WHERE D.deptno=E.workdept AND D.location = 'NY'
|
||
|
GROUP BY D.deptname</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><div class="p">Create a table, MQT2, that uses this query: <pre>CREATE TABLE MQT2
|
||
|
AS (SELECT D.deptname, D.location, sum(E.salary) as sum_sal
|
||
|
FROM DEPARTMENT D, EMPLOYEE E
|
||
|
WHERE D.deptno=E.workdept
|
||
|
GROUP BY D.Deptname, D.location)
|
||
|
DATA INITIALLY IMMEDIATE REFRESH DEFERRED
|
||
|
ENABLE QUERY OPTIMIZATION
|
||
|
MAINTAINED BY USER</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><div class="p">Resulting new query after replacing the specified tables with
|
||
|
the MQT: <pre>SELECT M.deptname, sum(M.sum_sal)
|
||
|
FROM MQT2 M
|
||
|
WHERE M.location = 'NY'
|
||
|
GROUP BY M.deptname</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><p>Since the MQT may potentially produce more groups
|
||
|
than the original query, the final resulting query must group again and SUM
|
||
|
the results to return the correct answer. Also the selection M.location='NY'
|
||
|
must be part of the new query.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>Visual Explain diagram of the query when using the MQT:<br /><img src="rzajq553.gif" alt="" /><br /></p>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzajqmqt.htm" title="Materialized query tables (MQTs) (also referred to as automatic summary tables or materialized views) can provide performance enhancements for queries.">Materialized query table optimization</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|