Developer and Text Tools

How to Choose SQL Indexes for Slow Queries (Column Order Explained)

Learn how composite indexes work, why equality columns come before sort and range columns, how to spot redundant indexes and test advice with EXPLAIN.

Saizul Amin
Saizul Amin3 Nov 2026 · 4 min read
How to Choose SQL Indexes for Slow Queries (Column Order Explained)

When a page gets slow as a table grows, the cause is usually a query that reads far more rows than it returns. The cure is often a single well-chosen index. Choosing it well is a skill, though, and a wrong index costs disk space and slows every insert without helping. This guide explains the reasoning behind composite indexes and how to use an advisor to speed it up.

How a B-tree index helps

A standard index keeps the values of its columns in sorted order with pointers to the rows. With an index on (status, created_at), all rows with status paid sit together, and within them the rows are already sorted by date. A query that wants the newest fifty paid orders can jump to the right place, read fifty entries in order and stop. Without the index, the database reads every order, keeps the paid ones and sorts them.

The column order rule

For one query, a common rule orders the index columns like this:

  1. Equality columns first. Columns compared with = or IN, including join columns.
  2. Sort columns next. The ORDER BY columns, or the GROUP BY columns.
  3. One range column last. A column compared with >, <, BETWEEN or LIKE 'abc%'.

Equality narrows the index to a sorted slice. Sort columns let the database read that slice in order. A range ends the ordered part, which is why only one range column is useful and why it goes last. Any later columns are checked row by row.

When an index cannot be used

  • LIKE '%text%' starts with a wildcard, so the sorted order does not help. Full-text or trigram indexes are the answer.
  • A column wrapped in a function, such as LOWER(email) = ..., needs an index on that expression.
  • Conditions joined only by OR usually need one index per column.
  • ORDER BY a ASC, b DESC needs an index with the same mix of directions to avoid a sort.

Redundant and unused indexes

An index on (customer_id) is almost always redundant next to one on (customer_id, status), because the longer index can serve the same lookups by its first column. Two indexes on the same columns in the same order are pure overhead. Indexes that no query uses still cost write time. Removing them is a quiet performance win, provided nothing else depends on them, such as a foreign key.

Using the advisor

The SQL Index Advisor applies these rules to your own schema and queries in the browser.

  1. Paste the CREATE TABLE and CREATE INDEX statements. Primary keys, unique constraints and inline keys are read as indexes too.
  2. Paste the queries you care about, separated by semicolons. Slow query log entries work well.
  3. Pick PostgreSQL, MySQL, SQLite or SQL Server so the statements use the right quoting and options.
  4. Read the suggested indexes. Each shows the reason, such as equality on status and ORDER BY created_at, and which queries it helps. For PostgreSQL and SQL Server, columns that are only selected can be added with INCLUDE to make a covering index.
  5. Check the redundant and unused lists, and open each query to see the parsed columns and notes.
  6. Download the migration script, which has the CREATE lines and commented DROP hints.

An example

An orders table has indexes on (customer_id), (customer_id, status) and (total). Four queries are pasted. For the recent paid orders page, the advisor proposes (status, created_at). For the customer screen it proposes (customer_id, created_at) and explains that the existing customer index covers only the equality part, so the database would still sort. It marks (customer_id) as a left prefix of (customer_id, status), lists (total) as unused by these queries, and warns that a search with LIKE '%rahman%' needs a different kind of index.

Always verify

An advisor that reads SQL text does not know your data. An index on a column with two distinct values may be useless, and a small table may be faster to scan. Before and after adding an index, run EXPLAIN (or EXPLAIN ANALYZE on a copy) and compare the plan and timing. On big production tables, create indexes with CONCURRENTLY in PostgreSQL or online options in MySQL to avoid long locks.

Summary

Put equality columns first, then sort columns, then one range column; avoid leading wildcards and functions on indexed columns; drop indexes that duplicate others. Let the advisor do the bookkeeping across many queries, then confirm each change with the planner.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages