inheritance, index, foreign key, and other ancillary information
about the table.
</para>
<para>
Because <command>CLUSTER</command> remembers the clustering information,
one can cluster the tables one wants clustered manually the first time, and
setup a timed event similar to <command>VACUUM</command> so that the tables
are periodically re-clustered.
are periodically reclustered.
</para>
<para>
Because the optimizer records statistics about the ordering of tables, it
Because the planner records statistics about the ordering of tables, it
is advisable to run <command>ANALYZE</command> on the newly clustered
table. Otherwise, the optimizer may make poor choices of query plans.
table. Otherwise, the planner may make poor choices of query plans.
</para>
<para>
...
...
@@ -196,68 +157,57 @@ CLUSTER
but the majority of a big table will not fit in the cache.)
The other way to cluster a table is to use
<programlisting>
SELECT <replaceable class="parameter">columnlist</replaceable> INTO TABLE <replaceable class="parameter">newtable</replaceable>
FROM <replaceable class="parameter">table</replaceable> ORDER BY <replaceable class="parameter">columnlist</replaceable>
</programlisting>
<programlisting>
CREATE TABLE <replaceable class="parameter">newtable</replaceable> AS
SELECT <replaceable class="parameter">columnlist</replaceable> FROM <replaceable class="parameter">table</replaceable> ORDER BY <replaceable class="parameter">columnlist</replaceable>;
</programlisting>
which uses the <productname>PostgreSQL</productname> sorting code in
the ORDER BY clause to create the desired order; this is usually much
the <literal>ORDER BY</literal> clause to create the desired order; this is usually much
faster than an index scan for
unordered data. You then drop the old table, use
<command>ALTER TABLE...RENAME</command>
<command>ALTER TABLE ... RENAME</command>
to rename <replaceable class="parameter">newtable</replaceable> to the old name, and
recreate the table's indexes. However, this approach does not preserve
OIDs, constraints, foreign key relationships, granted privileges, and
other ancillary properties of the table --- all such items must be
manually recreated.
</para>
</refsect2>
</refsect1>
<refsect1 id="R1-SQL-CLUSTER-2">
<title>
Usage
</title>
<refsect1>
<title>Examples</title>
<para>
Cluster the <literal>employees</literal> relation on the basis of
its ID attribute:
</para>
<programlisting>
Cluster the table <literal>employees</literal> on the basis of
its index <literal>emp_ind</literal>:
<programlisting>
CLUSTER emp_ind ON emp;
</programlisting>
</programlisting>
</para>
<para>
Cluster the <literal>employees</literal> relation using the same
index that was used before:
</para>
<programlisting>
<programlisting>
CLUSTER emp;
</programlisting>
</programlisting>
</para>
<para>
Cluster all the tables on the database that have previously been clustered:
</para>
<programlisting>
<programlisting>
CLUSTER;
</programlisting>
</programlisting>
</para>
</refsect1>
<refsect1 id="R1-SQL-CLUSTER-3">
<title>
Compatibility
</title>
<refsect2 id="R2-SQL-CLUSTER-4">
<refsect2info>
<date>1998-09-08</date>
</refsect2info>
<title>
SQL92
</title>
<para>
There is no <command>CLUSTER</command> statement in SQL92.
</para>
</refsect2>
<refsect1>
<title>Compatibility</title>
<para>
There is no <command>CLUSTER</command> statement in the SQL standard.