TASK
Design database indexes for performance
Indexes speed up reads but slow down writes. Learn B-tree, hash, composite, and partial indexes, and when to use each.
- 1
Index types: B-tree (default, supports range and equality), Hash (equality only, faster), GIN/GiST (full-text, geometry), Composite (multiple columns, order matters). Indexes speed up WHERE, JOIN, ORDER BY, and GROUP BY but slow down INSERT/UPDATE/DELETE (index maintenance). Index selectively — every index has a cost.
- 2 SQL Formatter →
Format CREATE INDEX statements. For composite indexes, put equality conditions first, then range conditions, then sort columns. Example: CREATE INDEX ON orders (customer_id, status, created_at) for WHERE customer_id = ? AND status = ? ORDER BY created_at.
Frequently asked questions
- How do I choose composite index column order?
- Put the most selective column first (highest cardinality). For WHERE clauses, put equality conditions before range conditions. For a query WHERE a = 1 AND b > 2 ORDER BY c, use (a, b, c). The leading columns must match the query — a index on (b, a) cannot serve WHERE a = 1 efficiently.
- When should I use a partial index?
- When you query a subset of rows frequently. CREATE INDEX ON orders (customer_id) WHERE status = 'active' indexes only active orders — smaller, faster, less write overhead. Use for common queries on a small subset (active users, unpaid invoices, recent records).
Workflows that solve this
Learn more
Want to understand why?
How to Analyze a Query Plan
An advanced-level guide covering query-plan, database, optimization. Includes step-by-step instructions, common errors, troubleshooting tips, and security considerations for query-plan. TUTORIAL
How to Choose a Consistency Level
An advanced-level guide covering consistency, database, distributed. Includes step-by-step instructions, common errors, troubleshooting tips, and security considerations for consistency. TUTORIAL
ACID Property
ACID is the four guarantees of transactions: atomicity, consistency, isolation, and durability, ensuring reliable transaction processing. GLOSSARY
Anti Join
An anti join returns rows from the outer table that have no match in the inner table, used to optimize NOT EXISTS / NOT IN subqueries. GLOSSARY
Related tools
SQL Formatter
Beautify or minify SQL queries online. Uppercase keywords, indent clauses, or compress to a single line. Free, private, and 100% client-side. TOOL
JSON Validator
Validate JSON and pinpoint syntax errors by line and column. Free, private, and 100% client-side. TOOL
Regex Tester
Test regular expressions with match highlighting, capture groups, and replace preview. Free, private, and 100% client-side. TOOL