Indexes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Overview

PostgreSQL provides several index types: B-tree, Hash, GiST and GIN. Each
index type uses a different algorithm that is best suited to different types of
queries. By default, the CREATE INDEX command creates B-tree indexes, which
fit the most common situations.

When should indexes be avoided?


Although indexes are intended to enhance a database's performance, there
are times when they should be avoided. The following guidelines indicate
when the use of an index should be reconsidered:

 Indexes should not be used on small tables.


 Tables that have frequent, large batch update or insert operations.
 Indexes should not be used on columns that contain a high number of
NULL values.
 Columns that are frequently manipulated should not be indexed.

B-tree indexes

B-trees can handle equality and range queries on data that can be sorted into
some ordering. In particular, the PostgreSQL query planner will consider using
a B-tree index whenever an indexed column is involved in a comparison using
one of these operators:

< , <= , = , >= , >

Constructs equivalent to combinations of these operators, such as BETWEEN


and IN, can also be implemented with a B-tree index search. Also, an IS
NULL or IS NOT NULL condition on an index column can be used with a B-tree
index. The optimizer can also use a B-tree index for queries involving the
pattern matching operators LIKE
Hash indexes

Hash 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 = operator. The following command is used to create a
hash index:

CREATE INDEX name ON table USING hash (column);

Caution :-

Hash index operations are not presently WAL-logged, so hash indexes might
need to be rebuilt with REINDEX after a database crash if there were unwritten
changes. Also, changes to hash indexes are not replicated over streaming or
file-based replication after the initial base backup, so they give wrong answers
to queries that subsequently use them. For these reasons, hash index use is
presently discouraged.

GIN Indexes

Generalized Inverted Indexes (GIN) are useful when an index must map many
values to one row, whereas B-Tree indexes are optimized for when a row has a
single key value. GINs are good for indexing array values as well as for
implementing full-text search.

<<, &< , &>, >>, <<|, &<|, |&> , |>>, @> , <@ , ~= ,&&.

GiST Indexes

Generalized Search Tree (GiST) indexes allow you to build general balanced
tree structures, and can be used for operations beyond equality and range
comparisons. They are used to index the geometric data types, as well as full-
text search.

You might also like