Imported upstream version 1.5
[manpages-zh.git] / raw / man7 / create_index.7
blobe0f52a6e3fc9edde463b045d6412b70eb6b46bdd
1 .\\" auto-generated by docbook2man-spec $Revision: 1.1 $
2 .TH "CREATE INDEX" "7" "2003-11-02" "SQL - Language Statements" "SQL Commands"
3 .SH NAME
4 CREATE INDEX \- define a new index
6 .SH SYNOPSIS
7 .sp
8 .nf
9 CREATE [ UNIQUE ] INDEX \fIname\fR ON \fItable\fR [ USING \fImethod\fR ]
10     ( { \fIcolumn\fR | ( \fIexpression\fR ) } [ \fIopclass\fR ] [, ...] )
11     [ WHERE \fIpredicate\fR ]
12 .sp
13 .fi
14 .SH "DESCRIPTION"
15 .PP
16 \fBCREATE INDEX\fR constructs an index \fIindex_name\fR on the specified table.
17 Indexes are primarily used to enhance database performance (though
18 inappropriate use will result in slower performance).
19 .PP
20 The key field(s) for the index are specified as column names,
21 or alternatively as expressions written in parentheses.
22 Multiple fields can be specified if the index method supports
23 multicolumn indexes.
24 .PP
25 An index field can be an expression computed from the values of
26 one or more columns of the table row. This feature can be used
27 to obtain fast access to data based on some transformation of
28 the basic data. For example, an index computed on
29 upper(col) would allow the clause
30 WHERE upper(col) = 'JIM' to use an index.
31 .PP
32 PostgreSQL provides the index methods
33 B-tree, R-tree, hash, and GiST. The B-tree index method is an
34 implementation of Lehman-Yao high-concurrency B-trees. The R-tree
35 index method implements standard R-trees using Guttman's quadratic
36 split algorithm. The hash index method is an implementation of
37 Litwin's linear hashing. Users can also define their own index
38 methods, but that is fairly complicated.
39 .PP
40 When the WHERE clause is present, a
41 \fIpartial index\fR is created.
42 A partial index is an index that contains entries for only a portion of
43 a table, usually a portion that is somehow more interesting than the
44 rest of the table. For example, if you have a table that contains both
45 billed and unbilled orders where the unbilled orders take up a small
46 fraction of the total table and yet that is an often used section, you
47 can improve performance by creating an index on just that portion.
48 Another possible application is to use WHERE with
49 UNIQUE to enforce uniqueness over a subset of a
50 table.
51 .PP
52 The expression used in the WHERE clause may refer
53 only to columns of the underlying table (but it can use all columns,
54 not only the one(s) being indexed). Presently, subqueries and
55 aggregate expressions are also forbidden in WHERE.
56 The same restrictions apply to index fields that are expressions.
57 .PP
58 All functions and operators used in an index definition must be
59 ``immutable'', that is, their results must depend only on
60 their arguments and never on any outside influence (such as
61 the contents of another table or the current time). This restriction
62 ensures that the behavior of the index is well-defined. To use a
63 user-defined function in an index expression or WHERE
64 clause, remember to mark the function immutable when you create it.
65 .SH "PARAMETERS"
66 .TP
67 \fBUNIQUE\fR
68 Causes the system to check for
69 duplicate values in the table when the index is created (if data
70 already exist) and each time data is added. Attempts to
71 insert or update data which would result in duplicate entries
72 will generate an error.
73 .TP
74 \fB\fIname\fB\fR
75 The name of the index to be created. No schema name can be included
76 here; the index is always created in the same schema as its parent
77 table.
78 .TP
79 \fB\fItable\fB\fR
80 The name (possibly schema-qualified) of the table to be indexed.
81 .TP
82 \fB\fImethod\fB\fR
83 The name of the method to be used for the index. Choices are
84 btree, hash,
85 rtree, and gist. The
86 default method is btree.
87 .TP
88 \fB\fIcolumn\fB\fR
89 The name of a column of the table.
90 .TP
91 \fB\fIexpression\fB\fR
92 An expression based on one or more columns of the table. The
93 expression usually must be written with surrounding parentheses,
94 as shown in the syntax. However, the parentheses may be omitted
95 if the expression has the form of a function call.
96 .TP
97 \fB\fIopclass\fB\fR
98 The name of an operator class. See below for details.
99 .TP
100 \fB\fIpredicate\fB\fR
101 The constraint expression for a partial index.
102 .SH "NOTES"
104 See the chapter called ``Indexes'' in the documentation for information about when indexes can
105 be used, when they are not used, and in which particular situations
106 can be useful.
108 Currently, only the B-tree and GiST index methods support
109 multicolumn indexes. Up to 32 fields may be specified by default.
110 (This limit can be altered when building
111 PostgreSQL.) Only B-tree currently
112 supports unique indexes.
114 An \fIoperator class\fR can be specified for each
115 column of an index. The operator class identifies the operators to be
116 used by the index for that column. For example, a B-tree index on
117 four-byte integers would use the int4_ops class;
118 this operator class includes comparison functions for four-byte
119 integers. In practice the default operator class for the column's data
120 type is usually sufficient. The main point of having operator classes
121 is that for some data types, there could be more than one meaningful
122 ordering. For example, we might want to sort a complex-number data
123 type either by absolute value or by real part. We could do this by
124 defining two operator classes for the data type and then selecting
125 the proper class when making an index. More information about
126 operator classes is in the sections called ``Operator Classes'' and ``Interfacing Extensions to Indexes'' in the documentation.
128 Use DROP INDEX [\fBdrop_index\fR(7)]
129 to remove an index.
130 .SH "EXAMPLES"
132 To create a B-tree index on the column title in
133 the table films:
136 CREATE UNIQUE INDEX title_idx ON films (title);
139 .SH "COMPATIBILITY"
141 \fBCREATE INDEX\fR is a
142 PostgreSQL language extension. There
143 are no provisions for indexes in the SQL standard.