2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_index.sgml,v 1.13 2000/05/02 20:02:03 thomas Exp $
6 <refentry id="SQL-CREATEINDEX">
8 <refentrytitle id="sql-createindex-title">
11 <refmiscinfo>SQL - Language Statements</refmiscinfo>
18 Constructs a secondary index
23 <date>1999-07-20</date>
26 CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable> ON <replaceable class="parameter">table</replaceable>
27 [ USING <replaceable class="parameter">acc_name</replaceable> ] ( <replaceable class="parameter">column</replaceable> [ <replaceable class="parameter">ops_name</replaceable> ] [, ...] )
28 CREATE [ UNIQUE ] INDEX <replaceable class="parameter">index_name</replaceable> ON <replaceable class="parameter">table</replaceable>
29 [ USING <replaceable class="parameter">acc_name</replaceable> ] ( <replaceable class="parameter">func_name</replaceable>( <replaceable class="parameter">column</replaceable> [, ... ]) [ <replaceable class="parameter">ops_name</replaceable> ] )
32 <refsect2 id="R2-SQL-CREATEINDEX-1">
34 <date>1998-09-09</date>
46 Causes the system to check for
47 duplicate values in the table when the index is created (if data
48 already exist) and each time data is added. Attempts to
49 insert or update data which would result in duplicate entries
50 will generate an error.
56 <term><replaceable class="parameter">index_name</replaceable></term>
59 The name of the index to be created.
65 <term><replaceable class="parameter">table</replaceable></term>
68 The name of the table to be indexed.
74 <term><replaceable class="parameter">acc_name</replaceable></term>
77 The name of the access method to be used for
78 the index. The default access method is BTREE.
79 Postgres provides three access methods for indexes:
86 an implementation of Lehman-Yao
87 high-concurrency btrees.
95 <para>implements standard rtrees using Guttman's
96 quadratic split algorithm.
105 an implementation of Litwin's linear hashing.
115 <term><replaceable class="parameter">column</replaceable></term>
118 The name of a column of the table.
124 <term><replaceable class="parameter">ops_name</replaceable></term>
127 An associated operator class. See below for details.
133 <term><replaceable class="parameter">func_name</replaceable></term>
136 A function, which returns a value that can be indexed.
144 <refsect2 id="R2-SQL-CREATEINDEX-2">
146 <date>1998-09-09</date>
155 <term><computeroutput>
157 </computeroutput></term>
160 The message returned if the index is successfully created.
166 <term><computeroutput>
167 ERROR: Cannot create index: 'index_name' already exists.
168 </computeroutput></term>
171 This error occurs if it is impossible to create the index.
180 <refsect1 id="R1-SQL-CREATEINDEX-1">
182 <date>1998-09-09</date>
188 <command>CREATE INDEX</command> constructs an index
189 <replaceable class="parameter">index_name</replaceable>
190 on the specified <replaceable class="parameter">table</replaceable>.
194 Indexes are primarily used to enhance database performance.
195 But inappropriate use will result in slower performance.
201 In the first syntax shown above, the key field(s) for the
202 index are specified as column names.
203 Multiple fields can be specified if the index access method supports
204 multi-column indexes.
208 In the second syntax shown above, an index is defined
209 on the result of a user-specified function
210 <replaceable class="parameter">func_name</replaceable> applied
211 to one or more attributes of a single class.
212 These <firstterm>functional indices</firstterm>
213 can be used to obtain fast access to data
214 based on operators that would normally require some
215 transformation to apply them to the base data.
219 Postgres provides btree, rtree and hash access methods for
220 indices. The btree access method is an implementation of
221 Lehman-Yao high-concurrency btrees. The rtree access method
222 implements standard rtrees using Guttman's quadratic split algorithm.
223 The hash access method is an implementation of Litwin's linear
224 hashing. We mention the algorithms used solely to indicate that all
225 of these access methods are fully dynamic and do not have to be
226 optimized periodically (as is the case with, for example, static hash
231 Use <xref linkend="sql-dropindex-title" endterm="sql-dropindex-title">
235 <refsect2 id="R2-SQL-CREATEINDEX-3">
237 <date>1998-09-09</date>
244 The <productname>Postgres</productname>
245 query optimizer will consider using a btree index whenever
246 an indexed attribute is involved in a comparison using one of:
248 <simplelist type="inline">
249 <member><</member>
250 <member><=</member>
252 <member>>=</member>
253 <member>></member>
258 The <productname>Postgres</productname>
259 query optimizer will consider using an rtree index whenever
260 an indexed attribute is involved in a comparison using one of:
262 <simplelist type="inline">
263 <member><<</member>
264 <member>&<</member>
265 <member>&></member>
266 <member>>></member>
269 <member>&&</member>
274 The <productname>Postgres</productname>
275 query optimizer will consider using a hash index whenever
276 an indexed attribute is involved in a comparison using
277 the <literal>=</literal> operator.
281 Currently, only the btree access method supports multi-column
282 indexes. Up to 16 keys may be specified by default (this limit
283 can be altered when building Postgres).
287 An <firstterm>operator class</firstterm> can be specified for each
288 column of an index. The operator class identifies the operators to
289 be used by the index for that column. For example, a btree index on
290 four-byte integers would use the <literal>int4_ops</literal> class;
291 this operator class includes comparison functions for four-byte
292 integers. In practice the default operator class for the field's
293 datatype is usually sufficient. The main point of having operator classes
294 is that for some datatypes, there could be more than one meaningful
295 ordering. For example, we might want to sort a complex-number datatype
296 either by absolute value or by real part. We could do this by defining
297 two operator classes for the datatype and then selecting the proper
298 class when making an index. There are also some operator classes with
304 The operator classes <literal>box_ops</literal> and
305 <literal>bigbox_ops</literal> both support rtree indices on the
306 <literal>box</literal> datatype.
307 The difference between them is that <literal>bigbox_ops</literal>
308 scales box coordinates down, to avoid floating point exceptions from
309 doing multiplication, addition, and subtraction on very large
310 floating-point coordinates. If the field on which your rectangles lie
311 is about 20,000 units square or larger, you should use
312 <literal>bigbox_ops</literal>.
318 The <literal>int24_ops</literal>
319 operator class is useful for constructing indices on int2 data, and
320 doing comparisons against int4 data in query qualifications.
321 Similarly, <literal>int42_ops</literal>
322 support indices on int4 data that is to be compared against int2 data
330 The following query shows all defined operator classes:
333 SELECT am.amname AS acc_name,
334 opc.opcname AS ops_name,
335 opr.oprname AS ops_comp
336 FROM pg_am am, pg_amop amop,
337 pg_opclass opc, pg_operator opr
338 WHERE amop.amopid = am.oid AND
339 amop.amopclaid = opc.oid AND
340 amop.amopopr = opr.oid
341 ORDER BY acc_name, ops_name, ops_comp
347 <refsect1 id="R1-SQL-CREATEINDEX-2">
351 <para>To create a btree index on the field <literal>title</literal>
352 in the table <literal>films</literal>:
355 CREATE UNIQUE INDEX title_idx
361 Is this example correct?
364 To create a rtree index on a point attribute so that we
365 can efficiently use box operators on the result of the
369 CREATE INDEX pointloc
370 ON points USING RTREE (point2box(location) box_ops);
372 WHERE point2box(points.pointloc) = boxes.box;
378 <refsect1 id="R1-SQL-CREATEINDEX-3">
383 <refsect2 id="R2-SQL-CREATEINDEX-4">
385 <date>1998-09-09</date>
391 CREATE INDEX is a <productname>Postgres</productname> language extension.
394 There is no <command>CREATE INDEX</command> command in SQL92.
400 <!-- Keep this comment at the end of the file
405 sgml-minimize-attributes:nil
406 sgml-always-quote-attributes:t
409 sgml-parent-document:nil
410 sgml-default-dtd-file:"../reference.ced"
411 sgml-exposed-tags:nil
412 sgml-local-catalogs:"/usr/lib/sgml/catalog"
413 sgml-local-ecat-files:nil