gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
6.1KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>11.1. Introduction</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="indexes.html" title="Chapter 11. Indexes" /><link rel="next" href="indexes-types.html" title="11.2. Index Types" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">11.1. Introduction</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="indexes.html" title="Chapter 11. Indexes">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="indexes.html" title="Chapter 11. Indexes">Up</a></td><th width="60%" align="center">Chapter 11. Indexes</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="indexes-types.html" title="11.2. Index Types">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="INDEXES-INTRO"><div class="titlepage"><div><div><h2 class="title" style="clear: both">11.1. Introduction</h2></div></div></div><p>
  3. Suppose we have a table similar to this:
  4. </p><pre class="programlisting">
  5. CREATE TABLE test1 (
  6. id integer,
  7. content varchar
  8. );
  9. </pre><p>
  10. and the application issues many queries of the form:
  11. </p><pre class="programlisting">
  12. SELECT content FROM test1 WHERE id = <em class="replaceable"><code>constant</code></em>;
  13. </pre><p>
  14. With no advance preparation, the system would have to scan the entire
  15. <code class="structname">test1</code> table, row by row, to find all
  16. matching entries. If there are many rows in
  17. <code class="structname">test1</code> and only a few rows (perhaps zero
  18. or one) that would be returned by such a query, this is clearly an
  19. inefficient method. But if the system has been instructed to maintain an
  20. index on the <code class="structfield">id</code> column, it can use a more
  21. efficient method for locating matching rows. For instance, it
  22. might only have to walk a few levels deep into a search tree.
  23. </p><p>
  24. A similar approach is used in most non-fiction books: terms and
  25. concepts that are frequently looked up by readers are collected in
  26. an alphabetic index at the end of the book. The interested reader
  27. can scan the index relatively quickly and flip to the appropriate
  28. page(s), rather than having to read the entire book to find the
  29. material of interest. Just as it is the task of the author to
  30. anticipate the items that readers are likely to look up,
  31. it is the task of the database programmer to foresee which indexes
  32. will be useful.
  33. </p><p>
  34. The following command can be used to create an index on the
  35. <code class="structfield">id</code> column, as discussed:
  36. </p><pre class="programlisting">
  37. CREATE INDEX test1_id_index ON test1 (id);
  38. </pre><p>
  39. The name <code class="structname">test1_id_index</code> can be chosen
  40. freely, but you should pick something that enables you to remember
  41. later what the index was for.
  42. </p><p>
  43. To remove an index, use the <code class="command">DROP INDEX</code> command.
  44. Indexes can be added to and removed from tables at any time.
  45. </p><p>
  46. Once an index is created, no further intervention is required: the
  47. system will update the index when the table is modified, and it will
  48. use the index in queries when it thinks doing so would be more efficient
  49. than a sequential table scan. But you might have to run the
  50. <code class="command">ANALYZE</code> command regularly to update
  51. statistics to allow the query planner to make educated decisions.
  52. See <a class="xref" href="performance-tips.html" title="Chapter 14. Performance Tips">Chapter 14</a> for information about
  53. how to find out whether an index is used and when and why the
  54. planner might choose <span class="emphasis"><em>not</em></span> to use an index.
  55. </p><p>
  56. Indexes can also benefit <code class="command">UPDATE</code> and
  57. <code class="command">DELETE</code> commands with search conditions.
  58. Indexes can moreover be used in join searches. Thus,
  59. an index defined on a column that is part of a join condition can
  60. also significantly speed up queries with joins.
  61. </p><p>
  62. Creating an index on a large table can take a long time. By default,
  63. <span class="productname">PostgreSQL</span> allows reads (<code class="command">SELECT</code> statements) to occur
  64. on the table in parallel with index creation, but writes (<code class="command">INSERT</code>,
  65. <code class="command">UPDATE</code>, <code class="command">DELETE</code>) are blocked until the index build is finished.
  66. In production environments this is often unacceptable.
  67. It is possible to allow writes to occur in parallel with index
  68. creation, but there are several caveats to be aware of —
  69. for more information see <a class="xref" href="sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY" title="Building Indexes Concurrently">Building Indexes Concurrently</a>.
  70. </p><p>
  71. After an index is created, the system has to keep it synchronized with the
  72. table. This adds overhead to data manipulation operations.
  73. Therefore indexes that are seldom or never used in queries
  74. should be removed.
  75. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="indexes.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="indexes.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="indexes-types.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 11. Indexes </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 11.2. Index Types</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1