ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzajq_5.4.0.1/rzajqdistinct.htm

78 lines
4.2 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="Distinct optimization" />
<meta name="abstract" content="Distinct is used to compare a value with another value." />
<meta name="description" content="Distinct is used to compare a value with another value." />
<meta name="DC.Relation" scheme="URI" content="per0001.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="rzajqdistinct" />
<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>Distinct optimization</title>
</head>
<body id="rzajqdistinct"><a name="rzajqdistinct"><!-- --></a>
<img src="./delta.gif" alt="Start of change" /><!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Distinct optimization</h1>
<div><p>Distinct is used to compare a value with another value.</p>
<div class="section"><p>There are two methods to write a query that returns distinct values
in SQL. One method uses the DISTINCT keyword:</p>
<pre><strong>SELECT DISTINCT</strong> COL1, COL2
<strong>FROM</strong> TABLE1</pre>
<p>The second method uses GROUP BY:</p>
<pre><strong>SELECT</strong> COL1, COL2
<strong>FROM</strong> TABLE1
<strong>GROUP BY</strong> COL1, COL2</pre>
<p>All queries that contain a DISTINCT,
and are run using SQE, will be rewritten into queries using GROUP BY. This
rewrite enables queries using DISTINCT to take advantage of the many grouping
techniques available to the optimizer.</p>
</div>
<div class="section"><h4 class="sectiontitle">Distinct to Grouping implementation</h4><p>Below is an
example of a query with a DISTINCT:</p>
<pre><strong>SELECT</strong> DISTINCT COL1, COL2
<strong>FROM</strong> T1
<strong>WHERE</strong> COL2 &gt; 5 AND COL3 = 2</pre>
<p>The optimizer will rewrite
it into this query:</p>
<pre><strong>SELECT</strong> COL1, COL2
<strong>FROM</strong> T1
<strong>WHERE</strong> COL2 &gt; 5 AND COL3 = 2
<strong>GROUP BY</strong> COL1, COL2</pre>
</div>
<div class="section"><h4 class="sectiontitle">Distinct removal</h4><p>A query containing a DISTINCT over
whole-file aggregation (no grouping or selection) allows the DISTINCT to be
removed. For example, look at this query with DISTINCT:</p>
<pre><strong>SELECT DISTINCT COUNT</strong>(C1), <strong>SUM</strong>(C1)
<strong>FROM</strong> TABLE1</pre>
<p>The optimizer rewrites this query as the
following:</p>
<pre><strong>SELECT COUNT</strong>(C1), <strong>SUM</strong>(C1)
<strong>FROM</strong> TABLE1</pre>
<p>If the DISTINCT and the GROUP BY fields
are identical, the DISTINCT can be removed. If the DISTINCT fields are
a subset of the GROUP BY fields (and there are no aggregates), the DISTINCTs
can be removed.</p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="per0001.htm" title="This overview of the query optimizer provides guidelines for designing queries that will perform and will use server resources more efficiently.">Processing queries: Overview</a></div>
</div>
</div>
<img src="./deltaend.gif" alt="End of change" /></body>
</html>