Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Database indexes make queries faster by helping the database locate matching rows without checking every row in a table. They do not make every query faster: the benefit depends on what the query asks for, how many rows match, and whether using the index costs less than reading the table directly.

What a database index does

An index is an auxiliary lookup structure built from selected table values. It organizes keys so the database can search for likely matches and then retrieve the corresponding rows. PostgreSQL’s documentation puts it simply: “An index allows the database server to find and retrieve specific rows much faster than it could do without an index.” See PostgreSQL 18: Introduction.

Without a useful index, the engine may perform a sequential scan: inspect rows one by one to test whether they meet a condition. With a suitable index, it can navigate to the relevant keys and avoid examining much of the table. The improvement comes from reducing how much data the query must visit—not from making all database work inherently faster.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why B-tree indexes are useful

A common general-purpose index is a B-tree. Its keys are kept in sorted order, which lets a database search for a particular value or a range of values rather than examine all keys sequentially. PostgreSQL documents B-tree support for equality and range comparisons, while MySQL notes that most of its common index types use B-trees. See PostgreSQL 18: Index Types and MySQL 8.4: How MySQL Uses Indexes.

For example, a query asking for a customer with a particular ID can often use an index on that ID to locate a small number of matching rows. A query asking for a range of dates may also benefit from an index whose ordered keys match the date condition. The index helps only when its keys and structure fit the query.

Which queries can benefit

Indexes are useful for more than simple lookups. Depending on the database engine and index definition, an index can help filter rows, find join matches, supply rows in a requested order, support grouping, or provide all the values a query needs without visiting the table itself. PostgreSQL and MySQL describe these uses in their index documentation: PostgreSQL 18: Introduction and MySQL 8.4: How MySQL Uses Indexes.

Filtering and joins

An index on a column used in a WHERE condition can help find matching rows. Indexes on join columns can help the engine locate related rows, although the best plan depends on the query and data.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Ordering and grouping

An index whose key order fits an ORDER BY or grouping operation may let the engine use that order instead of sorting all qualifying rows separately. Whether this is worthwhile depends on the requested order, filters, and plan chosen by the optimizer.

Covering indexes

If an index contains every column a query needs, the database may be able to answer it from the index entries alone. This is often called a covering index. It can avoid extra table-row lookups, but the index must still suit the query and its cost.

Composite indexes and column order

A composite index stores keys for more than one column in a defined order. That order affects which queries can use it. MySQL documents that lookups can use the leftmost prefix of a multi-column index; an index on (last_name, first_name), for example, can support lookups beginning with last_name, while it is not equivalent to an index beginning with first_name. See MySQL 8.4: How MySQL Uses Indexes.

Why an index can be slower or ignored

An index is not automatically the cheapest route. If a query returns a large fraction of the table, the database may need many index lookups followed by many row fetches. Reading the table sequentially can then cost less than following the index. The trade-off depends on the engine, storage, data distribution, and query.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Indexes also require storage and ongoing maintenance. Inserts, updates, and deletes can require changes to relevant index entries, adding work to write operations. Adding indexes may therefore improve some reads while increasing storage use and write costs. See PostgreSQL 18: Introduction and MySQL 8.4: How MySQL Uses Indexes.

The optimizer selects a plan using estimates, including estimates of how many rows a condition will match and the relative cost of available operations. It may choose a sequential scan even when an applicable index exists if that is estimated to be more efficient. Stale or inaccurate statistics can also make its estimates less useful. PostgreSQL explains how to inspect plans and maintain statistics in Using EXPLAIN and Routine Vacuuming.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How to check whether an index helps

  1. Inspect the planned operations. Run EXPLAIN for the query and check whether the plan uses an index scan or a sequential scan. The plan shows what the optimizer expects to do; it is not, by itself, a measurement of actual runtime.
  2. Compare with execution behavior. In PostgreSQL, EXPLAIN ANALYZE runs the query and reports actual execution details alongside the plan. Use care with queries that change data, because analyzing them executes them. PostgreSQL’s EXPLAIN documentation also cautions that costs and estimates depend on data and platform.
  3. Check statistics. PostgreSQL’s ANALYZE updates statistics used by the planner to estimate row counts and costs. For ordinary maintenance, PostgreSQL can also collect statistics through autovacuum; details are in Routine Vacuuming.
  4. Test representative workloads. Compare the query’s execution behavior with realistic data and the surrounding read-and-write workload. An index that helps a selective test lookup may not benefit a production query that returns many rows, and its maintenance has a cost.

There is no universal speedup percentage: the gain varies with the query, table, index, database engine, and workload. Plan estimates are not a substitute for measuring the cases that matter.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.