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.

80 lines
6.7KB

  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.3. Multicolumn Indexes</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-types.html" title="11.2. Index Types" /><link rel="next" href="indexes-ordering.html" title="11.4. Indexes and ORDER BY" /></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.3. Multicolumn Indexes</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="indexes-types.html" title="11.2. Index Types">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-ordering.html" title="11.4. Indexes and ORDER BY">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="INDEXES-MULTICOLUMN"><div class="titlepage"><div><div><h2 class="title" style="clear: both">11.3. Multicolumn Indexes</h2></div></div></div><a id="id-1.5.10.6.2" class="indexterm"></a><p>
  3. An index can be defined on more than one column of a table. For example, if
  4. you have a table of this form:
  5. </p><pre class="programlisting">
  6. CREATE TABLE test2 (
  7. major int,
  8. minor int,
  9. name varchar
  10. );
  11. </pre><p>
  12. (say, you keep your <code class="filename">/dev</code>
  13. directory in a database...) and you frequently issue queries like:
  14. </p><pre class="programlisting">
  15. SELECT name FROM test2 WHERE major = <em class="replaceable"><code>constant</code></em> AND minor = <em class="replaceable"><code>constant</code></em>;
  16. </pre><p>
  17. then it might be appropriate to define an index on the columns
  18. <code class="structfield">major</code> and
  19. <code class="structfield">minor</code> together, e.g.:
  20. </p><pre class="programlisting">
  21. CREATE INDEX test2_mm_idx ON test2 (major, minor);
  22. </pre><p>
  23. </p><p>
  24. Currently, only the B-tree, GiST, GIN, and BRIN
  25. index types support multicolumn
  26. indexes. Up to 32 columns can be specified. (This limit can be
  27. altered when building <span class="productname">PostgreSQL</span>; see the
  28. file <code class="filename">pg_config_manual.h</code>.)
  29. </p><p>
  30. A multicolumn B-tree index can be used with query conditions that
  31. involve any subset of the index's columns, but the index is most
  32. efficient when there are constraints on the leading (leftmost) columns.
  33. The exact rule is that equality constraints on leading columns, plus
  34. any inequality constraints on the first column that does not have an
  35. equality constraint, will be used to limit the portion of the index
  36. that is scanned. Constraints on columns to the right of these columns
  37. are checked in the index, so they save visits to the table proper, but
  38. they do not reduce the portion of the index that has to be scanned.
  39. For example, given an index on <code class="literal">(a, b, c)</code> and a
  40. query condition <code class="literal">WHERE a = 5 AND b &gt;= 42 AND c &lt; 77</code>,
  41. the index would have to be scanned from the first entry with
  42. <code class="literal">a</code> = 5 and <code class="literal">b</code> = 42 up through the last entry with
  43. <code class="literal">a</code> = 5. Index entries with <code class="literal">c</code> &gt;= 77 would be
  44. skipped, but they'd still have to be scanned through.
  45. This index could in principle be used for queries that have constraints
  46. on <code class="literal">b</code> and/or <code class="literal">c</code> with no constraint on <code class="literal">a</code>
  47. — but the entire index would have to be scanned, so in most cases
  48. the planner would prefer a sequential table scan over using the index.
  49. </p><p>
  50. A multicolumn GiST index can be used with query conditions that
  51. involve any subset of the index's columns. Conditions on additional
  52. columns restrict the entries returned by the index, but the condition on
  53. the first column is the most important one for determining how much of
  54. the index needs to be scanned. A GiST index will be relatively
  55. ineffective if its first column has only a few distinct values, even if
  56. there are many distinct values in additional columns.
  57. </p><p>
  58. A multicolumn GIN index can be used with query conditions that
  59. involve any subset of the index's columns. Unlike B-tree or GiST,
  60. index search effectiveness is the same regardless of which index column(s)
  61. the query conditions use.
  62. </p><p>
  63. A multicolumn BRIN index can be used with query conditions that
  64. involve any subset of the index's columns. Like GIN and unlike B-tree or
  65. GiST, index search effectiveness is the same regardless of which index
  66. column(s) the query conditions use. The only reason to have multiple BRIN
  67. indexes instead of one multicolumn BRIN index on a single table is to have
  68. a different <code class="literal">pages_per_range</code> storage parameter.
  69. </p><p>
  70. Of course, each column must be used with operators appropriate to the index
  71. type; clauses that involve other operators will not be considered.
  72. </p><p>
  73. Multicolumn indexes should be used sparingly. In most situations,
  74. an index on a single column is sufficient and saves space and time.
  75. Indexes with more than three columns are unlikely to be helpful
  76. unless the usage of the table is extremely stylized. See also
  77. <a class="xref" href="indexes-bitmap-scans.html" title="11.5. Combining Multiple Indexes">Section 11.5</a> and
  78. <a class="xref" href="indexes-index-only-scans.html" title="11.9. Index-Only Scans and Covering Indexes">Section 11.9</a> for some discussion of the
  79. merits of different index configurations.
  80. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="indexes-types.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-ordering.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">11.2. Index Types </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 11.4. Indexes and <code class="literal">ORDER BY</code></td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1