2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_index.sgml,v 1.10 2000/03/16 14:39:30 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">col</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 non-duplicate data will generate an
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 which is to be used for
78 the index. The default access method is BTREE.
79 Postgres provides three access methods for secondary indexes:
86 an implementation of the 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 user-defined function, which returns a value that can
145 <refsect2 id="R2-SQL-CREATEINDEX-2">
147 <date>1998-09-09</date>
156 <term><computeroutput>
158 </computeroutput></term>
161 The message returned if the index is successfully created.
167 <term><computeroutput>
168 ERROR: Cannot create index: 'index_name' already exists.
169 </computeroutput></term>
172 This error occurs if it is impossible to create the index.
181 <refsect1 id="R1-SQL-CREATEINDEX-1">
183 <date>1998-09-09</date>
189 <command>CREATE INDEX</command> constructs an index
190 <replaceable class="parameter">index_name</replaceable>
191 on the specified <replaceable class="parameter">table</replaceable>.
195 Indexes are primarily used to enhance database performance.
196 But inappropriate use will result in slower performance.
202 In the first syntax shown above, the key fields for the
203 index are specified as column names; a column may also have
204 an associated operator class. An operator class is used
205 to specify the operators to be used for a particular
206 index. For example, a btree index on four-byte integers
207 would use the <literal>int4_ops</literal> class;
208 this operator class includes
209 comparison functions for four-byte integers. The default
210 operator class is the appropriate operator class for that
215 In the second syntax shown above, an index is defined
216 on the result of a user-defined function
217 <replaceable class="parameter">func_name</replaceable> applied
218 to one or more attributes of a single class.
219 These <firstterm>functional indices</firstterm>
220 can be used to obtain fast access to data
221 based on operators that would normally require some
222 transformation to apply them to the base data.
226 Postgres provides btree, rtree and hash access methods for
227 secondary indices. The btree access method is an implementation of
228 the Lehman-Yao high-concurrency btrees. The rtree access method
229 implements standard rtrees using Guttman's quadratic split algorithm.
230 The hash access method is an implementation of Litwin's linear
231 hashing. We mention the algorithms used solely to indicate that all
232 of these access methods are fully dynamic and do not have to be
233 optimized periodically (as is the case with, for example, static hash
237 <refsect2 id="R2-SQL-CREATEINDEX-3">
239 <date>1998-09-09</date>
246 The Postgres query optimizer will consider using btree indices in a scan
247 whenever an indexed attribute is involved in a comparison using one of:
249 <simplelist type="inline">
250 <member><</member>
251 <member><=</member>
253 <member>>=</member>
254 <member>></member>
259 Both box classes support indices on the <literal>box</literal> data
260 type in <productname>Postgres</productname>.
261 The difference between them is that <literal>bigbox_ops</literal>
262 scales box coordinates down, to avoid floating point exceptions from
263 doing multiplication, addition, and subtraction on very large
264 floating-point coordinates. If the field on which your rectangles lie
265 is about 20,000 units square or larger, you should use
266 <literal>bigbox_ops</literal>.
267 The <literal>poly_ops</literal> operator class supports rtree
268 indices on <literal>polygon</literal> data.
272 The <productname>Postgres</productname>
273 query optimizer will consider using an rtree index whenever
274 an indexed attribute is involved in a comparison using one of:
276 <simplelist type="inline">
277 <member><<</member>
278 <member>&<</member>
279 <member>&></member>
280 <member>>></member>
283 <member>&&</member>
288 The <productname>Postgres</productname>
289 query optimizer will consider using a hash index whenever
290 an indexed attribute is involved in a comparison using
291 the <literal>=</literal> operator.
295 Currently, only the BTREE access method supports multi-column
296 indexes. Up to 7 keys may be specified.
300 Use <xref linkend="sql-dropindex-title" endterm="sql-dropindex-title">
305 The <literal>int24_ops</literal>
306 operator class is useful for constructing indices on int2 data, and
307 doing comparisons against int4 data in query qualifications.
308 Similarly, <literal>int42_ops</literal>
309 support indices on int4 data that is to be compared against int2 data
314 The following select list returns all ops_names:
317 SELECT am.amname AS acc_name,
318 opc.opcname AS ops_name,
319 opr.oprname AS ops_comp
320 FROM pg_am am, pg_amop amop,
321 pg_opclass opc, pg_operator opr
322 WHERE amop.amopid = am.oid AND
323 amop.amopclaid = opc.oid AND
324 amop.amopopr = opr.oid
325 ORDER BY acc_name, ops_name, ops_comp
331 <refsect1 id="R1-SQL-CREATEINDEX-2">
335 <para>To create a btree index on the field <literal>title</literal>
336 in the table <literal>films</literal>:
339 CREATE UNIQUE INDEX title_idx
345 Is this example correct?
348 To create a rtree index on a point attribute so that we
349 can efficiently use box operators on the result of the
353 CREATE INDEX pointloc
354 ON points USING RTREE (point2box(location) box_ops);
356 WHERE point2box(points.pointloc) = boxes.box;
362 <refsect1 id="R1-SQL-CREATEINDEX-3">
367 <refsect2 id="R2-SQL-CREATEINDEX-4">
369 <date>1998-09-09</date>
375 CREATE INDEX is a <productname>Postgres</productname> language extension.
378 There is no <command>CREATE INDEX</command> command in SQL92.
384 <!-- Keep this comment at the end of the file
389 sgml-minimize-attributes:nil
390 sgml-always-quote-attributes:t
393 sgml-parent-document:nil
394 sgml-default-dtd-file:"../reference.ced"
395 sgml-exposed-tags:nil
396 sgml-local-catalogs:"/usr/lib/sgml/catalog"
397 sgml-local-ecat-files:nil