Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > 9b6cc37ce608401d44f6535a0c7cb777 > files > 377

postgresql11-docs-11.5-1.mga7.noarch.rpm

<?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.2. Index Types</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-intro.html" title="11.1. Introduction" /><link rel="next" href="indexes-multicolumn.html" title="11.3. Multicolumn Indexes" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">11.2. Index Types</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="indexes-intro.html" title="11.1. Introduction">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 11.5 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="indexes-multicolumn.html" title="11.3. Multicolumn Indexes">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="INDEXES-TYPES"><div class="titlepage"><div><div><h2 class="title" style="clear: both">11.2. Index Types</h2></div></div></div><p>
   <span class="productname">PostgreSQL</span> provides several index types:
   B-tree, Hash, GiST, SP-GiST, GIN and BRIN.
   Each index type uses a different
   algorithm that is best suited to different types of queries.
   By default, the <code class="command">CREATE INDEX</code> command creates
   B-tree indexes, which fit the most common situations.
  </p><p>
   <a id="id-1.5.10.5.3.1" class="indexterm"></a>
   <a id="id-1.5.10.5.3.2" class="indexterm"></a>
   B-trees can handle equality and range queries on data that can be sorted
   into some ordering.
   In particular, the <span class="productname">PostgreSQL</span> query planner
   will consider using a B-tree index whenever an indexed column is
   involved in a comparison using one of these operators:

   </p><table border="0" summary="Simple list" class="simplelist"><tr><td><code class="literal">&lt;</code></td></tr><tr><td><code class="literal">&lt;=</code></td></tr><tr><td><code class="literal">=</code></td></tr><tr><td><code class="literal">&gt;=</code></td></tr><tr><td><code class="literal">&gt;</code></td></tr></table><p>

   Constructs equivalent to combinations of these operators, such as
   <code class="literal">BETWEEN</code> and <code class="literal">IN</code>, can also be implemented with
   a B-tree index search.  Also, an <code class="literal">IS NULL</code> or <code class="literal">IS NOT
   NULL</code> condition on an index column can be used with a B-tree index.
  </p><p>
   The optimizer can also use a B-tree index for queries involving the
   pattern matching operators <code class="literal">LIKE</code> and <code class="literal">~</code>
   <span class="emphasis"><em>if</em></span> the pattern is a constant and is anchored to
   the beginning of the string — for example, <code class="literal">col LIKE
   'foo%'</code> or <code class="literal">col ~ '^foo'</code>, but not
   <code class="literal">col LIKE '%bar'</code>. However, if your database does not
   use the C locale you will need to create the index with a special
   operator class to support indexing of pattern-matching queries; see
   <a class="xref" href="indexes-opclass.html" title="11.10. Operator Classes and Operator Families">Section 11.10</a> below. It is also possible to use
   B-tree indexes for <code class="literal">ILIKE</code> and
   <code class="literal">~*</code>, but only if the pattern starts with
   non-alphabetic characters, i.e., characters that are not affected by
   upper/lower case conversion.
  </p><p>
   B-tree indexes can also be used to retrieve data in sorted order.
   This is not always faster than a simple scan and sort, but it is
   often helpful.
  </p><p>
   <a id="id-1.5.10.5.6.1" class="indexterm"></a>
   <a id="id-1.5.10.5.6.2" class="indexterm"></a>
   Hash indexes can only handle simple equality comparisons.
   The query planner will consider using a hash index whenever an
   indexed column is involved in a comparison using the
   <code class="literal">=</code> operator.
   The following command is used to create a hash index:
</p><pre class="synopsis">
CREATE INDEX <em class="replaceable"><code>name</code></em> ON <em class="replaceable"><code>table</code></em> USING HASH (<em class="replaceable"><code>column</code></em>);
</pre><p>
  </p><p>
   <a id="id-1.5.10.5.7.1" class="indexterm"></a>
   <a id="id-1.5.10.5.7.2" class="indexterm"></a>
   GiST indexes are not a single kind of index, but rather an infrastructure
   within which many different indexing strategies can be implemented.
   Accordingly, the particular operators with which a GiST index can be
   used vary depending on the indexing strategy (the <em class="firstterm">operator
   class</em>).  As an example, the standard distribution of
   <span class="productname">PostgreSQL</span> includes GiST operator classes
   for several two-dimensional geometric data types, which support indexed
   queries using these operators:

   </p><table border="0" summary="Simple list" class="simplelist"><tr><td><code class="literal">&lt;&lt;</code></td></tr><tr><td><code class="literal">&amp;&lt;</code></td></tr><tr><td><code class="literal">&amp;&gt;</code></td></tr><tr><td><code class="literal">&gt;&gt;</code></td></tr><tr><td><code class="literal">&lt;&lt;|</code></td></tr><tr><td><code class="literal">&amp;&lt;|</code></td></tr><tr><td><code class="literal">|&amp;&gt;</code></td></tr><tr><td><code class="literal">|&gt;&gt;</code></td></tr><tr><td><code class="literal">@&gt;</code></td></tr><tr><td><code class="literal">&lt;@</code></td></tr><tr><td><code class="literal">~=</code></td></tr><tr><td><code class="literal">&amp;&amp;</code></td></tr></table><p>

   (See <a class="xref" href="functions-geometry.html" title="9.11. Geometric Functions and Operators">Section 9.11</a> for the meaning of
   these operators.)
   The GiST operator classes included in the standard distribution are
   documented in <a class="xref" href="gist-builtin-opclasses.html#GIST-BUILTIN-OPCLASSES-TABLE" title="Table 64.1. Built-in GiST Operator Classes">Table 64.1</a>.
   Many other GiST operator
   classes are available in the <code class="literal">contrib</code> collection or as separate
   projects.  For more information see <a class="xref" href="gist.html" title="Chapter 64. GiST Indexes">Chapter 64</a>.
  </p><p>
   GiST indexes are also capable of optimizing <span class="quote">“<span class="quote">nearest-neighbor</span>”</span>
   searches, such as
</p><pre class="programlisting">
SELECT * FROM places ORDER BY location &lt;-&gt; point '(101,456)' LIMIT 10;

</pre><p>
   which finds the ten places closest to a given target point.  The ability
   to do this is again dependent on the particular operator class being used.
   In <a class="xref" href="gist-builtin-opclasses.html#GIST-BUILTIN-OPCLASSES-TABLE" title="Table 64.1. Built-in GiST Operator Classes">Table 64.1</a>, operators that can be
   used in this way are listed in the column <span class="quote">“<span class="quote">Ordering Operators</span>”</span>.
  </p><p>
   <a id="id-1.5.10.5.9.1" class="indexterm"></a>
   <a id="id-1.5.10.5.9.2" class="indexterm"></a>
   SP-GiST indexes, like GiST indexes, offer an infrastructure that supports
   various kinds of searches.  SP-GiST permits implementation of a wide range
   of different non-balanced disk-based data structures, such as quadtrees,
   k-d trees, and radix trees (tries).  As an example, the standard distribution of
   <span class="productname">PostgreSQL</span> includes SP-GiST operator classes
   for two-dimensional points, which support indexed
   queries using these operators:

   </p><table border="0" summary="Simple list" class="simplelist"><tr><td><code class="literal">&lt;&lt;</code></td></tr><tr><td><code class="literal">&gt;&gt;</code></td></tr><tr><td><code class="literal">~=</code></td></tr><tr><td><code class="literal">&lt;@</code></td></tr><tr><td><code class="literal">&lt;^</code></td></tr><tr><td><code class="literal">&gt;^</code></td></tr></table><p>

   (See <a class="xref" href="functions-geometry.html" title="9.11. Geometric Functions and Operators">Section 9.11</a> for the meaning of
   these operators.)
   The SP-GiST operator classes included in the standard distribution are
   documented in <a class="xref" href="spgist-builtin-opclasses.html#SPGIST-BUILTIN-OPCLASSES-TABLE" title="Table 65.1. Built-in SP-GiST Operator Classes">Table 65.1</a>.
   For more information see <a class="xref" href="spgist.html" title="Chapter 65. SP-GiST Indexes">Chapter 65</a>.
  </p><p>
   <a id="id-1.5.10.5.10.1" class="indexterm"></a>
   <a id="id-1.5.10.5.10.2" class="indexterm"></a>
   GIN indexes are <span class="quote">“<span class="quote">inverted indexes</span>”</span> which are appropriate for
   data values that contain multiple component values, such as arrays.  An
   inverted index contains a separate entry for each component value, and
   can efficiently handle queries that test for the presence of specific
   component values.
  </p><p>
   Like GiST and SP-GiST, GIN can support
   many different user-defined indexing strategies, and the particular
   operators with which a GIN index can be used vary depending on the
   indexing strategy.
   As an example, the standard distribution of
   <span class="productname">PostgreSQL</span> includes a GIN operator class
   for arrays, which supports indexed queries using these operators:

   </p><table border="0" summary="Simple list" class="simplelist"><tr><td><code class="literal">&lt;@</code></td></tr><tr><td><code class="literal">@&gt;</code></td></tr><tr><td><code class="literal">=</code></td></tr><tr><td><code class="literal">&amp;&amp;</code></td></tr></table><p>

   (See <a class="xref" href="functions-array.html" title="9.18. Array Functions and Operators">Section 9.18</a> for the meaning of
   these operators.)
   The GIN operator classes included in the standard distribution are
   documented in <a class="xref" href="gin-builtin-opclasses.html#GIN-BUILTIN-OPCLASSES-TABLE" title="Table 66.1. Built-in GIN Operator Classes">Table 66.1</a>.
   Many other GIN operator
   classes are available in the <code class="literal">contrib</code> collection or as separate
   projects.  For more information see <a class="xref" href="gin.html" title="Chapter 66. GIN Indexes">Chapter 66</a>.
  </p><p>
   <a id="id-1.5.10.5.12.1" class="indexterm"></a>
   <a id="id-1.5.10.5.12.2" class="indexterm"></a>
   BRIN indexes (a shorthand for Block Range INdexes) store summaries about
   the values stored in consecutive physical block ranges of a table.
   Like GiST, SP-GiST and GIN,
   BRIN can support many different indexing strategies,
   and the particular operators with which a BRIN index can be used
   vary depending on the indexing strategy.
   For data types that have a linear sort order, the indexed data
   corresponds to the minimum and maximum values of the
   values in the column for each block range.  This supports indexed queries
   using these operators:

   </p><table border="0" summary="Simple list" class="simplelist"><tr><td><code class="literal">&lt;</code></td></tr><tr><td><code class="literal">&lt;=</code></td></tr><tr><td><code class="literal">=</code></td></tr><tr><td><code class="literal">&gt;=</code></td></tr><tr><td><code class="literal">&gt;</code></td></tr></table><p>

   The BRIN operator classes included in the standard distribution are
   documented in <a class="xref" href="brin-builtin-opclasses.html#BRIN-BUILTIN-OPCLASSES-TABLE" title="Table 67.1. Built-in BRIN Operator Classes">Table 67.1</a>.
   For more information see <a class="xref" href="brin.html" title="Chapter 67. BRIN Indexes">Chapter 67</a>.
  </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="indexes-intro.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="indexes.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="indexes-multicolumn.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">11.1. Introduction </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 11.3. Multicolumn Indexes</td></tr></table></div></body></html>