95 lines
6.0 KiB
HTML
95 lines
6.0 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="Ordering optimization" />
|
||
|
<meta name="abstract" content="This section describes how DB2 Universal Database for iSeries implements ordering techniques, and how optimization choices are made by the query optimizer. The query optimizer can use either index ordering or a sort to implement ordering." />
|
||
|
<meta name="description" content="This section describes how DB2 Universal Database for iSeries implements ordering techniques, and how optimization choices are made by the query optimizer. The query optimizer can use either index ordering or a sort to implement ordering." />
|
||
|
<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="orderingopt" />
|
||
|
<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>Ordering optimization</title>
|
||
|
</head>
|
||
|
<body id="orderingopt"><a name="orderingopt"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Ordering optimization</h1>
|
||
|
<div><p>This section describes how <span class="keyword">DB2 Universal Database™ for iSeries™</span> implements
|
||
|
ordering techniques, and how optimization choices are made by the query optimizer.
|
||
|
The query optimizer can use either index ordering or a sort to implement ordering.</p>
|
||
|
<div class="section"><h4 class="sectiontitle">Sort Ordering implementation</h4></div>
|
||
|
<div class="section"><p>The sort algorithm reads the rows into a sort space and sorts
|
||
|
the rows based on the specified ordering keys. The rows are then returned
|
||
|
to the user from the ordered sort space.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Index Ordering implementation</h4></div>
|
||
|
<div class="section"><p>The index ordering implementation requires an index that contains
|
||
|
all of the ordering columns as contiguous leftmost key columns. The database
|
||
|
manager accesses the individual rows through the index in index order, which
|
||
|
results in the rows being returned in order to the requester.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>This implementation can be beneficial if an application does not
|
||
|
need to retrieve all of the ordered results, or if an index already exists
|
||
|
that matches the ordering columns. When the ordering is implemented with an
|
||
|
index, and a permanent index does not already exist that satisfies ordering
|
||
|
columns, a temporary index is created. The ordering columns specified within
|
||
|
the query are used as the key columns for this index.</p>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Optimizing ordering by eliminating ordering columns</h4></div>
|
||
|
<div class="section"><p>All of the ordering columns are evaluated to determine if they
|
||
|
can be removed from the list of ordering columns. Only those ordering columns
|
||
|
that have isolatable selection predicates with an equal operator specified
|
||
|
can be considered. This guarantees that the column can match only a single
|
||
|
value, and will not help determine in the order.</p>
|
||
|
</div>
|
||
|
<div class="section"><div class="p">This processing is done to allow the optimizer to consider more
|
||
|
indexes as it implements the query, and to reduce the number of columns that
|
||
|
will be added as key columns to a temporary index. The following SQL example
|
||
|
illustrates a query where the optimizer might eliminate an ordering column. <pre><strong>DECLARE</strong> DEPTEMP <strong>CURSOR FOR</strong>
|
||
|
<strong>SELECT</strong> EMPNO, LASTNAME, WORKDEPT
|
||
|
<strong>FROM</strong> CORPDATA.EMPLOYEE
|
||
|
<strong>WHERE</strong> EMPNO = '000190'
|
||
|
<strong>ORDER BY</strong> EMPNO, LASTNAME, WORKDEPT</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><h4 class="sectiontitle">Optimizing ordering by adding additional ordering columns</h4></div>
|
||
|
<div class="section"><p>The same logic that is applied to removing ordering columns can
|
||
|
also be used to add additional grouping columns to the query. This is done
|
||
|
only when you are trying to determine if an index can be used to implement
|
||
|
the ordering.</p>
|
||
|
</div>
|
||
|
<div class="section"><div class="p">The following example illustrates a query where the optimizer
|
||
|
might add an additional ordering column. <pre><strong>CREATE INDEX</strong> X1 <strong>ON</strong> EMPLOYEE (LASTNAME, EMPNO, WORKDEPT)
|
||
|
|
||
|
<strong>DECLARE</strong> DEPTEMP <strong>CURSOR FOR</strong>
|
||
|
<strong>SELECT</strong> LASTNAME, WORKDEPT
|
||
|
<strong>FROM</strong> CORPDATA.EMPLOYEE
|
||
|
<strong>WHERE</strong> EMPNO = '000190'
|
||
|
<strong>ORDER BY</strong> LASTNAME, WORKDEPT</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><p>For this query request, the optimizer can add EMPNO as an additional
|
||
|
ordering column when considering X1 for the query.</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>
|
||
|
</body>
|
||
|
</html>
|