]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/create_index.sgml
93ae0c1c8165e698d295e3c3306997bf50c37ec6
[postgresql] / doc / src / sgml / ref / create_index.sgml
1 <!--
2 $PostgreSQL: pgsql/doc/src/sgml/ref/create_index.sgml,v 1.48 2004/06/18 06:13:05 tgl Exp $
3 PostgreSQL documentation
4 -->
5
6 <refentry id="SQL-CREATEINDEX">
7  <refmeta>
8   <refentrytitle id="sql-createindex-title">CREATE INDEX</refentrytitle>
9   <refmiscinfo>SQL - Language Statements</refmiscinfo>
10  </refmeta>
11
12  <refnamediv>
13   <refname>CREATE INDEX</refname>
14   <refpurpose>define a new index</refpurpose>
15  </refnamediv>
16
17  <indexterm zone="sql-createindex">
18   <primary>CREATE INDEX</primary>
19  </indexterm>
20
21  <refsynopsisdiv>
22 <synopsis>
23 CREATE [ UNIQUE ] INDEX <replaceable class="parameter">name</replaceable> ON <replaceable class="parameter">table</replaceable> [ USING <replaceable class="parameter">method</replaceable> ]
24     ( { <replaceable class="parameter">column</replaceable> | ( <replaceable class="parameter">expression</replaceable> ) } [ <replaceable class="parameter">opclass</replaceable> ] [, ...] )
25     [ TABLESPACE <replaceable class="parameter">tablespace</replaceable> ]
26     [ WHERE <replaceable class="parameter">predicate</replaceable> ]
27 </synopsis>
28  </refsynopsisdiv>
29
30  <refsect1>
31   <title>Description</title>
32
33   <para>
34    <command>CREATE INDEX</command> constructs an index <replaceable
35    class="parameter">index_name</replaceable> on the specified table.
36    Indexes are primarily used to enhance database performance (though
37    inappropriate use will result in slower performance).
38   </para>
39
40   <para>
41    The key field(s) for the index are specified as column names,
42    or alternatively as expressions written in parentheses.
43    Multiple fields can be specified if the index method supports
44    multicolumn indexes.
45   </para>
46
47   <para>
48    An index field can be an expression computed from the values of
49    one or more columns of the table row.  This feature can be used
50    to obtain fast access to data based on some transformation of
51    the basic data. For example, an index computed on
52    <literal>upper(col)</> would allow the clause
53    <literal>WHERE upper(col) = 'JIM'</> to use an index.
54   </para>
55
56   <para>
57    <productname>PostgreSQL</productname> provides the index methods
58    B-tree, R-tree, hash, and GiST. The B-tree index method is an
59    implementation of Lehman-Yao high-concurrency B-trees. The R-tree
60    index method implements standard R-trees using Guttman's quadratic
61    split algorithm. The hash index method is an implementation of
62    Litwin's linear hashing.  Users can also define their own index
63    methods, but that is fairly complicated.
64   </para>
65
66   <para>
67     When the <literal>WHERE</literal> clause is present, a
68     <firstterm>partial index</firstterm> is created.
69     A partial index is an index that contains entries for only a portion of
70     a table, usually a portion that is more useful for indexing than the
71     rest of the table. For example, if you have a table that contains both
72     billed and unbilled orders where the unbilled orders take up a small
73     fraction of the total table and yet that is an often used section, you
74     can improve performance by creating an index on just that portion.
75     Another possible application is to use <literal>WHERE</literal> with
76     <literal>UNIQUE</literal> to enforce uniqueness over a subset of a
77     table.
78   </para>
79
80   <para>
81    Indexes are not used for <literal>IS NULL</> clauses by default.
82    The best way to use indexes in such cases is to create a partial index
83    using an <literal>IS NULL</> comparison.
84   </para>
85
86   <para>
87     The expression used in the <literal>WHERE</literal> clause may refer
88     only to columns of the underlying table, but it can use all columns,
89     not just the ones being indexed.  Presently, subqueries and
90     aggregate expressions are also forbidden in <literal>WHERE</literal>.
91     The same restrictions apply to index fields that are expressions.
92   </para>
93
94   <para>
95    All functions and operators used in an index definition must be
96    <quote>immutable</>, that is, their results must depend only on
97    their arguments and never on any outside influence (such as
98    the contents of another table or the current time).  This restriction
99    ensures that the behavior of the index is well-defined.  To use a
100    user-defined function in an index expression or <literal>WHERE</literal>
101    clause, remember to mark the function immutable when you create it.
102   </para>
103  </refsect1>
104
105  <refsect1>
106   <title>Parameters</title>
107
108     <variablelist>
109      <varlistentry>
110       <term><literal>UNIQUE</literal></term>
111       <listitem>
112        <para>
113         Causes the system to check for
114         duplicate values in the table when the index is created (if data
115         already exist) and each time data is added. Attempts to
116         insert or update data which would result in duplicate entries
117         will generate an error.
118        </para>
119       </listitem>
120      </varlistentry>
121
122      <varlistentry>
123       <term><replaceable class="parameter">name</replaceable></term>
124       <listitem>
125        <para>
126         The name of the index to be created.  No schema name can be included
127         here; the index is always created in the same schema as its parent
128         table.
129        </para>
130       </listitem>
131      </varlistentry>
132
133      <varlistentry>
134       <term><replaceable class="parameter">table</replaceable></term>
135       <listitem>
136        <para>
137         The name (possibly schema-qualified) of the table to be indexed.
138        </para>
139       </listitem>
140      </varlistentry>
141
142      <varlistentry>
143       <term><replaceable class="parameter">method</replaceable></term>
144       <listitem>
145        <para>
146         The name of the method to be used for the index.  Choices are
147         <literal>btree</literal>, <literal>hash</literal>,
148         <literal>rtree</literal>, and <literal>gist</literal>.  The
149         default method is <literal>btree</literal>.
150        </para>
151       </listitem>
152      </varlistentry>
153
154      <varlistentry>
155       <term><replaceable class="parameter">column</replaceable></term>
156       <listitem>
157        <para>
158         The name of a column of the table.
159        </para>
160       </listitem>
161      </varlistentry>
162
163      <varlistentry>
164       <term><replaceable class="parameter">expression</replaceable></term>
165       <listitem>
166        <para>
167         An expression based on one or more columns of the table.  The
168         expression usually must be written with surrounding parentheses,
169         as shown in the syntax.  However, the parentheses may be omitted
170         if the expression has the form of a function call.
171        </para>
172       </listitem>
173      </varlistentry>
174
175      <varlistentry>
176       <term><replaceable class="parameter">opclass</replaceable></term>
177       <listitem>
178        <para>
179         The name of an operator class. See below for details.
180        </para>
181       </listitem>
182      </varlistentry>
183
184      <varlistentry>
185       <term><replaceable class="parameter">tablespace</replaceable></term>
186       <listitem>
187        <para>
188         The tablespace in which to create the index.  If not specified,
189         the tablespace of the parent table is used.
190        </para>
191       </listitem>
192      </varlistentry>
193
194      <varlistentry>
195       <term><replaceable class="parameter">predicate</replaceable></term>
196       <listitem>
197        <para>
198         The constraint expression for a partial index.
199        </para>
200       </listitem>
201      </varlistentry>
202
203     </variablelist>
204  </refsect1>
205
206  <refsect1>
207   <title>Notes</title>
208
209   <para>
210    See <xref linkend="indexes"> for information about when indexes can
211    be used, when they are not used, and in which particular situations
212    can be useful.
213   </para>
214
215   <para>
216    Currently, only the B-tree and GiST index methods support
217    multicolumn indexes. Up to 32 fields may be specified by default.
218    (This limit can be altered when building
219    <productname>PostgreSQL</productname>.)  Only B-tree currently
220    supports unique indexes.
221   </para>
222
223   <para>
224    An <firstterm>operator class</firstterm> can be specified for each
225    column of an index. The operator class identifies the operators to be
226    used by the index for that column. For example, a B-tree index on
227    four-byte integers would use the <literal>int4_ops</literal> class;
228    this operator class includes comparison functions for four-byte
229    integers. In practice the default operator class for the column's data
230    type is usually sufficient. The main point of having operator classes
231    is that for some data types, there could be more than one meaningful
232    ordering. For example, we might want to sort a complex-number data
233    type either by absolute value or by real part. We could do this by
234    defining two operator classes for the data type and then selecting
235    the proper class when making an index.  More information about
236    operator classes is in <xref linkend="indexes-opclass"> and in <xref
237    linkend="xindex">.
238   </para>
239
240   <para>
241    Use <xref linkend="sql-dropindex" endterm="sql-dropindex-title">
242    to remove an index.
243   </para>
244  </refsect1>
245
246  <refsect1>
247   <title>Examples</title>
248
249   <para>
250    To create a B-tree index on the column <literal>title</literal> in
251    the table <literal>films</literal>:
252 <programlisting>
253 CREATE UNIQUE INDEX title_idx ON films (title);
254 </programlisting>
255   </para>
256
257 <!--
258 <comment>
259 Is this example correct?
260 </comment>
261   <para>
262    To create a R-tree index on a point attribute so that we
263    can efficiently use box operators on the result of the
264    conversion function:
265   </para>
266   <programlisting>
267 CREATE INDEX pointloc
268     ON points USING RTREE (point2box(location) box_ops);
269 SELECT * FROM points
270     WHERE point2box(points.pointloc) = boxes.box;
271   </programlisting>
272 -->
273
274  </refsect1>
275
276  <refsect1>
277   <title>Compatibility</title>
278
279   <para>
280    <command>CREATE INDEX</command> is a
281    <productname>PostgreSQL</productname> language extension.  There
282    are no provisions for indexes in the SQL standard.
283   </para>
284  </refsect1>
285 </refentry>
286
287 <!-- Keep this comment at the end of the file
288 Local variables:
289 mode: sgml
290 sgml-omittag:nil
291 sgml-shorttag:t
292 sgml-minimize-attributes:nil
293 sgml-always-quote-attributes:t
294 sgml-indent-step:1
295 sgml-indent-data:t
296 sgml-parent-document:nil
297 sgml-default-dtd-file:"../reference.ced"
298 sgml-exposed-tags:nil
299 sgml-local-catalogs:"/usr/lib/sgml/catalog"
300 sgml-local-ecat-files:nil
301 End:
302 -->