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 is a separate lookup structure that helps the database find rows without checking every row in a table. Like the index at the back of a book, it maps a searchable term to a place to look—but unlike a printed index, it must stay current as data changes, and using it is not always the fastest option.

What is a database index, in plain English?

A table holds the database’s records; an index holds selected keys and references that help the database locate matching records. The index does not replace the table. For a typical lookup, the database finds an entry in the index and then may fetch the corresponding row from the table.

For example, this SQL creates an index on the email column and asks for a customer with a particular email address:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
CREATE INDEX customers_email_idx ON customers (email);

SELECT customer_id, email
FROM customers
WHERE email = 'reader@example.com';

The database may use the index to find matching entries, then retrieve the corresponding row or rows. The actual plan depends on the database engine, schema, table size, and data distribution; the syntax alone does not promise a speedup. See the SQLite CREATE INDEX documentation and the PostgreSQL 18 index overview.

How does the book-index analogy work?

  • The table is the book’s main text: it contains the full records, just as the pages contain the full discussion.
  • The index key is the term being searched: for instance, a customer’s email address or a book topic such as “orchids.”
  • The index entry points toward the match: a book index gives page references; a database index gives references that help locate table rows.
  • The database navigates to a likely match: rather than examining every row in sequence, it can search the index structure and follow its references.

The analogy is most useful for understanding keys and pointers. A database index is not necessarily a flat, printed list. PostgreSQL 17 describes its B-tree indexes as multi-level structures: internal pages guide the search, while leaf entries point to table rows. The levels narrow down the search, much like consulting successively more specific directions before reaching a page. See PostgreSQL 17 B-tree internals.

Why can a B-tree help with lookups and ranges?

A B-tree keeps keys in order in a balanced, multi-way tree. That ordered structure can help find a particular value and, depending on the engine and query, scan a range of values or return data in an order. For example, a query seeking dates within a range may be able to navigate to the first matching key and continue through nearby entries rather than inspect every row.

These benefits depend on the index definition and the query. A B-tree is a common index type, not the only kind: PostgreSQL 18 documents B-tree, Hash, GiST, SP-GiST, GIN, and BRIN indexes. Different types serve different search patterns and data shapes; it is not accurate to treat every database index as a B-tree.

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

Do indexes make every query faster?

No. An index takes storage and has to be maintained as table data changes. For a query that needs a large share of a table, the database may find it cheaper to read rows sequentially than to look up many index entries and then fetch many separate rows. Small tables can also be faster to scan directly. MySQL’s manual describes these scan-versus-index tradeoffs in its index overview.

The planner weighs whether an index is worthwhile for a particular query. The answer depends on the number of rows needed, table size, data distribution, and whether the index contains the values the query needs. There is no universal percentage by which an index speeds up a query, and adding more indexes is not automatically better.

Why does the order of columns in a composite index matter?

A composite index stores keys made from more than one column, in a defined order. Think of it as an index sorted first by one field and then by the next: the leading field determines how the rest of the keys are grouped.

Rank #3

For MySQL, an index on (A, B, C) supports searches using the leftmost prefixes (A), (A, B), and (A, B, C). A search on B alone does not match those leftmost prefixes. This is MySQL-specific guidance; check the documentation and query plan for the database you use rather than assuming every engine behaves identically. For MySQL’s details, see the index manual.

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

What is a covering index?

A covering index contains all the columns a query needs, so the database can sometimes answer from the index without consulting the table rows for those values. For instance, if a query filters on one indexed column and returns only columns also present in that index, the index may cover it. This is an optimization, not a guarantee that every query with those columns will use that path. MySQL documents covering-index behavior in its index overview.

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

How can you tell whether a query is using an index?

Inspect the execution plan using the plan-explaining command or interface for your database engine. Look for whether the plan uses an index, scans the table, or combines both, and consider how many rows it expects to read. Then evaluate the plan against the query’s real workload; an index appearing in a plan does not by itself prove the query is faster.

Before adding an index for a production query, check the query plan and the workload it serves. Consider how often the query runs, how many rows it needs, and the extra storage and data-change work that the index will add. The right choice is specific to the engine, schema, and workload.

Where does the book analogy break down?

A printed book index is fixed after publication. A database index must remain coordinated with a table that can be inserted into, updated, and deleted from, so it has ongoing maintenance costs. Also, a book reader generally benefits from a page reference, while a database planner can decide that following index references is a worse route than reading much of the table in sequence.

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

Finally, “database index” does not name one universal structure. PostgreSQL lists several index types, while MySQL notes exceptions to B-trees for cases including spatial data, MEMORY tables, and InnoDB FULLTEXT indexes. The useful question is not simply whether a database has an index, but whether that index type and definition fit the query pattern and engine.

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.