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
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'.
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.