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

11.2. インデックスの種類 #

<title>Index Types</title>

<productname>PostgreSQL</productname> provides several index types: B-tree, Hash, GiST, SP-GiST, GIN, BRIN, and the extension <link linkend="bloom">bloom</link>. Each index type uses a different algorithm that is best suited to different types of queries. By default, the <link linkend="sql-createindex"><command>CREATE INDEX</command></link> command creates B-tree indexes, which fit the most common situations. The other index types are selected by writing the keyword <literal>USING</literal> followed by the index type name. For example, to create a Hash index: PostgreSQLでは、B-tree、Hash、GiST、SP-GiST、GIN、BRIN、そしてブルーム拡張といった複数の種類のインデックスを使用可能です。 インデックスの各種類は、異なる種類の問い合わせに最も適した、異なるアルゴリズムを使用します。 デフォルトでCREATE INDEXコマンドは、B-treeインデックスを作成し、それは最も一般的な状況に適合します。 他のインデックスの種類は、キーワードUSINGの後にインデックス種類名を記述することで選択されます。 例えば、ハッシュインデックスを作成するには、次のようにします。

CREATE INDEX name ON table USING HASH (column);

11.2.1. B-Tree #

B-trees can handle equality and range queries on data that can be sorted into some ordering. In particular, the <productname>PostgreSQL</productname> query planner will consider using a B-tree index whenever an indexed column is involved in a comparison using one of these operators: B-treeインデックスは、ある順番でソート可能なデータに対する等価性や範囲を問い合わせることを扱うことができます。 具体的には、PostgreSQLの問い合わせプランナは、インデックスの付いた列を次の演算子を使用して比較する場合に、B-treeインデックスの使用を検討します。

<   <=   =   >=   >

Constructs equivalent to combinations of these operators, such as <literal>BETWEEN</literal> and <literal>IN</literal>, can also be implemented with a B-tree index search. Also, an <literal>IS NULL</literal> or <literal>IS NOT NULL</literal> condition on an index column can be used with a B-tree index. また、BETWEENINなどのこれらの演算子の組み合わせと等価な式もB-treeインデックス検索で実装することができます。 インデックスの付いた列に対するIS NULLIS NOT NULLでもB-treeインデックスを使用することができます。

The optimizer can also use a B-tree index for queries involving the pattern matching operators <literal>LIKE</literal> and <literal>~</literal> <emphasis>if</emphasis> the pattern is a constant and is anchored to the beginning of the string &mdash; for example, <literal>col LIKE 'foo%'</literal> or <literal>col ~ '^foo'</literal>, but not <literal>col LIKE '%bar'</literal>. However, if your database does not use the C locale you will need to create the index with a special operator class to support indexing of pattern-matching queries; see <xref linkend="indexes-opclass"/> below. It is also possible to use B-tree indexes for <literal>ILIKE</literal> and <literal>~*</literal>, but only if the pattern starts with non-alphabetic characters, i.e., characters that are not affected by upper/lower case conversion. オプティマイザは、パターンマッチ演算子LIKE~を含む問い合わせでも、そのパターンが定数であり、先頭文字列を指定しているのであればB-treeインデックスを使用することができます。 例えば、col LIKE 'foo%'またはcol ~ '^foo'では使用されますが、col LIKE '%bar'では使用されません。 しかし、データベースがCロケールを使用していない場合、パターンマッチ問い合わせのインデックス付けをサポートする特別な演算子クラスでインデックスを作成しなければなりません。 後述の11.10を参照してください。 なお、ILIKE~*でもB-treeインデックスを使用することができますが、パターンが英字以外の文字、つまり、大文字小文字の違いの影響がない文字で始まる場合のみです。

B-tree indexes can also be used to retrieve data in sorted order. This is not always faster than a simple scan and sort, but it is often helpful. B-treeインデックスをソートされた順序でデータを受けとるために使用することもできます。 これは常に単純なスキャンとソート処理より高速になるものではありませんが、よく役に立つことがあります。

11.2.2. Hash #

Hash indexes store a 32-bit hash code derived from the value of the indexed column. Hence, such indexes can only handle simple equality comparisons. The query planner will consider using a hash index whenever an indexed column is involved in a comparison using the equal operator: ハッシュインデックスは、インデックスの付いた列の値から算出される32ビットのハッシュコードを格納します。 したがって、ハッシュインデックスは単純な等価性比較のみを扱うことができます。 問い合わせプランナでは、インデックスの付いた列を以下の等号演算子を使用して比較する場合は常に、ハッシュインデックスの使用を検討します。

=

11.2.3. GiST #

GiST indexes are not a single kind of index, but rather an infrastructure within which many different indexing strategies can be implemented. Accordingly, the particular operators with which a GiST index can be used vary depending on the indexing strategy (the <firstterm>operator class</firstterm>). As an example, the standard distribution of <productname>PostgreSQL</productname> includes GiST operator classes for several two-dimensional geometric data types, which support indexed queries using these operators: GiSTインデックスは単一種類のインデックスではなく、多くの異なるインデックス戦略を実装することができる基盤です。 したがって、具体的なGiSTインデックスで使用できる演算子はインデックス戦略(演算子クラス)によって異なります。 例えば、PostgreSQLの標準配布物には、複数の二次元幾何データ型用のGiST演算子クラスが含まれており、以下の演算子を使用してインデックス付けされた問い合わせをサポートします。

<<   &<   &>   >>   <<|   &<|   |&>   |>>   @>   <@   ~=   &&

(See <xref linkend="functions-geometry"/> for the meaning of these operators.) The GiST operator classes included in the standard distribution are documented in <xref linkend="gist-builtin-opclasses-table"/>. Many other GiST operator classes are available in the <literal>contrib</literal> collection or as separate projects. For more information see <xref linkend="gist"/>. (これらの演算子の意味については9.11を参照してください。) 標準配布物に含まれるGiST演算子クラスは表 68.1に記載されています。 他の多くのGiST演算子クラスがcontrib群や別のプロジェクトとして利用可能です。 詳細は第68章を参照してください。

GiST indexes are also capable of optimizing <quote>nearest-neighbor</quote> searches, such as GiSTインデックスは以下のような最近傍検索を最適化する機能も持ちます。

SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;

which finds the ten places closest to a given target point. The ability to do this is again dependent on the particular operator class being used. In <xref linkend="gist-builtin-opclasses-table"/>, operators that can be used in this way are listed in the column <quote>Ordering Operators</quote>. これは指定された対象地点に最も近い10箇所を見つけ出します。 この場合も、これができるかどうかは使用される特定の演算子クラスに依存します。 このように利用できる演算子は表 68.1順序付け演算子列に表示されています。

11.2.4. SP-GiST #

SP-GiST indexes, like GiST indexes, offer an infrastructure that supports various kinds of searches. SP-GiST permits implementation of a wide range of different non-balanced disk-based data structures, such as quadtrees, k-d trees, and radix trees (tries). As an example, the standard distribution of <productname>PostgreSQL</productname> includes SP-GiST operator classes for two-dimensional points, which support indexed queries using these operators: SP-GiSTインデックスは、GiSTインデックスと同様に様々な種類の検索を支援する基盤を提供します。 SP-GiSTインデックスは広域な異なる不均衡でディスクベースのデータ構造、つまり、四分木,kd木、基数木のような実装を認めます。 例えば、PostgreSQL標準配布物には、以下の演算子を使用する問い合わせに対するインデックスをサポートする2次元の点用のSP-GiST用の演算子クラスが含まれています。

<<   >>   ~=   <@   <<|   |>>

(See <xref linkend="functions-geometry"/> for the meaning of these operators.) The SP-GiST operator classes included in the standard distribution are documented in <xref linkend="spgist-builtin-opclasses-table"/>. For more information see <xref linkend="spgist"/>. (これらの演算子の意味については9.11を参照してください。) 標準配布物に含まれるSP-GiST演算子クラスは表 69.1に記載されています。 詳細は 第69章を参照してください。

Like GiST, SP-GiST supports <quote>nearest-neighbor</quote> searches. For SP-GiST operator classes that support distance ordering, the corresponding operator is listed in the <quote>Ordering Operators</quote> column in <xref linkend="spgist-builtin-opclasses-table"/>. GiSTと同様に、SP-GiSTは最近傍検索をサポートします。 距離の順序付けをサポートするSP-GiST演算子クラスの場合、対応する演算子は表 69.1順序付け演算子列に一覧表示されます。

11.2.5. GIN #

GIN indexes are <quote>inverted indexes</quote> which are appropriate for data values that contain multiple component values, such as arrays. An inverted index contains a separate entry for each component value, and can efficiently handle queries that test for the presence of specific component values. GINは転置インデックスであり、配列などのように複数の要素を持つデータ値に適しています。 転置インデックスは各要素値に対して別々のエントリを持っており、特定の要素値の存在について検査する問い合わせを効率的に処理できます。

Like GiST and SP-GiST, GIN can support many different user-defined indexing strategies, and the particular operators with which a GIN index can be used vary depending on the indexing strategy. As an example, the standard distribution of <productname>PostgreSQL</productname> includes a GIN operator class for arrays, which supports indexed queries using these operators: GiSTやSP-GiST同様、GINも多くの異なるユーザ定義のインデックス戦略を持つことができ、GINが使用できる具体的な演算子はインデックス戦略によって変化します。 例えば、PostgreSQL標準配布物には、配列用のGIN演算子クラスが含まれており、これらは、以下の演算子を使用するインデックスによる問い合わせをサポートします。

<@   @>   =   &&

(See <xref linkend="functions-array"/> for the meaning of these operators.) The GIN operator classes included in the standard distribution are documented in <xref linkend="gin-builtin-opclasses-table"/>. Many other GIN operator classes are available in the <literal>contrib</literal> collection or as separate projects. For more information see <xref linkend="gin"/>. (これらの演算子の意味については9.19を参照してください。) 標準配布物に含まれるGIN演算子クラスは表 70.1に記載されています。 他の多くのGIN演算子クラスはcontrib群または別のプロジェクトで利用可能です。 詳細は第70章を参照してください。

11.2.6. BRIN #

BRIN indexes (a shorthand for Block Range INdexes) store summaries about the values stored in consecutive physical block ranges of a table. Thus, they are most effective for columns whose values are well-correlated with the physical order of the table rows. Like GiST, SP-GiST and GIN, BRIN can support many different indexing strategies, and the particular operators with which a BRIN index can be used vary depending on the indexing strategy. For data types that have a linear sort order, the indexed data corresponds to the minimum and maximum values of the values in the column for each block range. This supports indexed queries using these operators: BRINインデックス(ブロックレンジインデックス(Block Range INdex)を縮めたものです)はテーブルの連続的な物理ブロックの範囲に格納された値についての要約を格納します。 したがって、これらの値は、テーブル行の物理的な順序とよく相関している列に最も効果的です。 GiST、SP-GiST、GINと同じように、BRINは多くの異なるインデックス戦略をサポートし、BRINインデックスが使用できる具体的な演算子はインデックス戦略によって変化します。 線形のソート順を持つデータ型では、インデックス付けされたデータは各ブロックレンジの列の中の値の最小値と最大値に対応しています。 これは以下の演算子を使用したインデックスによる問い合わせをサポートします。

<   <=   =   >=   >

The BRIN operator classes included in the standard distribution are documented in <xref linkend="brin-builtin-opclasses-table"/>. For more information see <xref linkend="brin"/>. 標準配布物に含まれるBRIN演算子クラスは表 71.1に記載されています。 詳細は第71章を参照してください。