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

A database index can speed up a query by giving the database a searchable route to matching rows, instead of making it inspect every row in a table. That route helps only when it fits the query and costs less than a scan; indexes also consume storage and add work when data changes.

How an index reduces the work of a query

A table scan checks table rows to find those that satisfy a condition. An index is a separate structure containing searchable key information and a way to reach the rows associated with those keys. When a query can use a suitable index, the database navigates the index to identify candidate rows rather than checking the entire table. PostgreSQL describes the purpose directly: an index lets the server find specific rows much faster than it could without one (PostgreSQL: Indexes).

This reduces the work required to find rows; it does not guarantee constant-time lookup or eliminate disk access. The amount of work depends on factors such as the index type, table size, data distribution, cache state, and how many matching rows the database must retrieve. MySQL describes common B-tree indexes as searchable structures whose entries point to rows (MySQL: How MySQL Uses Indexes).

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

Which queries can benefit from an index?

Filters and joins

Indexes are often useful for conditions in WHERE clauses and for join keys, provided the indexed expression, operators, and key order are compatible with the query and database engine. An index that does not match the way a query searches may offer little benefit. PostgreSQL explains how indexes can support queries in its introduction to indexes.

Sorting

A B-tree stores keys in an order the database can use. In PostgreSQL, a B-tree index can supply sorted output for a compatible ORDER BY, potentially avoiding a separate sort step (PostgreSQL: Indexes and ORDER BY). This depends on the query’s requested order and the index’s ordering; it is not a general property of every index type.

Why the database may choose a scan instead

Having an index does not mean every query should use it. If a query needs a large fraction of a table, following index entries and fetching many rows can cost more than reading the table sequentially. MySQL documents that the optimizer may prefer a table scan in this situation (MySQL: How MySQL Uses Indexes). There is no universal percentage of rows at which an index stops being worthwhile: the decision depends on the engine, data, query, and workload.

The optimizer compares possible plans using estimates. If those estimates are poor, it may choose a plan that performs badly, including a scan when an index would have helped. Microsoft notes that outdated statistics can contribute to poor plan choices and that a scan can be appropriate when a query requires all rows (Microsoft: Query Processing Architecture Guide). Inspect the execution plan for the specific database rather than treating every scan as a fault. PostgreSQL also notes that ANALYZE may be needed to refresh planner statistics (PostgreSQL: Introduction to Indexes).

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.

What indexes cost

  • Storage: Each index occupies space in addition to the table.
  • Write and maintenance work: Inserts, updates to indexed values, and deletes may require the database to maintain affected indexes. Extra or wide indexes can increase this work.
  • Design effort: An index helps only if its type and key order fit useful queries in the actual workload.

PostgreSQL cautions that indexes add overhead to the system and data-manipulation operations (PostgreSQL: Indexes). Microsoft frames index design as a balance among query speed, update cost, and storage cost (Microsoft: SQL Server Index Design Guide).

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

How to judge whether an index is worthwhile

  • Query shape: Identify the filters, join keys, and expressions the application actually uses. Check that the index type and key order support them.
  • Rows returned: Consider whether the query needs a small subset or much of the table; retrieving many rows can make a scan cheaper.
  • Ordering: Check whether a compatible index can provide the requested order and avoid a separate sort.
  • Workload balance: Weigh how often queries benefit against how often inserts, updates, and deletes must maintain the index.
  • Evidence from the plan: Review the actual execution plan and the optimizer’s estimates, and check whether statistics are current using the method appropriate for your database.

PostgreSQL, MySQL, and SQL Server differ in index types, syntax, optimizer behavior, and diagnostics. The cited vendor documentation covers PostgreSQL 18/current, MySQL 26.7, and SQL Server documentation labeled version 17; check guidance against the version and workload you use. These sources explain the mechanism and trade-offs, but do not establish a universal speedup or a fixed threshold for choosing an index.

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.