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