1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>11.4. Indexes and ORDER BY</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="indexes-multicolumn.html" title="11.3. Multicolumn Indexes" /><link rel="next" href="indexes-bitmap-scans.html" title="11.5. Combining Multiple Indexes" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">11.4. Indexes and <code class="literal">ORDER BY</code></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="indexes-multicolumn.html" title="11.3. Multicolumn Indexes">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="indexes.html" title="Chapter 11. Indexes">Up</a></td><th width="60%" align="center">Chapter 11. Indexes</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.7 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="indexes-bitmap-scans.html" title="11.5. Combining Multiple Indexes">Next</a></td></tr></table><hr /></div><div class="sect1" id="INDEXES-ORDERING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">11.4. Indexes and <code class="literal">ORDER BY</code></h2></div></div></div><a id="id-1.5.10.7.2" class="indexterm"></a><p>
In addition to simply finding the rows to be returned by a query,
an index may be able to deliver them in a specific sorted order.
This allows a query's <code class="literal">ORDER BY</code> specification to be honored
without a separate sorting step. Of the index types currently
supported by <span class="productname">PostgreSQL</span>, only B-tree
can produce sorted output — the other index types return
matching rows in an unspecified, implementation-dependent order.
</p><p>
The planner will consider satisfying an <code class="literal">ORDER BY</code> specification
either by scanning an available index that matches the specification,
or by scanning the table in physical order and doing an explicit
sort. For a query that requires scanning a large fraction of the
table, an explicit sort is likely to be faster than using an index
because it requires
less disk I/O due to following a sequential access pattern. Indexes are
more useful when only a few rows need be fetched. An important
special case is <code class="literal">ORDER BY</code> in combination with
<code class="literal">LIMIT</code> <em class="replaceable"><code>n</code></em>: an explicit sort will have to process
all the data to identify the first <em class="replaceable"><code>n</code></em> rows, but if there is
an index matching the <code class="literal">ORDER BY</code>, the first <em class="replaceable"><code>n</code></em>
rows can be retrieved directly, without scanning the remainder at all.
</p><p>
By default, B-tree indexes store their entries in ascending order
with nulls last (table TID is treated as a tiebreaker column among
otherwise equal entries). This means that a forward scan of an
index on column <code class="literal">x</code> produces output satisfying <code class="literal">ORDER BY x</code>
(or more verbosely, <code class="literal">ORDER BY x ASC NULLS LAST</code>). The
index can also be scanned backward, producing output satisfying
<code class="literal">ORDER BY x DESC</code>
(or more verbosely, <code class="literal">ORDER BY x DESC NULLS FIRST</code>, since
<code class="literal">NULLS FIRST</code> is the default for <code class="literal">ORDER BY DESC</code>).
</p><p>
You can adjust the ordering of a B-tree index by including the
options <code class="literal">ASC</code>, <code class="literal">DESC</code>, <code class="literal">NULLS FIRST</code>,
and/or <code class="literal">NULLS LAST</code> when creating the index; for example:
</p><pre class="programlisting">
CREATE INDEX test2_info_nulls_low ON test2 (info NULLS FIRST);
CREATE INDEX test3_desc_index ON test3 (id DESC NULLS LAST);
</pre><p>
An index stored in ascending order with nulls first can satisfy
either <code class="literal">ORDER BY x ASC NULLS FIRST</code> or
<code class="literal">ORDER BY x DESC NULLS LAST</code> depending on which direction
it is scanned in.
</p><p>
You might wonder why bother providing all four options, when two
options together with the possibility of backward scan would cover
all the variants of <code class="literal">ORDER BY</code>. In single-column indexes
the options are indeed redundant, but in multicolumn indexes they can be
useful. Consider a two-column index on <code class="literal">(x, y)</code>: this can
satisfy <code class="literal">ORDER BY x, y</code> if we scan forward, or
<code class="literal">ORDER BY x DESC, y DESC</code> if we scan backward.
But it might be that the application frequently needs to use
<code class="literal">ORDER BY x ASC, y DESC</code>. There is no way to get that
ordering from a plain index, but it is possible if the index is defined
as <code class="literal">(x ASC, y DESC)</code> or <code class="literal">(x DESC, y ASC)</code>.
</p><p>
Obviously, indexes with non-default sort orderings are a fairly
specialized feature, but sometimes they can produce tremendous
speedups for certain queries. Whether it's worth maintaining such an
index depends on how often you use queries that require a special
sort ordering.
</p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="indexes-multicolumn.html" title="11.3. Multicolumn Indexes">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="indexes.html" title="Chapter 11. Indexes">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="indexes-bitmap-scans.html" title="11.5. Combining Multiple Indexes">Next</a></td></tr><tr><td width="40%" align="left" valign="top">11.3. Multicolumn Indexes </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.7 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 11.5. Combining Multiple Indexes</td></tr></table></div></body></html>
|