|
Server : Apache/2.2.2 (Fedora) System : Linux App1.pathumtani.go.th 2.6.20-1.2320.fc5smp #1 SMP Tue Jun 12 19:40:16 EDT 2007 i686 User : apache ( 48) PHP Version : 5.2.9 Disable Function : NONE Directory : /proc/self/root/usr/share/doc/postgresql-8.1.9/html/ |
Upload File : |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>CLUSTER</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 8.1.9 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="CLOSE"
HREF="sql-close.html"><LINK
REL="NEXT"
TITLE="COMMENT"
HREF="sql-comment.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2007-04-20T04:40:08"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 8.1.9 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-close.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-close.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-comment.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-comment.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-CLUSTER"
></A
>CLUSTER</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN40635"
></A
><H2
>Name</H2
>CLUSTER -- cluster a table according to an index</DIV
><A
NAME="AEN40638"
></A
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN40640"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>CLUSTER <TT
CLASS="REPLACEABLE"
><I
>indexname</I
></TT
> ON <TT
CLASS="REPLACEABLE"
><I
>tablename</I
></TT
>
CLUSTER <TT
CLASS="REPLACEABLE"
><I
>tablename</I
></TT
>
CLUSTER</PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN40645"
></A
><H2
>Description</H2
><P
> <TT
CLASS="COMMAND"
>CLUSTER</TT
> instructs <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
to cluster the table specified
by <TT
CLASS="REPLACEABLE"
><I
>tablename</I
></TT
>
based on the index specified by
<TT
CLASS="REPLACEABLE"
><I
>indexname</I
></TT
>. The index must
already have been defined on
<TT
CLASS="REPLACEABLE"
><I
>tablename</I
></TT
>.
</P
><P
> When a table is clustered, it is physically reordered
based on the index information. Clustering is a one-time operation:
when the table is subsequently updated, the changes are
not clustered. That is, no attempt is made to store new or
updated rows according to their index order. If one wishes, one can
periodically recluster by issuing the command again.
</P
><P
> When a table is clustered, <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
remembers on which index it was clustered. The form
<TT
CLASS="COMMAND"
>CLUSTER <TT
CLASS="REPLACEABLE"
><I
>tablename</I
></TT
></TT
>
reclusters the table on the same index that it was clustered before.
</P
><P
> <TT
CLASS="COMMAND"
>CLUSTER</TT
> without any parameter reclusters all the tables
in the
current database that the calling user owns, or all tables if called
by a superuser. (Never-clustered tables are not included.) This
form of <TT
CLASS="COMMAND"
>CLUSTER</TT
> cannot be called from inside a
transaction or function.
</P
><P
> When a table is being clustered, an <TT
CLASS="LITERAL"
>ACCESS
EXCLUSIVE</TT
> lock is acquired on it. This prevents any other
database operations (both reads and writes) from operating on the
table until the <TT
CLASS="COMMAND"
>CLUSTER</TT
> is finished.
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN40664"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>indexname</I
></TT
></DT
><DD
><P
> The name of an index.
</P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>tablename</I
></TT
></DT
><DD
><P
> The name (possibly schema-qualified) of a table.
</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN40677"
></A
><H2
>Notes</H2
><P
> In cases where you are accessing single rows randomly
within a table, the actual order of the data in the
table is unimportant. However, if you tend to access some
data more than others, and there is an index that groups
them together, you will benefit from using <TT
CLASS="COMMAND"
>CLUSTER</TT
>.
If you are requesting a range of indexed values from a table, or a
single indexed value that has multiple rows that match,
<TT
CLASS="COMMAND"
>CLUSTER</TT
> will help because once the index identifies the
heap page for the first row that matches, all other rows
that match are probably already on the same heap page,
and so you save disk accesses and speed up the query.
</P
><P
> During the cluster operation, a temporary copy of the table is created
that contains the table data in the index order. Temporary copies of
each index on the table are created as well. Therefore, you need free
space on disk at least equal to the sum of the table size and the index
sizes.
</P
><P
> Because <TT
CLASS="COMMAND"
>CLUSTER</TT
> remembers the clustering information,
one can cluster the tables one wants clustered manually the first time, and
setup a timed event similar to <TT
CLASS="COMMAND"
>VACUUM</TT
> so that the tables
are periodically reclustered.
</P
><P
> Because the planner records statistics about the ordering of
tables, it is advisable to run <A
HREF="sql-analyze.html"
><I
>ANALYZE</I
></A
> on the newly clustered table.
Otherwise, the planner may make poor choices of query plans.
</P
><P
> There is another way to cluster data. The
<TT
CLASS="COMMAND"
>CLUSTER</TT
> command reorders the original table using
the ordering of the index you specify. This can be slow
on large tables because the rows are fetched from the heap
in index order, and if the heap table is unordered, the
entries are on random pages, so there is one disk page
retrieved for every row moved. (<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> has a cache,
but the majority of a big table will not fit in the cache.)
The other way to cluster a table is to use
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TABLE <TT
CLASS="REPLACEABLE"
><I
>newtable</I
></TT
> AS
SELECT <TT
CLASS="REPLACEABLE"
><I
>columnlist</I
></TT
> FROM <TT
CLASS="REPLACEABLE"
><I
>table</I
></TT
> ORDER BY <TT
CLASS="REPLACEABLE"
><I
>columnlist</I
></TT
>;</PRE
><P>
which uses the <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> sorting code in
the <TT
CLASS="LITERAL"
>ORDER BY</TT
> 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
<TT
CLASS="COMMAND"
>ALTER TABLE ... RENAME</TT
>
to rename <TT
CLASS="REPLACEABLE"
><I
>newtable</I
></TT
> 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.
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN40700"
></A
><H2
>Examples</H2
><P
> Cluster the table <TT
CLASS="LITERAL"
>employees</TT
> on the basis of
its index <TT
CLASS="LITERAL"
>emp_ind</TT
>:
</P><PRE
CLASS="PROGRAMLISTING"
>CLUSTER emp_ind ON emp;</PRE
><P>
</P
><P
> Cluster the <TT
CLASS="LITERAL"
>employees</TT
> table using the same
index that was used before:
</P><PRE
CLASS="PROGRAMLISTING"
>CLUSTER emp;</PRE
><P>
</P
><P
> Cluster all tables in the database that have previously been clustered:
</P><PRE
CLASS="PROGRAMLISTING"
>CLUSTER;</PRE
><P>
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN40711"
></A
><H2
>Compatibility</H2
><P
> There is no <TT
CLASS="COMMAND"
>CLUSTER</TT
> statement in the SQL standard.
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN40715"
></A
><H2
>See Also</H2
><A
HREF="app-clusterdb.html"
><I
><I
>clusterdb</I
></I
></A
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-close.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-comment.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>CLOSE</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>COMMENT</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>