create_index.l 8.9 KB
Newer Older
1 2
.\" This is -*-nroff-*-
.\" XXX standard disclaimer belongs here....
3
.\" $Header: /cvsroot/pgsql/src/man/Attic/create_index.l,v 1.12 1998/08/19 02:04:12 momjian Exp $
4
.TH "CREATE INDEX" SQL 11/05/95 PostgreSQL PostgreSQL
5
.SH NAME
B
Bruce Momjian 已提交
6
create index - construct a secondary index
7 8
.SH SYNOPSIS
.nf
9
\fBcreate\fR [\fBunique\fR] \fBindex\fR index-name
10
	\fBon\fR classname [\fBusing\fR am-name]
V
Added:  
Vadim B. Mikheev 已提交
11
	\fB(\fR attname [type_class], ...\fB )\fR
12

13
\fBcreate\fR [\fBunique\fR] \fBindex\fR index-name
14 15 16 17 18 19 20 21 22 23 24
	\fBon\fR classname [\fBusing\fR am-name]
	\fB(\fR funcname \fB(\fR attname\-1 { , attname\-i } \fB)\fR type_class \fB)\fR
.fi
.SH DESCRIPTION
This command constructs an index called
.IR index-name.
.PP
.IR Am-name
is the name of the access method which is used for the index.
The default access method is btree.
.PP
25 26
In the first syntax shown above, the key fields for the index are
specified as attribute names.  It may also have an associated
27 28
.IR "operator class" .
An operator class is used to specify the operators to be used for a
29 30
particular index.
For example, a btree index on four-byte integers would use the
31 32 33 34 35 36
.IR int4_ops
class; this operator class includes comparison functions for four-byte
integers.
The default operator class is the appropriate operator class for
that field type.
.PP
V
Added:  
Vadim B. Mikheev 已提交
37 38 39 40 41
\fBNote:\fR currently, only 
.IR btree
access method supports multi-attribute indices.
Up to 7 keys may be specified.
.PP
42 43 44 45 46
In the second syntax shown above, an index can be defined on the
result of a user-defined function
.IR funcname
applied to one or more attributes of a single class.  These
.IR "functional indices"
47
can be used to obtain fast access to data based on
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
operators that would normally require some transformation to be
applied to the base data.  For example, say you have an attribute in
class \*(lqmyclass\*(rq called \*(lqpt\*(rq that consists of a 2D
point type.  Now, suppose that you would like to index this attribute
but you only have index operator classes for 2D polygon types.  You
can define an index on the point attribute using a function that you
write (call it \*(lqpoint_to_polygon\*(rq) and your existing polygon
operator class; after that, queries using existing polygon operators
that reference \*(lqpoint_to_polygon(myclass.pt)\*(rq on one side will
use the precomputed polygons stored in the functional index instead of
computing a polygon for each and every instance in \*(lqmyclass\*(rq
and then comparing it to the value on the other side of the operator.
Obviously, the decision to build a functional index represents a
tradeoff between space (for the index) and execution time.
.PP
63 64 65 66 67
The \fBunique\fR keyword causes the system to check for duplicate
values when the index is created (if data already exist) and each
time data is added.
Attempts to insert or update non-duplicate data will generate an error.
.PP
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
Postgres provides btree, rtree and hash access methods for
secondary indices.  The btree access method is an implementation of
the Lehman-Yao high-concurrency btrees.  The rtree access method
implements standard rtrees using Guttman's quadratic split algorithm.
The hash access method is an implementation of Litwin's linear
hashing.  We mention the algorithms used solely to indicate that all
of these access methods are fully dynamic and do not have to be
optimized periodically (as is the case with, for example, static hash
access methods).
.PP
This list was generated from the Postgres system catalogs with the query:

.nf
SELECT am.amname AS acc_name,
       opc.opcname AS ops_name,
       opr.oprname AS ops_comp
FROM   pg_am am, pg_amop amop, pg_opclass opc, pg_operator opr
WHERE  amop.amopid = am.oid AND
       amop.amopclaid = opc.oid AND
       amop.amopopr = opr.oid
ORDER BY acc_name, ops_name, ops_comp;

acc_name|ops_name   |ops_comp
--------+-----------+--------
btree   |abstime_ops|<       
btree   |abstime_ops|<=      
btree   |abstime_ops|=       
btree   |abstime_ops|>       
btree   |abstime_ops|>=      
btree   |bpchar_ops |<       
btree   |bpchar_ops |<=      
btree   |bpchar_ops |=       
btree   |bpchar_ops |>       
btree   |bpchar_ops |>=      
btree   |char_ops   |<       
btree   |char_ops   |<=      
btree   |char_ops   |=       
btree   |char_ops   |>       
btree   |char_ops   |>=      
btree   |date_ops   |<       
btree   |date_ops   |<=      
btree   |date_ops   |=       
btree   |date_ops   |>       
btree   |date_ops   |>=      
btree   |float4_ops |<       
btree   |float4_ops |<=      
btree   |float4_ops |=       
btree   |float4_ops |>       
btree   |float4_ops |>=      
btree   |float8_ops |<       
btree   |float8_ops |<=      
btree   |float8_ops |=       
btree   |float8_ops |>       
btree   |float8_ops |>=      
btree   |int24_ops  |<       
btree   |int24_ops  |<=      
btree   |int24_ops  |=       
btree   |int24_ops  |>       
btree   |int24_ops  |>=      
btree   |int2_ops   |<       
btree   |int2_ops   |<=      
btree   |int2_ops   |=       
btree   |int2_ops   |>       
btree   |int2_ops   |>=      
btree   |int42_ops  |<       
btree   |int42_ops  |<=      
btree   |int42_ops  |=       
btree   |int42_ops  |>       
btree   |int42_ops  |>=      
btree   |int4_ops   |<       
btree   |int4_ops   |<=      
btree   |int4_ops   |=       
btree   |int4_ops   |>       
btree   |int4_ops   |>=      
btree   |name_ops   |<       
btree   |name_ops   |<=      
btree   |name_ops   |=       
btree   |name_ops   |>       
btree   |name_ops   |>=      
btree   |oid_ops    |<       
btree   |oid_ops    |<=      
btree   |oid_ops    |=       
btree   |oid_ops    |>       
btree   |oid_ops    |>=      
btree   |text_ops   |<       
btree   |text_ops   |<=      
btree   |text_ops   |=       
btree   |text_ops   |>       
btree   |text_ops   |>=      
btree   |time_ops   |<       
btree   |time_ops   |<=      
btree   |time_ops   |=       
btree   |time_ops   |>       
btree   |time_ops   |>=      
btree   |varchar_ops|<       
btree   |varchar_ops|<=      
btree   |varchar_ops|=       
btree   |varchar_ops|>       
btree   |varchar_ops|>=      
hash    |bpchar_ops |=       
hash    |char_ops   |=       
hash    |date_ops   |=       
hash    |float4_ops |=       
hash    |float8_ops |=       
hash    |int2_ops   |=       
hash    |int4_ops   |=       
hash    |name_ops   |=       
hash    |oid_ops    |=       
hash    |text_ops   |=       
hash    |time_ops   |=       
hash    |varchar_ops|=       
rtree   |bigbox_ops |&&      
rtree   |bigbox_ops |&<      
rtree   |bigbox_ops |&>      
rtree   |bigbox_ops |<<      
rtree   |bigbox_ops |>>      
rtree   |bigbox_ops |@       
rtree   |bigbox_ops |~       
rtree   |bigbox_ops |~=      
rtree   |box_ops    |&&      
rtree   |box_ops    |&<      
rtree   |box_ops    |&>      
rtree   |box_ops    |<<      
rtree   |box_ops    |>>      
rtree   |box_ops    |@       
rtree   |box_ops    |~       
rtree   |box_ops    |~=      
rtree   |poly_ops   |&&      
rtree   |poly_ops   |&<      
rtree   |poly_ops   |&>      
rtree   |poly_ops   |<<      
rtree   |poly_ops   |>>      
rtree   |poly_ops   |@       
rtree   |poly_ops   |~       
rtree   |poly_ops   |~=      

.fi
The
.IR int24_ops
operator class is useful for constructing indices on int2 data, and
doing comparisons against int4 data in query qualifications.
Similarly,
.IR int42_ops
support indices on int4 data that is to be compared against int2 data
in queries.
.PP
.PP
The Postgres query optimizer will consider using btree indices in a scan
whenever an indexed attribute is involved in a comparison using one of:

.nf
<    <=    =    >=    >
.fi

Both box classes support indices on the \*(lqbox\*(rq datatype in
Postgres.  The difference between them is that
.IR bigbox_ops
scales box coordinates down, to avoid floating point exceptions from
doing multiplication, addition, and subtraction on very large
floating-point coordinates.  If the field on which your rectangles lie
is about 20,000 units square or larger, you should use
.IR bigbox_ops .
The
.IR poly_ops
operator class supports rtree indices on \*(lqpolygon\*(rq data.
.PP
The Postgres query optimizer will consider using an rtree index whenever
an indexed attribute is involved in a comparison using one of:

.nf
<<    &<    &>    >>    @    ~=    &&
.fi

The Postgres query optimizer will consider using a hash index whenever
an indexed attribute is involved in a comparison using the \fB=\fR operator.
.SH EXAMPLES
.nf
--
--Create a btree index on the emp class using the age attribute.
--
create index empindex on emp using btree (age int4_ops)
.fi
.nf
--
--Create a btree index on employee name.
--
create index empname
255
	on emp using btree (name name_ops)
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
.fi
.nf
--
--Create an rtree index on the bounding rectangle of cities.
--
create index cityrect
	on city using rtree (boundbox box_ops)
.fi
.nf
--
--Create a rtree index on a point attribute such that we
--can efficiently use box operators on the result of the 
--conversion function.  Such a qualification might look 
--like "where point2box(points.pointloc) = boxes.box".
--
create index pointloc
	on points using rtree (point2box(location) box_ops)
.nf