1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5
>Partial Indexes</TITLE
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
12
TITLE="PostgreSQL 9.1beta1 Documentation"
13
HREF="index.html"><LINK
16
HREF="indexes.html"><LINK
18
TITLE="Indexes on Expressions"
19
HREF="indexes-expressional.html"><LINK
21
TITLE="Operator Classes and Operator Families"
22
HREF="indexes-opclass.html"><LINK
25
HREF="stylesheet.css"><META
26
HTTP-EQUIV="Content-Type"
27
CONTENT="text/html; charset=ISO-8859-1"><META
29
CONTENT="2011-04-27T21:20:33"></HEAD
35
SUMMARY="Header navigation table"
47
>PostgreSQL 9.1beta1 Documentation</A
56
TITLE="Indexes on Expressions"
57
HREF="indexes-expressional.html"
74
>Chapter 11. Indexes</TD
89
TITLE="Operator Classes and Operator Families"
90
HREF="indexes-opclass.html"
104
NAME="INDEXES-PARTIAL"
105
>11.8. Partial Indexes</A
111
> is an index built over a
112
subset of a table; the subset is defined by a conditional
113
expression (called the <I
117
partial index). The index contains entries only for those table
118
rows that satisfy the predicate. Partial indexes are a specialized
119
feature, but there are several situations in which they are useful.
122
> One major reason for using a partial index is to avoid indexing common
123
values. Since a query searching for a common value (one that
124
accounts for more than a few percent of all the table rows) will not
125
use the index anyway, there is no point in keeping those rows in the
126
index at all. This reduces the size of the index, which will speed
127
up those queries that do use the index. It will also speed up many table
128
update operations because the index does not need to be
129
updated in all cases. <A
130
HREF="indexes-partial.html#INDEXES-PARTIAL-EX1"
133
possible application of this idea.
138
NAME="INDEXES-PARTIAL-EX1"
142
>Example 11-1. Setting up a Partial Index to Exclude Common Values</B
145
> Suppose you are storing web server access logs in a database.
146
Most accesses originate from the IP address range of your organization but
147
some are from elsewhere (say, employees on dial-up connections).
148
If your searches by IP are primarily for outside accesses,
149
you probably do not need to index the IP range that corresponds to your
150
organization's subnet.
153
> Assume a table like this:
155
CLASS="PROGRAMLISTING"
156
>CREATE TABLE access_log (
164
> To create a partial index that suits our example, use a command
167
CLASS="PROGRAMLISTING"
168
>CREATE INDEX access_log_client_ip_ix ON access_log (client_ip)
169
WHERE NOT (client_ip > inet '192.168.100.0' AND
170
client_ip < inet '192.168.100.255');</PRE
174
> A typical query that can use this index would be:
176
CLASS="PROGRAMLISTING"
179
WHERE url = '/index.html' AND client_ip = inet '212.78.10.32';</PRE
181
A query that cannot use this index is:
183
CLASS="PROGRAMLISTING"
186
WHERE client_ip = inet '192.168.100.23';</PRE
190
> Observe that this kind of partial index requires that the common
191
values be predetermined, so such partial indexes are best used for
192
data distributions that do not change. The indexes can be recreated
193
occasionally to adjust for new data distributions, but this adds
198
> Another possible use for a partial index is to exclude values from the
200
typical query workload is not interested in; this is shown in <A
201
HREF="indexes-partial.html#INDEXES-PARTIAL-EX2"
203
>. This results in the same
204
advantages as listed above, but it prevents the
207
>"uninteresting"</SPAN
208
> values from being accessed via that
209
index, even if an index scan might be profitable in that
210
case. Obviously, setting up partial indexes for this kind of
211
scenario will require a lot of care and experimentation.
216
NAME="INDEXES-PARTIAL-EX2"
220
>Example 11-2. Setting up a Partial Index to Exclude Uninteresting Values</B
223
> If you have a table that contains both billed and unbilled orders,
224
where the unbilled orders take up a small fraction of the total
225
table and yet those are the most-accessed rows, you can improve
226
performance by creating an index on just the unbilled rows. The
227
command to create the index would look like this:
229
CLASS="PROGRAMLISTING"
230
>CREATE INDEX orders_unbilled_index ON orders (order_nr)
231
WHERE billed is not true;</PRE
235
> A possible query to use this index would be:
237
CLASS="PROGRAMLISTING"
238
>SELECT * FROM orders WHERE billed is not true AND order_nr < 10000;</PRE
240
However, the index can also be used in queries that do not involve
246
CLASS="PROGRAMLISTING"
247
>SELECT * FROM orders WHERE billed is not true AND amount > 5000.00;</PRE
249
This is not as efficient as a partial index on the
253
> column would be, since the system has to
254
scan the entire index. Yet, if there are relatively few unbilled
255
orders, using this partial index just to find the unbilled orders
259
> Note that this query cannot use this index:
261
CLASS="PROGRAMLISTING"
262
>SELECT * FROM orders WHERE order_nr = 3501;</PRE
264
The order 3501 might be among the billed or unbilled
270
HREF="indexes-partial.html#INDEXES-PARTIAL-EX2"
272
> also illustrates that the
273
indexed column and the column used in the predicate do not need to
278
indexes with arbitrary predicates, so long as only columns of the
279
table being indexed are involved. However, keep in mind that the
280
predicate must match the conditions used in the queries that
281
are supposed to benefit from the index. To be precise, a partial
282
index can be used in a query only if the system can recognize that
286
> condition of the query mathematically implies
287
the predicate of the index.
291
> does not have a sophisticated
292
theorem prover that can recognize mathematically equivalent
293
expressions that are written in different forms. (Not
294
only is such a general theorem prover extremely difficult to
295
create, it would probably be too slow to be of any real use.)
296
The system can recognize simple inequality implications, for example
304
the predicate condition must exactly match part of the query's
309
or the index will not be recognized as usable. Matching takes
310
place at query planning time, not at run time. As a result,
311
parameterized query clauses do not work with a partial index. For
312
example a prepared query with a parameter might specify
316
> which will never imply
320
> for all possible values of the parameter.
323
> A third possible use for partial indexes does not require the
324
index to be used in queries at all. The idea here is to create
325
a unique index over a subset of a table, as in <A
326
HREF="indexes-partial.html#INDEXES-PARTIAL-EX3"
328
>. This enforces uniqueness
329
among the rows that satisfy the index predicate, without constraining
335
NAME="INDEXES-PARTIAL-EX3"
339
>Example 11-3. Setting up a Partial Unique Index</B
342
> Suppose that we have a table describing test outcomes. We wish
343
to ensure that there is only one <SPAN
347
a given subject and target combination, but there might be any number of
350
>"unsuccessful"</SPAN
351
> entries. Here is one way to do it:
353
CLASS="PROGRAMLISTING"
354
>CREATE TABLE tests (
361
CREATE UNIQUE INDEX tests_success_constraint ON tests (subject, target)
364
This is a particularly efficient approach when there are few
365
successful tests and many unsuccessful ones.
369
> Finally, a partial index can also be used to override the system's
370
query plan choices. Also, data sets with peculiar
371
distributions might cause the system to use an index when it really
372
should not. In that case the index can be set up so that it is not
373
available for the offending query. Normally,
377
> makes reasonable choices about index
378
usage (e.g., it avoids them when retrieving common values, so the
379
earlier example really only saves index size, it is not required to
380
avoid index usage), and grossly incorrect plan choices are cause
384
> Keep in mind that setting up a partial index indicates that you
385
know at least as much as the query planner knows, in particular you
386
know when an index might be profitable. Forming this knowledge
387
requires experience and understanding of how indexes in
391
> work. In most cases, the advantage of a
392
partial index over a regular index will be minimal.
395
> More information about partial indexes can be found in <A
396
HREF="biblio.html#STON89B"
399
HREF="http://db.cs.berkeley.edu/papers/ERL-M89-17.pdf"
401
> The case for partial indexes
406
HREF="biblio.html#OLSON93"
408
>Partial indexing in POSTGRES: research project</I
411
HREF="biblio.html#SESHADRI95"
413
>Generalized Partial Indexes
415
HREF="http://citeseer.ist.psu.edu/seshadri95generalized.html"
429
SUMMARY="Footer navigation table"
440
HREF="indexes-expressional.html"
458
HREF="indexes-opclass.html"
468
>Indexes on Expressions</TD
482
>Operator Classes and Operator Families</TD
b'\\ No newline at end of file'