Imported upstream version 1.5
[manpages-zh.git] / raw / man7 / cluster.7
blob9dc1ab2aaddd723d8add4551b77351801f9e6452
1 .\\" auto-generated by docbook2man-spec $Revision: 1.1 $
2 .TH "CLUSTER" "7" "2003-11-02" "SQL - Language Statements" "SQL Commands"
3 .SH NAME
4 CLUSTER \- cluster a table according to an index
6 .SH SYNOPSIS
7 .sp
8 .nf
9 CLUSTER \fIindexname\fR ON \fItablename\fR
10 CLUSTER \fItablename\fR
11 CLUSTER
12 .sp
13 .fi
14 .SH "DESCRIPTION"
15 .PP
16 \fBCLUSTER\fR instructs PostgreSQL 
17 to cluster the table specified
18 by \fItablename\fR
19 based on the index specified by
20 \fIindexname\fR. The index must
21 already have been defined on 
22 \fItablename\fR.
23 .PP
24 When a table is clustered, it is physically reordered
25 based on the index information. Clustering is a one-time operation:
26 when the table is subsequently updated, the changes are
27 not clustered. That is, no attempt is made to store new or
28 updated rows according to their index order. If one wishes, one can
29 periodically recluster by issuing the command again.
30 .PP
31 When a table is clustered, PostgreSQL
32 remembers on which index it was clustered. The form
33 \fBCLUSTER \fItablename\fB\fR,
34 reclusters the table on the same index that it was clustered before.
35 .PP
36 \fBCLUSTER\fR without any parameter reclusters all the tables
37 in the
38 current database that the calling user owns, or all tables if called
39 by a superuser. (Never-clustered tables are not included.) This
40 form of \fBCLUSTER\fR cannot be called from inside a
41 transaction or function.
42 .PP
43 When a table is being clustered, an ACCESS
44 EXCLUSIVE lock is acquired on it. This prevents any other
45 database operations (both reads and writes) from operating on the
46 table until the \fBCLUSTER\fR is finished.
47 .SH "PARAMETERS"
48 .TP
49 \fB\fIindexname\fB\fR
50 The name of an index.
51 .TP
52 \fB\fItablename\fB\fR
53 The name (possibly schema-qualified) of a table.
54 .SH "NOTES"
55 .PP
56 In cases where you are accessing single rows randomly
57 within a table, the actual order of the data in the
58 table is unimportant. However, if you tend to access some
59 data more than others, and there is an index that groups
60 them together, you will benefit from using \fBCLUSTER\fR.
61 If you are requesting a range of indexed values from a table, or a
62 single indexed value that has multiple rows that match,
63 \fBCLUSTER\fR will help because once the index identifies the
64 heap page for the first row that matches, all other rows
65 that match are probably already on the same heap page,
66 and so you save disk accesses and speed up the query.
67 .PP
68 During the cluster operation, a temporary copy of the table is created
69 that contains the table data in the index order. Temporary copies of
70 each index on the table are created as well. Therefore, you need free
71 space on disk at least equal to the sum of the table size and the index
72 sizes.
73 .PP
74 Because \fBCLUSTER\fR remembers the clustering information,
75 one can cluster the tables one wants clustered manually the first time, and
76 setup a timed event similar to \fBVACUUM\fR so that the tables
77 are periodically reclustered.
78 .PP
79 Because the planner records statistics about the ordering of tables, it
80 is advisable to run \fBANALYZE\fR on the newly clustered
81 table. Otherwise, the planner may make poor choices of query plans.
82 .PP
83 There is another way to cluster data. The
84 \fBCLUSTER\fR command reorders the original table using
85 the ordering of the index you specify. This can be slow
86 on large tables because the rows are fetched from the heap
87 in index order, and if the heap table is unordered, the
88 entries are on random pages, so there is one disk page
89 retrieved for every row moved. (PostgreSQL has a cache,
90 but the majority of a big table will not fit in the cache.)
91 The other way to cluster a table is to use
92 .sp
93 .nf
94 CREATE TABLE \fInewtable\fR AS
95     SELECT \fIcolumnlist\fR FROM \fItable\fR ORDER BY \fIcolumnlist\fR;
96 .sp
97 .fi
98 which uses the PostgreSQL sorting code in 
99 the ORDER BY clause to create the desired order; this is usually much
100 faster than an index scan for
101 unordered data. You then drop the old table, use
102 \fBALTER TABLE ... RENAME\fR
103 to rename \fInewtable\fR to the old name, and
104 recreate the table's indexes. However, this approach does not preserve
105 OIDs, constraints, foreign key relationships, granted privileges, and
106 other ancillary properties of the table --- all such items must be
107 manually recreated.
108 .SH "EXAMPLES"
110 Cluster the table employees on the basis of
111 its index emp_ind:
114 CLUSTER emp_ind ON emp;
118 Cluster the employees relation using the same
119 index that was used before:
122 CLUSTER emp;
126 Cluster all the tables on the database that have previously been clustered:
129 CLUSTER;
132 .SH "COMPATIBILITY"
134 There is no \fBCLUSTER\fR statement in the SQL standard.
135 .SH "SEE ALSO"
136 clusterdb [\fBclusterdb\fR(1)]