What is Database index?

A data structure in a database for quickly finding, from a value in a particular column of a table, the location of the rows holding that value. B+ trees are commonly used. It makes searches faster but must be maintained every time a row is inserted, updated or deleted, and it uses storage.

Japanese name: インデックス

Database index appears in 3 practice questions across 1 topic, including Database Basics.

Field: Database Basics

How a B+ tree index works

Relational database indexes commonly use B+ trees. A B+ tree is a tree structure with many branches per node and the same number of levels from the root to every leaf; the leaves hold column values and row locations in value order and are linked to each other in sequence. If each node branches 100 ways, three levels can distinguish 1 million entries, so even in a table of 1 million rows the target row is reached in a few reads. Because the leaves are linked in value order, the index can be used not only for searches with = but also for range searches such as BETWEEN and for sorting with ORDER BY.

When an index helps and when it does not

An index is effective on a highly selective column, where a condition narrows the rows to a small fraction of the whole. A column whose values are all different, such as a member number, is ideal, but on a column with only two values, such as gender, about half the rows match, and a full table scan reading the table in order is often faster than fetching rows one at a time through the index. An ordinary index may also be unusable for conditions that apply a function to the column or for suffix matches such as LIKE '%son'.

The cost on writes

An index is a data structure maintained separately from the table. Every time a row is inserted, updated or deleted, all related indexes must be updated too, so the more indexes there are, the slower writes become and the more storage is used. The basic approach is therefore to create indexes on columns often used as search or join conditions and to drop indexes that are not used. Many DBMSs automatically create an index on primary key and UNIQUE columns.

Try it: questions about Database index

A B+ tree database index was created on the member_no column of a member table with 1 million rows. Which correctly pairs the effect that can generally be expected with its cost?

  1. ASearches such as WHERE member_no = 12345 become faster, but writes become slower because the index must also be updated every time a row is added or updated
  2. BSearches on every column become faster, at no cost
  3. CThe table's rows are sorted by member_no, so every search becomes faster, and the storage needed is halved
Show answer and explanation

Answer: A. Searches such as WHERE member_no = 12345 become faster, but writes become slower because the index must also be updated every time a row is added or updated

A database index is a data structure for quickly finding, from a value in a particular column, the location of the rows holding that value; relational databases commonly use B+ trees. Without an index, finding the row WHERE member_no = 12345 requires examining 1 million rows in order (a full table scan), but with a B+ tree the row is found by following just a few levels from the root to a leaf. If each node branches 100 ways, for example, 100 × 100 × 100 = 1 million, so about three levels are enough. Because the leaves of a B+ tree are linked in value order, the index can also be used for range searches such as BETWEEN and for sorting with ORDER BY. On the other hand, the index must be updated each time a row is inserted, updated or deleted, so writes become slower, and storage is needed for the index. Searches on columns without an index do not become faster.

For a member table with 1 million rows, consider creating a B+ tree index on each of the columns member_no (all different), prefecture (47 values) and gender (2 values, split roughly in half). When searching by specifying a value for one column in the WHERE clause, on which column is an index likely to be least effective?

  1. Aprefecture
  2. Bgender
  3. Cmember_no
Show answer and explanation

Answer: B. gender

An index is effective when the rows that meet the condition are narrowed down to a small fraction of the whole. Every member_no is different, so only one row matches and the index leads straight to it. Prefecture has 47 values, so if members are evenly spread, one value narrows the search to about 1 million ÷ 47 ≈ 21000 rows (about 2% of the total). Gender has two values split roughly in half, so either value matches about 500000 rows (about 50%). Fetching that many rows one at a time through an index is often slower than a full table scan reading the table from start to finish, so the DBMS optimizer may choose an execution plan that does not use the index. In this way, the more distinct values a column has and the more strongly a condition on it narrows down the rows (the more selective it is), the more effective an index on it is.

FAQ

Should an index be created on every column?

No. The more indexes there are, the more maintenance work is needed every time a row is inserted or updated, and the more storage is used. Create indexes on columns often used as search or join conditions that narrow down the rows sufficiently.

Why is an index not used even though it was created?

Possible reasons include the optimizer judging that so many rows match that a full table scan is faster, or a condition that cannot use the index's order, such as one applying a function to the column or a LIKE suffix match.

Database index in the explanations

Questions that mention Database index (3)

  1. A B+ tree database index was created on the member_no column of a member table with 1 million rows. Which correctly pairs the effect that can generally be expected with its cost?
  2. For a member table with 1 million rows, consider creating a B+ tree index on each of the columns member_no (all different), prefecture (47 values) and gender (2 values, split roughly in half). When searching by specifying a value for one column in the WHERE clause, on which column is an index likely to be least effective?
  3. Which correctly describes a database view in a relational database?

Books on Database Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Database view Relational database ACID properties E-R diagram Foreign key Isolation level Normalization NoSQL Primary key SQL

Back to the glossary