Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5b68dfb

Browse files
committed
Add some discussion of sort ordering to indices.sgml, which curiously
had never touched the subject before.
1 parent2f8ee82 commit5b68dfb

File tree

1 file changed

+88
-3
lines changed

1 file changed

+88
-3
lines changed

‎doc/src/sgml/indices.sgml

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/indices.sgml,v 1.69 2007/02/01 00:28:17 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/indices.sgml,v 1.70 2007/02/14 20:47:15 tgl Exp $ -->
22

33
<chapter id="indexes">
44
<title id="indexes-title">Indexes</title>
@@ -359,6 +359,88 @@ CREATE INDEX test2_mm_idx ON test2 (major, minor);
359359
</sect1>
360360

361361

362+
<sect1 id="indexes-ordering">
363+
<title>Indexes and <literal>ORDER BY</></title>
364+
365+
<indexterm zone="indexes-ordering">
366+
<primary>index</primary>
367+
<secondary>and <literal>ORDER BY</></secondary>
368+
</indexterm>
369+
370+
<para>
371+
In addition to simply finding the rows to be returned by a query,
372+
an index may be able to deliver them in a specific sorted order.
373+
This allows a query's <literal>ORDER BY</> specification to be met
374+
without a separate sorting step. Of the index types currently
375+
supported by <productname>PostgreSQL</productname>, only B-tree
376+
can produce sorted output &mdash; the other index types return
377+
matching rows in an unspecified, implementation-dependent order.
378+
</para>
379+
380+
<para>
381+
The planner will consider satisfying an <literal>ORDER BY</> specification
382+
either by scanning any available index that matches the specification,
383+
or by scanning the table in physical order and doing an explicit
384+
sort. For a query that requires scanning a large fraction of the
385+
table, the explicit sort is likely to be faster because it requires
386+
less disk I/O due to a better-ordered access pattern. Indexes are
387+
more useful when only a few rows need be fetched. An important
388+
special case is <literal>ORDER BY</> in combination with
389+
<literal>LIMIT</> <replaceable>n</>: an explicit sort will have to process
390+
all the data to identify the first <replaceable>n</> rows, but if there is
391+
an index matching the <literal>ORDER BY</> then the first <replaceable>n</>
392+
rows can be retrieved directly, without scanning the remainder at all.
393+
</para>
394+
395+
<para>
396+
By default, B-tree indexes store their entries in ascending order
397+
with nulls last. This means that a forward scan of an index on a
398+
column <literal>x</> produces output satisfying <literal>ORDER BY x</>
399+
(or more verbosely, <literal>ORDER BY x ASC NULLS LAST</>). The
400+
index can also be scanned backward, producing output satisfying
401+
<literal>ORDER BY x DESC</>
402+
(or more verbosely, <literal>ORDER BY x DESC NULLS FIRST</>, since
403+
<literal>NULLS FIRST</> is the default for <literal>ORDER BY DESC</>).
404+
</para>
405+
406+
<para>
407+
You can adjust the ordering of a B-tree index by including the
408+
options <literal>ASC</>, <literal>DESC</>, <literal>NULLS FIRST</>,
409+
and/or <literal>NULLS LAST</> when creating the index; for example:
410+
<programlisting>
411+
CREATE INDEX test2_info_nulls_low ON test2 (info NULLS FIRST);
412+
CREATE INDEX test3_desc_index ON test3 (id DESC NULLS LAST);
413+
</programlisting>
414+
An index stored in ascending order with nulls first can satisfy
415+
either <literal>ORDER BY x ASC NULLS FIRST</> or
416+
<literal>ORDER BY x DESC NULLS LAST</> depending on which direction
417+
it is scanned in.
418+
</para>
419+
420+
<para>
421+
You might wonder why bother providing all four options, when two
422+
options together with the possibility of backward scan would cover
423+
all the variants of <literal>ORDER BY</>. In single-column indexes
424+
the options are indeed redundant, but in multicolumn indexes they can be
425+
useful. Consider a two-column index on <literal>(x, y)</>: this can
426+
satisfy <literal>ORDER BY x, y</> if we scan forward, or
427+
<literal>ORDER BY x DESC, y DESC</> if we scan backward.
428+
But it might be that the application frequently needs to use
429+
<literal>ORDER BY x ASC, y DESC</>. There is no way to get that
430+
ordering from a regular index, but it is possible if the index is defined
431+
as <literal>(x ASC, y DESC)</> or <literal>(x DESC, y ASC)</>.
432+
</para>
433+
434+
<para>
435+
Obviously, indexes with non-default sort orderings are a fairly
436+
specialized feature, but sometimes they can produce tremendous
437+
speedups for certain queries. Whether it's worth keeping such an
438+
index depends on how often you use queries that require a special
439+
sort ordering.
440+
</para>
441+
</sect1>
442+
443+
362444
<sect1 id="indexes-bitmap-scans">
363445
<title>Combining Multiple Indexes</title>
364446

@@ -798,7 +880,7 @@ CREATE UNIQUE INDEX tests_success_constraint ON tests (subject, target)
798880
An index definition can specify an <firstterm>operator
799881
class</firstterm> for each column of an index.
800882
<synopsis>
801-
CREATE INDEX <replaceable>name</replaceable> ON <replaceable>table</replaceable> (<replaceable>column</replaceable> <replaceable>opclass</replaceable> <optional>, ...</optional>);
883+
CREATE INDEX <replaceable>name</replaceable> ON <replaceable>table</replaceable> (<replaceable>column</replaceable> <replaceable>opclass</replaceable> <optional><replaceable>sort options</replaceable></optional> <optional>, ...</optional>);
802884
</synopsis>
803885
The operator class identifies the operators to be used by the index
804886
for that column. For example, a B-tree index on the type <type>int4</type>
@@ -810,7 +892,10 @@ CREATE INDEX <replaceable>name</replaceable> ON <replaceable>table</replaceable>
810892
index behavior. For example, we might want to sort a complex-number data
811893
type either by absolute value or by real part. We could do this by
812894
defining two operator classes for the data type and then selecting
813-
the proper class when making an index.
895+
the proper class when making an index. The operator class determines
896+
the basic sort ordering (which can then be modified by adding sort options
897+
<literal>ASC</>/<literal>DESC</> and/or
898+
<literal>NULLS FIRST</>/<literal>NULLS LAST</>).
814899
</para>
815900

816901
<para>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp