Senior
What indexes do you know in PostgreSQL?
sobes.tech AI
Answer from AI
In PostgreSQL, there are several types of indexes, each suitable for different tasks:
- B-tree — the most common type of index, suitable for most comparison operations (=, <, >, BETWEEN).
- Hash — used for fast equality searches, but less versatile and less frequently used.
- GIN (Generalized Inverted Index) — effective for indexing arrays, JSONB, full-text search.
- GiST (Generalized Search Tree) — a versatile index for working with geometric data, full-text search, and other data types.
- SP-GiST — a specialized GiST for non-standard data structures, such as spatial data.
- BRIN (Block Range Index) — a compact index for very large tables with ordered data.
Example of creating a B-tree index:
CREATE INDEX idx_users_email ON users(email);
The choice of index type depends on the nature of the data and queries.