@@ -118,32 +118,39 @@ CREATE INDEX test1_id_index ON test1 (id);
118
118
B-tree, Hash, GiST, SP-GiST, GIN and BRIN.
119
119
Each index type uses a different
120
120
algorithm that is best suited to different types of queries.
121
- By default, the <command>CREATE INDEX</command> command creates
121
+ By default, the <link linkend="sql-createindex"><command>CREATE
122
+ INDEX</command></link> command creates
122
123
B-tree indexes, which fit the most common situations.
124
+ The other index types are selected by writing the keyword
125
+ <literal>USING</literal> followed by the index type name.
126
+ For example, to create a Hash index:
127
+ <programlisting>
128
+ CREATE INDEX <replaceable>name</replaceable> ON <replaceable>table</replaceable> USING HASH (<replaceable>column</replaceable>);
129
+ </programlisting>
123
130
</para>
124
131
125
- <para>
132
+ <sect2 id="indexes-types-btree">
133
+ <title>B-Tree</title>
134
+
126
135
<indexterm>
127
136
<primary>index</primary>
128
- <secondary>B-tree </secondary>
137
+ <secondary>B-Tree </secondary>
129
138
</indexterm>
130
139
<indexterm>
131
- <primary>B-tree </primary>
140
+ <primary>B-Tree </primary>
132
141
<see>index</see>
133
142
</indexterm>
143
+
144
+ <para>
134
145
B-trees can handle equality and range queries on data that can be sorted
135
146
into some ordering.
136
147
In particular, the <productname>PostgreSQL</productname> query planner
137
148
will consider using a B-tree index whenever an indexed column is
138
149
involved in a comparison using one of these operators:
139
150
140
- <simplelist>
141
- <member><literal><</literal></member>
142
- <member><literal><=</literal></member>
143
- <member><literal>=</literal></member>
144
- <member><literal>>=</literal></member>
145
- <member><literal>></literal></member>
146
- </simplelist>
151
+ <synopsis>
152
+ < <= = >= >
153
+ </synopsis>
147
154
148
155
Constructs equivalent to combinations of these operators, such as
149
156
<literal>BETWEEN</literal> and <literal>IN</literal>, can also be implemented with
@@ -172,8 +179,11 @@ CREATE INDEX test1_id_index ON test1 (id);
172
179
This is not always faster than a simple scan and sort, but it is
173
180
often helpful.
174
181
</para>
182
+ </sect2>
183
+
184
+ <sect2 id="indexes-types-hash">
185
+ <title>Hash</title>
175
186
176
- <para>
177
187
<indexterm>
178
188
<primary>index</primary>
179
189
<secondary>hash</secondary>
@@ -182,17 +192,24 @@ CREATE INDEX test1_id_index ON test1 (id);
182
192
<primary>hash</primary>
183
193
<see>index</see>
184
194
</indexterm>
185
- Hash indexes can only handle simple equality comparisons.
195
+
196
+ <para>
197
+ Hash indexes store a 32-bit hash code derived from the
198
+ value of the indexed column. Hence,
199
+ such indexes can only handle simple equality comparisons.
186
200
The query planner will consider using a hash index whenever an
187
201
indexed column is involved in a comparison using the
188
- <literal>=</literal> operator.
189
- The following command is used to create a hash index:
202
+ equal operator:
203
+
190
204
<synopsis>
191
- CREATE INDEX <replaceable>name</replaceable> ON <replaceable>table</replaceable> USING HASH (<replaceable>column</replaceable>);
205
+ =
192
206
</synopsis>
193
207
</para>
208
+ </sect2>
209
+
210
+ <sect2 id="indexes-type-gist">
211
+ <title>GiST</title>
194
212
195
- <para>
196
213
<indexterm>
197
214
<primary>index</primary>
198
215
<secondary>GiST</secondary>
@@ -201,6 +218,8 @@ CREATE INDEX <replaceable>name</replaceable> ON <replaceable>table</replaceable>
201
218
<primary>GiST</primary>
202
219
<see>index</see>
203
220
</indexterm>
221
+
222
+ <para>
204
223
GiST indexes are not a single kind of index, but rather an infrastructure
205
224
within which many different indexing strategies can be implemented.
206
225
Accordingly, the particular operators with which a GiST index can be
@@ -210,20 +229,9 @@ CREATE INDEX <replaceable>name</replaceable> ON <replaceable>table</replaceable>
210
229
for several two-dimensional geometric data types, which support indexed
211
230
queries using these operators:
212
231
213
- <simplelist>
214
- <member><literal><<</literal></member>
215
- <member><literal>&<</literal></member>
216
- <member><literal>&></literal></member>
217
- <member><literal>>></literal></member>
218
- <member><literal><<|</literal></member>
219
- <member><literal>&<|</literal></member>
220
- <member><literal>|&></literal></member>
221
- <member><literal>|>></literal></member>
222
- <member><literal>@></literal></member>
223
- <member><literal><@</literal></member>
224
- <member><literal>~=</literal></member>
225
- <member><literal>&&</literal></member>
226
- </simplelist>
232
+ <synopsis>
233
+ << &< &> >> <<| &<| |&> |>> @> <@ ~= &&
234
+ </synopsis>
227
235
228
236
(See <xref linkend="functions-geometry"/> for the meaning of
229
237
these operators.)
@@ -246,8 +254,11 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
246
254
In <xref linkend="gist-builtin-opclasses-table"/>, operators that can be
247
255
used in this way are listed in the column <quote>Ordering Operators</quote>.
248
256
</para>
257
+ </sect2>
258
+
259
+ <sect2 id="indexes-type-spgist">
260
+ <title>SP-GiST</title>
249
261
250
- <para>
251
262
<indexterm>
252
263
<primary>index</primary>
253
264
<secondary>SP-GiST</secondary>
@@ -256,6 +267,8 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
256
267
<primary>SP-GiST</primary>
257
268
<see>index</see>
258
269
</indexterm>
270
+
271
+ <para>
259
272
SP-GiST indexes, like GiST indexes, offer an infrastructure that supports
260
273
various kinds of searches. SP-GiST permits implementation of a wide range
261
274
of different non-balanced disk-based data structures, such as quadtrees,
@@ -264,14 +277,9 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
264
277
for two-dimensional points, which support indexed
265
278
queries using these operators:
266
279
267
- <simplelist>
268
- <member><literal><<</literal></member>
269
- <member><literal>>></literal></member>
270
- <member><literal>~=</literal></member>
271
- <member><literal><@</literal></member>
272
- <member><literal><^</literal></member>
273
- <member><literal>>^</literal></member>
274
- </simplelist>
280
+ <synopsis>
281
+ << >> ~= <@ <<| |>>
282
+ </synopsis>
275
283
276
284
(See <xref linkend="functions-geometry"/> for the meaning of
277
285
these operators.)
@@ -283,11 +291,14 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
283
291
<para>
284
292
Like GiST, SP-GiST supports <quote>nearest-neighbor</quote> searches.
285
293
For SP-GiST operator classes that support distance ordering, the
286
- corresponding operator is specified in the <quote>Ordering Operators</quote>
294
+ corresponding operator is listed in the <quote>Ordering Operators</quote>
287
295
column in <xref linkend="spgist-builtin-opclasses-table"/>.
288
296
</para>
297
+ </sect2>
298
+
299
+ <sect2 id="indexes-types-gin">
300
+ <title>GIN</title>
289
301
290
- <para>
291
302
<indexterm>
292
303
<primary>index</primary>
293
304
<secondary>GIN</secondary>
@@ -296,6 +307,8 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
296
307
<primary>GIN</primary>
297
308
<see>index</see>
298
309
</indexterm>
310
+
311
+ <para>
299
312
GIN indexes are <quote>inverted indexes</quote> which are appropriate for
300
313
data values that contain multiple component values, such as arrays. An
301
314
inverted index contains a separate entry for each component value, and
@@ -312,12 +325,9 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
312
325
<productname>PostgreSQL</productname> includes a GIN operator class
313
326
for arrays, which supports indexed queries using these operators:
314
327
315
- <simplelist>
316
- <member><literal><@</literal></member>
317
- <member><literal>@></literal></member>
318
- <member><literal>=</literal></member>
319
- <member><literal>&&</literal></member>
320
- </simplelist>
328
+ <synopsis>
329
+ <@ @> = &&
330
+ </synopsis>
321
331
322
332
(See <xref linkend="functions-array"/> for the meaning of
323
333
these operators.)
@@ -327,8 +337,11 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
327
337
classes are available in the <literal>contrib</literal> collection or as separate
328
338
projects. For more information see <xref linkend="gin"/>.
329
339
</para>
340
+ </sect2>
341
+
342
+ <sect2 id="indexes-types-brin">
343
+ <title>BRIN</title>
330
344
331
- <para>
332
345
<indexterm>
333
346
<primary>index</primary>
334
347
<secondary>BRIN</secondary>
@@ -337,8 +350,12 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
337
350
<primary>BRIN</primary>
338
351
<see>index</see>
339
352
</indexterm>
353
+
354
+ <para>
340
355
BRIN indexes (a shorthand for Block Range INdexes) store summaries about
341
356
the values stored in consecutive physical block ranges of a table.
357
+ Thus, they are most effective for columns whose values are well-correlated
358
+ with the physical order of the table rows.
342
359
Like GiST, SP-GiST and GIN,
343
360
BRIN can support many different indexing strategies,
344
361
and the particular operators with which a BRIN index can be used
@@ -348,18 +365,15 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
348
365
values in the column for each block range. This supports indexed queries
349
366
using these operators:
350
367
351
- <simplelist>
352
- <member><literal><</literal></member>
353
- <member><literal><=</literal></member>
354
- <member><literal>=</literal></member>
355
- <member><literal>>=</literal></member>
356
- <member><literal>></literal></member>
357
- </simplelist>
368
+ <synopsis>
369
+ < <= = >= >
370
+ </synopsis>
358
371
359
372
The BRIN operator classes included in the standard distribution are
360
373
documented in <xref linkend="brin-builtin-opclasses-table"/>.
361
374
For more information see <xref linkend="brin"/>.
362
375
</para>
376
+ </sect2>
363
377
</sect1>
364
378
365
379
0 commit comments