CREATE INDEX idx_products_sku ON products (sku);B-tree is the default and best starting point for many OLTP queries.
Use PostgreSQL B-tree, GIN, GiST, BRIN, partial indexes, expression indexes, and concurrent builds effectively.
Choose the right index method for the workload.
CREATE INDEX idx_products_sku ON products (sku);B-tree is the default and best starting point for many OLTP queries.
CREATE INDEX idx_events_payload_gin ON events USING GIN (payload jsonb_path_ops);GIN is often used for composite or document-like structures such as JSONB and arrays.
Use BRIN when physical order correlates with query ranges.
CREATE INDEX idx_logs_created_at_brin ON logs USING BRIN (created_at);BRIN can be compact and effective for very large time-series or append-only tables.
Index the result of an expression used in predicates.
CREATE INDEX idx_users_lower_email ON users ((lower(email)));Expression indexes help when the query applies a function such as `lower(email)`.
CREATE INDEX idx_orders_unshipped ON orders (created_at) WHERE shipped_at IS NULL;Partial indexes reduce size and write cost when the workload focuses on a subset of rows.
CREATE INDEX CONCURRENTLY idx_orders_user_id ON orders (user_id);Concurrent index builds are useful in production migrations when minimizing write blocking matters.
Verify that the planner uses the index the way you expect.