バージョンごとのドキュメント一覧

CLUSTER

CLUSTER <refpurpose>cluster a table according to an index</refpurpose> — インデックスに従ってテーブルをクラスタ化する

概要

CLUSTER [VERBOSE] table_name [ USING index_name ]
CLUSTER ( option [, ...] ) table_name [ USING index_name ]
CLUSTER [VERBOSE]

where option can be one of:

    VERBOSE [ boolean ]

説明

<title>Description</title>

<command>CLUSTER</command> instructs <productname>PostgreSQL</productname> to cluster the table specified by <replaceable class="parameter">table_name</replaceable> based on the index specified by <replaceable class="parameter">index_name</replaceable>. The index must already have been defined on <replaceable class="parameter">table_name</replaceable>. CLUSTERは、index_nameで指定されたインデックスに基づき、table_nameで指定されたテーブルをクラスタ化するように、PostgreSQLに指示します。 このインデックスは前もってtable_name上に定義されていなければなりません。

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. Also, setting the table's <literal>fillfactor</literal> storage parameter to less than 100% can aid in preserving cluster ordering during updates, since updated rows are kept on the same page if enough space is available there.) テーブルがクラスタ化されると、それぞれのテーブルはインデックス情報に基づいて物理的に並べ直されます。 クラスタ化は、1回限りの操作です。 クラスタ化後にテーブルが更新されても、その変更はクラスタ化されません。 つまり、新規に追加された行や更新された行は、インデックス順には保管されません。 (インデックス順に保管したい場合は、コマンドを再度入力し、定期的に再クラスタ化を行います。 また、更新される行は十分な領域が利用可能ならば同一ページ内に保持されますので、テーブルのfillfactor格納パラメータを100%より小さく設定することで、更新処理中のクラスタ順序付けを保護するのに役に立ちます。)

When a table is clustered, <productname>PostgreSQL</productname> remembers which index it was clustered by. The form <command>CLUSTER <replaceable class="parameter">table_name</replaceable></command> reclusters the table using the same index as before. You can also use the <literal>CLUSTER</literal> or <literal>SET WITHOUT CLUSTER</literal> forms of <link linkend="sql-altertable"><command>ALTER TABLE</command></link> to set the index to be used for future cluster operations, or to clear any previous setting. テーブルがクラスタ化されると、PostgreSQLはクラスタ化に使用されたインデックスを記録します。 CLUSTER table_nameという構文によって、以前と同じインデックスを使用してテーブルを再クラスタ化します。 またALTER TABLECLUSTERもしくはSET WITHOUT CLUSTER構文を使用して、将来のクラスタ化操作で使用するインデックスを設定したり、過去の設定を取り消すことができます。

<command>CLUSTER</command> without a <replaceable class="parameter">table_name</replaceable> reclusters all the previously-clustered tables in the current database that the calling user owns, or all such tables if called by a superuser. This form of <command>CLUSTER</command> cannot be executed inside a transaction block. table_nameを指定しないでCLUSTERを実行した場合、現在のデータベース内の以前にクラスタ化されたテーブルのうち、呼び出したユーザが所有するすべてのテーブルを(スーパーユーザが実行する場合はすべてのテーブルを)再クラスタ化します。 この形式のCLUSTERを、トランザクションブロック内では実行できません。

When a table is being clustered, an <literal>ACCESS EXCLUSIVE</literal> lock is acquired on it. This prevents any other database operations (both reads and writes) from operating on the table until the <command>CLUSTER</command> is finished. クラスタ化を行っているテーブルでは、ACCESS EXCLUSIVEロックが獲得されています。 これにより、CLUSTERが終わるまで、そのテーブルに対するデータベース操作(読み書き両方)はできません。

パラメータ

<title>Parameters</title>
table_name

The name (possibly schema-qualified) of a table. テーブルの名前です(スキーマ修飾名も可)。

index_name

The name of an index. インデックスの名前です。

VERBOSE

Prints a progress report as each table is clustered. 各テーブルのクラスタ化を行う時に進行状況報告を出力します。

boolean

Specifies whether the selected option should be turned on or off. You can write <literal>TRUE</literal>, <literal>ON</literal>, or <literal>1</literal> to enable the option, and <literal>FALSE</literal>, <literal>OFF</literal>, or <literal>0</literal> to disable it. The <replaceable class="parameter">boolean</replaceable> value can also be omitted, in which case <literal>TRUE</literal> is assumed. 選択したオプションをオンにするかオフにするか指定します。 オプションを有効にする場合にはTRUEONまたは1と書くことができ、無効にする場合に>はFALSEOFFまたは0と書くことができます。 booleanの値は省略することもでき、その場合にはTRUEとみなされます。

注釈

<title>Notes</title>

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 <command>CLUSTER</command>. If you are requesting a range of indexed values from a table, or a single indexed value that has multiple rows that match, <command>CLUSTER</command> will help because once the index identifies the table page for the first row that matches, all other rows that match are probably already on the same table page, and so you save disk accesses and speed up the query. テーブル内の個々の行にランダムにアクセスする場合、テーブル内のデータの順序は重要でありません。 しかし、テーブル内の特定のデータにアクセスが集中していて、それらのデータをひとまとめにしているインデックスが存在する時は、CLUSTERによる利益を享受できます。 テーブルからインデックスの値の範囲や、一致する複数の行を保有する1つのインデックスの値を要求する場合、CLUSTERが役に立ちます。 一度インデックスが一致する最初の行に対するテーブルページを認識すると、一致する他の全ての行も同じテーブルページに存在する可能性が高いので、ディスクアクセスを減らして問い合わせ処理の速度を向上することができるからです。

<command>CLUSTER</command> can re-sort the table using either an index scan on the specified index, or (if the index is a b-tree) a sequential scan followed by sorting. It will attempt to choose the method that will be faster, based on planner cost parameters and available statistical information. CLUSTERは、指定されたインデックスによるインデックススキャン、または(インデックスがB-Treeの場合)シーケンシャルスキャン後のソートのいずれかを用いて、テーブルを再ソートすることができます。 プランナのコストパラメータと利用可能な統計情報に基づき、より高速な方式の選択を試みます。

When an index scan is used, 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. インデックススキャンが使用される場合、インデックス順にテーブルデータを並べた、テーブルの一時コピーが作成されます。 同様に、テーブルの各インデックスの一時コピーも作成されます。 したがって、ディスクには、少なくともテーブルとインデックスの合計サイズと同じ容量の空き領域が必要です。

When a sequential scan and sort is used, a temporary sort file is also created, so that the peak temporary space requirement is as much as double the table size, plus the index sizes. This method is often faster than the index scan method, but if the disk space requirement is intolerable, you can disable this choice by temporarily setting <xref linkend="guc-enable-sort"/> to <literal>off</literal>. シーケンシャルスキャンとソートが使用される場合も一時的なソートファイルが作成されます。 一時的に必要となるサイズの最大値はテーブルサイズの倍のサイズにインデックスサイズを加えた値となります。 この方式はインデックススキャンより高速になることが多いのですが、必要なディスク容量に耐えられない場合は、一時的にenable_sortoffにすることで、この方式を無効にすることができます。

It is advisable to set <xref linkend="guc-maintenance-work-mem"/> to a reasonably large value (but not more than the amount of RAM you can dedicate to the <command>CLUSTER</command> operation) before clustering. クラスタ処理の前にmaintenance_work_memを程良く大きな値に設定することを勧めます。 (しかしCLUSTER操作専用に割り当てられるRAMの容量を超えないようにしてください。)

Because the planner records statistics about the ordering of tables, it is advisable to run <link linkend="sql-analyze"><command>ANALYZE</command></link> on the newly clustered table. Otherwise, the planner might make poor choices of query plans. プランナはテーブルの順序付けに関する統計情報を記録しているため、新しくクラスタ化されたテーブルでは、ANALYZEを実行することが推奨されます。 そうしないと、プランナが問い合わせ計画を適切に選択できない可能性があります。

Because <command>CLUSTER</command> remembers which indexes are clustered, one can cluster the tables one wants clustered manually the first time, then set up a periodic maintenance script that executes <command>CLUSTER</command> without any parameters, so that the desired tables are periodically reclustered. CLUSTERはどのインデックスでクラスタ化したかを記録していますので、対象のテーブルを定期的に再クラスタ化できるように、最初にクラスタ化したいテーブルを手作業でクラスタ化し、その後にパラメータをまったく持たないCLUSTERを実行する定期的な保守用スクリプトを設定することができます。

Each backend running <command>CLUSTER</command> will report its progress in the <structname>pg_stat_progress_cluster</structname> view. See <xref linkend="cluster-progress-reporting"/> for details. CLUSTERを実行している各バックエンドはその進捗をpg_stat_progress_clusterビューで報告します。 詳細は28.4.2を参照してください。

Clustering a partitioned table clusters each of its partitions using the partition of the specified partitioned index. When clustering a partitioned table, the index may not be omitted. <command>CLUSTER</command> on a partitioned table cannot be executed inside a transaction block. パーティションテーブルをクラスタ化すると、指定したパーティションインデックスのパーティションを使用して各パーティションがクラスタ化されます。 パーティションテーブルをクラスタ化する場合は、インデックスを省略できません。 パーティションテーブルのCLUSTERは、トランザクションブロック内では実行できません。

<title>Examples</title>

Cluster the table <literal>employees</literal> on the basis of its index <literal>employees_ind</literal>: インデックスemployees_indに基づいて、テーブルempをクラスタ化します。

CLUSTER employees USING employees_ind;

Cluster the <literal>employees</literal> table using the same index that was used before: 以前に使用したインデックスを使用して、テーブルemployeesをクラスタ化します。

CLUSTER employees;

Cluster all tables in the database that have previously been clustered: データベース内の、以前にクラスタ化されたテーブルを全てクラスタ化します。

CLUSTER;

互換性

<title>Compatibility</title>

There is no <command>CLUSTER</command> statement in the SQL standard. 標準SQLにはCLUSTER文はありません。

The syntax

CLUSTER index_name ON table_name

is also supported for compatibility with pre-8.3 <productname>PostgreSQL</productname> versions. という構文も、8.3より前のバージョンのPostgreSQLとの互換性のためサポートされます。

関連項目

<title>See Also</title> clusterdb, 28.4.2