1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter11.Indexes</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="sql.html" title="PartII.The SQL Language">
<link rel="prev" href="typeconv-union-case.html" title="10.5.UNION, CASE, and Related Constructs">
<link rel="next" href="indexes-types.html" title="11.2.Index Types">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" lang="en" id="indexes">
<div class="titlepage"><div><div><h2 class="title">
<a name="indexes"></a>Chapter11.Indexes</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="sect1"><a href="indexes.html#indexes-intro">11.1. Introduction</a></span></dt>
<dt><span class="sect1"><a href="indexes-types.html">11.2. Index Types</a></span></dt>
<dt><span class="sect1"><a href="indexes-multicolumn.html">11.3. Multicolumn Indexes</a></span></dt>
<dt><span class="sect1"><a href="indexes-bitmap-scans.html">11.4. Combining Multiple Indexes</a></span></dt>
<dt><span class="sect1"><a href="indexes-unique.html">11.5. Unique Indexes</a></span></dt>
<dt><span class="sect1"><a href="indexes-expressional.html">11.6. Indexes on Expressions</a></span></dt>
<dt><span class="sect1"><a href="indexes-partial.html">11.7. Partial Indexes</a></span></dt>
<dt><span class="sect1"><a href="indexes-opclass.html">11.8. Operator Classes</a></span></dt>
<dt><span class="sect1"><a href="indexes-examine.html">11.9. Examining Index Usage</a></span></dt>
</dl>
</div>
<a name="id627616"></a><p> Indexes are a common way to enhance database performance. An index
allows the database server to find and retrieve specific rows much
faster than it could do without an index. But indexes also add
overhead to the database system as a whole, so they should be used
sensibly.
</p>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="indexes-intro"></a>11.1.Introduction</h2></div></div></div>
<p> Suppose we have a table similar to this:
</p>
<pre class="programlisting">CREATE TABLE test1 (
id integer,
content varchar
);</pre>
<p>
and the application requires a lot of queries of the form
</p>
<pre class="programlisting">SELECT content FROM test1 WHERE id = <em class="replaceable"><code>constant</code></em>;</pre>
<p>
With no advance preparation, the system would have to scan the entire
<code class="structname">test1</code> table, row by row, to find all
matching entries. If there are a lot of rows in
<code class="structname">test1</code> and only a few rows (perhaps only zero
or one) that would be returned by such a query, then this is clearly an
inefficient method. But if the system has been instructed to maintain an
index on the <code class="structfield">id</code> column, then it can use a more
efficient method for locating matching rows. For instance, it
might only have to walk a few levels deep into a search tree.
</p>
<p> A similar approach is used in most books of non-fiction: terms and
concepts that are frequently looked up by readers are collected in
an alphabetic index at the end of the book. The interested reader
can scan the index relatively quickly and flip to the appropriate
page(s), rather than having to read the entire book to find the
material of interest. Just as it is the task of the author to
anticipate the items that the readers are likely to look up,
it is the task of the database programmer to foresee which indexes
will be of advantage.
</p>
<p> The following command would be used to create the index on the
<code class="structfield">id</code> column, as discussed:
</p>
<pre class="programlisting">CREATE INDEX test1_id_index ON test1 (id);</pre>
<p>
The name <code class="structname">test1_id_index</code> can be chosen
freely, but you should pick something that enables you to remember
later what the index was for.
</p>
<p> To remove an index, use the <code class="command">DROP INDEX</code> command.
Indexes can be added to and removed from tables at any time.
</p>
<p> Once an index is created, no further intervention is required: the
system will update the index when the table is modified, and it will
use the index in queries when it thinks this would be more efficient
than a sequential table scan. But you may have to run the
<code class="command">ANALYZE</code> command regularly to update
statistics to allow the query planner to make educated decisions.
See <a href="performance-tips.html" title="Chapter13.Performance Tips">Chapter13, <i>Performance Tips</i></a> for information about
how to find out whether an index is used and when and why the
planner may choose <span class="emphasis"><em>not</em></span> to use an index.
</p>
<p> Indexes can also benefit <code class="command">UPDATE</code> and
<code class="command">DELETE</code> commands with search conditions.
Indexes can moreover be used in join searches. Thus,
an index defined on a column that is part of a join condition can
significantly speed up queries with joins.
</p>
<p> After an index is created, the system has to keep it synchronized with the
table. This adds overhead to data manipulation operations.
Therefore indexes that are seldom or never used in queries
should be removed.
</p>
</div>
</div></body>
</html>
|