gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

117 linhas
9.2KB

  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>CLUSTER</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="sql-close.html" title="CLOSE" /><link rel="next" href="sql-comment.html" title="COMMENT" /></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">CLUSTER</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-close.html" title="CLOSE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</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="sql-comment.html" title="COMMENT">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CLUSTER"><div class="titlepage"></div><a id="id-1.9.3.51.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CLUSTER</span></h2><p>CLUSTER — cluster a table according to an index</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CLUSTER [VERBOSE] <em class="replaceable"><code>table_name</code></em> [ USING <em class="replaceable"><code>index_name</code></em> ]
  4. CLUSTER [VERBOSE]
  5. </pre></div><div class="refsect1" id="id-1.9.3.51.5"><h2>Description</h2><p>
  6. <code class="command">CLUSTER</code> instructs <span class="productname">PostgreSQL</span>
  7. to cluster the table specified
  8. by <em class="replaceable"><code>table_name</code></em>
  9. based on the index specified by
  10. <em class="replaceable"><code>index_name</code></em>. The index must
  11. already have been defined on
  12. <em class="replaceable"><code>table_name</code></em>.
  13. </p><p>
  14. When a table is clustered, it is physically reordered
  15. based on the index information. Clustering is a one-time operation:
  16. when the table is subsequently updated, the changes are
  17. not clustered. That is, no attempt is made to store new or
  18. updated rows according to their index order. (If one wishes, one can
  19. periodically recluster by issuing the command again. Also, setting
  20. the table's <code class="literal">fillfactor</code> storage parameter to less than
  21. 100% can aid in preserving cluster ordering during updates, since updated
  22. rows are kept on the same page if enough space is available there.)
  23. </p><p>
  24. When a table is clustered, <span class="productname">PostgreSQL</span>
  25. remembers which index it was clustered by. The form
  26. <code class="command">CLUSTER <em class="replaceable"><code>table_name</code></em></code>
  27. reclusters the table using the same index as before. You can also
  28. use the <code class="literal">CLUSTER</code> or <code class="literal">SET WITHOUT CLUSTER</code>
  29. forms of <a class="xref" href="sql-altertable.html" title="ALTER TABLE"><span class="refentrytitle">ALTER TABLE</span></a> to set the index to be used for
  30. future cluster operations, or to clear any previous setting.
  31. </p><p>
  32. <code class="command">CLUSTER</code> without any parameter reclusters all the
  33. previously-clustered tables in the current database that the calling user
  34. owns, or all such tables if called by a superuser. This
  35. form of <code class="command">CLUSTER</code> cannot be executed inside a transaction
  36. block.
  37. </p><p>
  38. When a table is being clustered, an <code class="literal">ACCESS
  39. EXCLUSIVE</code> lock is acquired on it. This prevents any other
  40. database operations (both reads and writes) from operating on the
  41. table until the <code class="command">CLUSTER</code> is finished.
  42. </p></div><div class="refsect1" id="id-1.9.3.51.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
  43. The name (possibly schema-qualified) of a table.
  44. </p></dd><dt><span class="term"><em class="replaceable"><code>index_name</code></em></span></dt><dd><p>
  45. The name of an index.
  46. </p></dd><dt><span class="term"><code class="literal">VERBOSE</code></span></dt><dd><p>
  47. Prints a progress report as each table is clustered.
  48. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.51.7"><h2>Notes</h2><p>
  49. In cases where you are accessing single rows randomly
  50. within a table, the actual order of the data in the
  51. table is unimportant. However, if you tend to access some
  52. data more than others, and there is an index that groups
  53. them together, you will benefit from using <code class="command">CLUSTER</code>.
  54. If you are requesting a range of indexed values from a table, or a
  55. single indexed value that has multiple rows that match,
  56. <code class="command">CLUSTER</code> will help because once the index identifies the
  57. table page for the first row that matches, all other rows
  58. that match are probably already on the same table page,
  59. and so you save disk accesses and speed up the query.
  60. </p><p>
  61. <code class="command">CLUSTER</code> can re-sort the table using either an index scan
  62. on the specified index, or (if the index is a b-tree) a sequential
  63. scan followed by sorting. It will attempt to choose the method that
  64. will be faster, based on planner cost parameters and available statistical
  65. information.
  66. </p><p>
  67. When an index scan is used, a temporary copy of the table is created that
  68. contains the table data in the index order. Temporary copies of each
  69. index on the table are created as well. Therefore, you need free space on
  70. disk at least equal to the sum of the table size and the index sizes.
  71. </p><p>
  72. When a sequential scan and sort is used, a temporary sort file is
  73. also created, so that the peak temporary space requirement is as much
  74. as double the table size, plus the index sizes. This method is often
  75. faster than the index scan method, but if the disk space requirement is
  76. intolerable, you can disable this choice by temporarily setting <a class="xref" href="runtime-config-query.html#GUC-ENABLE-SORT">enable_sort</a> to <code class="literal">off</code>.
  77. </p><p>
  78. It is advisable to set <a class="xref" href="runtime-config-resource.html#GUC-MAINTENANCE-WORK-MEM">maintenance_work_mem</a> to
  79. a reasonably large value (but not more than the amount of RAM you can
  80. dedicate to the <code class="command">CLUSTER</code> operation) before clustering.
  81. </p><p>
  82. Because the planner records statistics about the ordering of
  83. tables, it is advisable to run <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a>
  84. on the newly clustered table.
  85. Otherwise, the planner might make poor choices of query plans.
  86. </p><p>
  87. Because <code class="command">CLUSTER</code> remembers which indexes are clustered,
  88. one can cluster the tables one wants clustered manually the first time,
  89. then set up a periodic maintenance script that executes
  90. <code class="command">CLUSTER</code> without any parameters, so that the desired tables
  91. are periodically reclustered.
  92. </p></div><div class="refsect1" id="id-1.9.3.51.8"><h2>Examples</h2><p>
  93. Cluster the table <code class="literal">employees</code> on the basis of
  94. its index <code class="literal">employees_ind</code>:
  95. </p><pre class="programlisting">
  96. CLUSTER employees USING employees_ind;
  97. </pre><p>
  98. </p><p>
  99. Cluster the <code class="literal">employees</code> table using the same
  100. index that was used before:
  101. </p><pre class="programlisting">
  102. CLUSTER employees;
  103. </pre><p>
  104. </p><p>
  105. Cluster all tables in the database that have previously been clustered:
  106. </p><pre class="programlisting">
  107. CLUSTER;
  108. </pre></div><div class="refsect1" id="id-1.9.3.51.9"><h2>Compatibility</h2><p>
  109. There is no <code class="command">CLUSTER</code> statement in the SQL standard.
  110. </p><p>
  111. The syntax
  112. </p><pre class="synopsis">
  113. CLUSTER <em class="replaceable"><code>index_name</code></em> ON <em class="replaceable"><code>table_name</code></em>
  114. </pre><p>
  115. is also supported for compatibility with pre-8.3 <span class="productname">PostgreSQL</span>
  116. versions.
  117. </p></div><div class="refsect1" id="id-1.9.3.51.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="app-clusterdb.html" title="clusterdb"><span class="refentrytitle"><span class="application">clusterdb</span></span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-close.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-comment.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CLOSE </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> COMMENT</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1