🐘 PostgreSQL
Beginner
What are indexes in PostgreSQL?
Answer
An index is a data structure that speeds up query lookups at the cost of additional storage and slower writes. PostgreSQL automatically creates indexes for PRIMARY KEY and UNIQUE constraints. Create manually: CREATE INDEX idx_users_email ON users(email);. The default index type is B-tree — suitable for equality and range queries, sorting, and most use cases. Other types: Hash (equality only), GIN (full-text search, arrays, JSONB), GiST (geometric, full-text), BRIN (very large tables with naturally ordered data). Indexes slow down INSERT/UPDATE/DELETE operations. Use EXPLAIN ANALYZE to verify an index is being used. List indexes: \di in psql or query pg_indexes.