SQL Index Generator

Generate CREATE INDEX statements — build single-column, composite, unique, and full-text indexes for MySQL and PostgreSQL.

About SQL Indexes

Database indexes are data structures that improve the speed of data retrieval operations at the cost of additional storage and slightly slower write operations. A well-indexed database can execute queries 100-10,000x faster than an unindexed one. Indexes work like a book's index — instead of scanning every page (every row), the database jumps directly to the relevant location.

Index types: A regular INDEX speeds up lookups on a column. A UNIQUE index does the same but also enforces uniqueness (no duplicate values). A composite index covers multiple columns and speeds up queries that filter or sort on those columns together. FULLTEXT indexes enable fast text search operations (MySQL's MATCH/AGAINST, PostgreSQL's tsvector). A COVERING INDEX includes all columns needed by a query in the index itself, eliminating the need to look up the actual table rows.

Best practices: Index all columns used in WHERE, JOIN ON, and ORDER BY clauses. Avoid over-indexing — each index slows down INSERT/UPDATE/DELETE. Use composite indexes for multi-column WHERE clauses, with the most selective column first. Use EXPLAIN to see if your indexes are being used.

FAQ

How do I know which columns to index?
Run EXPLAIN (MySQL/PostgreSQL) on your slow queries. Look for "full table scan" warnings. Index columns that appear in: WHERE conditions, JOIN ON clauses, ORDER BY/GROUP BY columns, and foreign key columns. Columns with high cardinality (many distinct values like email) benefit more from indexing than low-cardinality columns (like boolean active flags).
What is a composite index and when should I use it?
A composite index covers multiple columns, e.g., INDEX ON orders(user_id, created_at). It's used when queries filter on multiple columns together. The order matters — the leftmost column is used first. A composite index on (user_id, status) helps queries filtering by user_id alone, or user_id AND status together, but not status alone.