gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

61 行
6.5KB

  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.5. Combining Multiple 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-ordering.html" title="11.4. Indexes and ORDER BY" /><link rel="next" href="indexes-unique.html" title="11.6. Unique Indexes" /></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.5. Combining Multiple Indexes</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="indexes-ordering.html" title="11.4. Indexes and ORDER BY">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-unique.html" title="11.6. Unique Indexes">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="INDEXES-BITMAP-SCANS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">11.5. Combining Multiple Indexes</h2></div></div></div><a id="id-1.5.10.8.2" class="indexterm"></a><a id="id-1.5.10.8.3" class="indexterm"></a><p>
  3. A single index scan can only use query clauses that use the index's
  4. columns with operators of its operator class and are joined with
  5. <code class="literal">AND</code>. For example, given an index on <code class="literal">(a, b)</code>
  6. a query condition like <code class="literal">WHERE a = 5 AND b = 6</code> could
  7. use the index, but a query like <code class="literal">WHERE a = 5 OR b = 6</code> could not
  8. directly use the index.
  9. </p><p>
  10. Fortunately,
  11. <span class="productname">PostgreSQL</span> has the ability to combine multiple indexes
  12. (including multiple uses of the same index) to handle cases that cannot
  13. be implemented by single index scans. The system can form <code class="literal">AND</code>
  14. and <code class="literal">OR</code> conditions across several index scans. For example,
  15. a query like <code class="literal">WHERE x = 42 OR x = 47 OR x = 53 OR x = 99</code>
  16. could be broken down into four separate scans of an index on <code class="literal">x</code>,
  17. each scan using one of the query clauses. The results of these scans are
  18. then ORed together to produce the result. Another example is that if we
  19. have separate indexes on <code class="literal">x</code> and <code class="literal">y</code>, one possible
  20. implementation of a query like <code class="literal">WHERE x = 5 AND y = 6</code> is to
  21. use each index with the appropriate query clause and then AND together
  22. the index results to identify the result rows.
  23. </p><p>
  24. To combine multiple indexes, the system scans each needed index and
  25. prepares a <em class="firstterm">bitmap</em> in memory giving the locations of
  26. table rows that are reported as matching that index's conditions.
  27. The bitmaps are then ANDed and ORed together as needed by the query.
  28. Finally, the actual table rows are visited and returned. The table rows
  29. are visited in physical order, because that is how the bitmap is laid
  30. out; this means that any ordering of the original indexes is lost, and
  31. so a separate sort step will be needed if the query has an <code class="literal">ORDER
  32. BY</code> clause. For this reason, and because each additional index scan
  33. adds extra time, the planner will sometimes choose to use a simple index
  34. scan even though additional indexes are available that could have been
  35. used as well.
  36. </p><p>
  37. In all but the simplest applications, there are various combinations of
  38. indexes that might be useful, and the database developer must make
  39. trade-offs to decide which indexes to provide. Sometimes multicolumn
  40. indexes are best, but sometimes it's better to create separate indexes
  41. and rely on the index-combination feature. For example, if your
  42. workload includes a mix of queries that sometimes involve only column
  43. <code class="literal">x</code>, sometimes only column <code class="literal">y</code>, and sometimes both
  44. columns, you might choose to create two separate indexes on
  45. <code class="literal">x</code> and <code class="literal">y</code>, relying on index combination to
  46. process the queries that use both columns. You could also create a
  47. multicolumn index on <code class="literal">(x, y)</code>. This index would typically be
  48. more efficient than index combination for queries involving both
  49. columns, but as discussed in <a class="xref" href="indexes-multicolumn.html" title="11.3. Multicolumn Indexes">Section 11.3</a>, it
  50. would be almost useless for queries involving only <code class="literal">y</code>, so it
  51. should not be the only index. A combination of the multicolumn index
  52. and a separate index on <code class="literal">y</code> would serve reasonably well. For
  53. queries involving only <code class="literal">x</code>, the multicolumn index could be
  54. used, though it would be larger and hence slower than an index on
  55. <code class="literal">x</code> alone. The last alternative is to create all three
  56. indexes, but this is probably only reasonable if the table is searched
  57. much more often than it is updated and all three types of query are
  58. common. If one of the types of query is much less common than the
  59. others, you'd probably settle for creating just the two indexes that
  60. best match the common types.
  61. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="indexes-ordering.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-unique.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">11.4. Indexes and <code class="literal">ORDER BY</code> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 11.6. Unique Indexes</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1