Actuarial Notes

Database Index

Study guide: DISC-DA

A Database Index is a separate, sorted lookup structure built on one or more columns of a table, which stores each value of those columns with a pointer to the rows that hold it. A query that filters, joins or sorts on an indexed column can go straight to the matching rows instead of scanning the whole table, at the cost of extra storage and of updating the index every time the table changes.

An index changes how fast a query runs, never which rows it returns. SQL says what to retrieve, not how, and the database's query planner decides whether an index helps. In many systems, wrapping an indexed column in a function, as in WHERE YEAR(acc_date) = 2024, stops the planner from using an index on acc_date, while the equivalent range acc_date >= '2024-01-01' AND acc_date < '2025-01-01' can use it.

Primary and secondary indexes

A table's indexes are of two kinds:

Primary index Secondary index
Built on The table's primary key Any other column used to find rows
Values Unique and non-null May repeat
Number per table One Several

The primary index is built on the table's primary key, such as the policy number in a policy table. It enforces that the key is unique and present. A table has one primary key and so one primary index, and in many systems the table's rows are stored in that index's order.

A secondary index is built on any other column used to find rows, such as the policy number in a claim table, the claim's accident date or the agent code. A secondary index allows repeated values, so an index on the policy number in a claim table is secondary, because one policy can have many claims. An index on two or more columns together, such as (policy number, term effective date), is a composite index that serves searches on its leading column or on its columns together.

Rows read with and without an index

Without an index, a query scans all n rows of the table to find those meeting a condition. Most relational databases store indexes as balanced trees (B-trees). If each page of a B-tree index points to f pages below it, the number of levels, and so of page reads, needed to find a key is roughly

h = \lceil \log_f n \rceil

The matching rows are then read through their pointers. A large fan-out f is why even a table of millions of rows is searched in a handful of page reads.

How many reads does this query make with and without an index? A policy table holds 1,000,000 rows, and an underwriter's query asks for the row of one policy number. The B-tree index on policy number has pages that each point to 200 pages below them.

Answer

  • Without an index the database cannot know where the policy is, so it scans all 1,000,000 rows.
  • Since 200^2 = 40{,}000 and 200^3 = 8{,}000{,}000, the B-tree needs h = 3 levels to reach any of the million keys.
  • The index turns a million row reads into three page reads, plus one read of the matching row.

Which columns to index

Indexes pay off on columns used in WHERE conditions and in join keys, and on columns with many distinct values. A column with only a few values, such as a claim status of open, closed or reopened, returns a large share of the table for each value, and reading those rows is most of the work whether or not the index is used.

Every index takes storage, and every insert, update or delete must also change each index on the table, so indexes slow down loading and writing. A table that is read far more often than it is written, as an analytical table in a data warehouse is, benefits most. Indexing every column of a frequently loaded table can make the load the bottleneck.

What do this claim table's indexes cost, and which one adds the least value? A claim table of 2,000,000 rows has five indexes: on claim number, policy number, accident date, adjuster code and claim status. Each index entry takes about 20 bytes for its key and pointer, and every night 40,000 new claim rows are loaded.

Answer

  • Each index holds one entry per row, so one index takes 20 \times 2{,}000{,}000 = 40{,}000{,}000 bytes, about 40 MB.
  • Each new row is written to the table and to every index, so the load makes 40{,}000 \times (1 + 5) = 240{,}000 writes, six times what it would make on an unindexed table.
  • The claim status index adds the least value. With three values, a query for open claims returns a large share of the table, so the database gains little from the index and may scan the table anyway.
  • The policy number index is the valuable one, because claims are joined to policies on it in nearly every analysis.

Thinking an index changes the result An answer saying that an index filters, sorts or deduplicates the data confuses the index with the query. The same query returns the same rows with or without the index.

Practice questions

Actuarial Notes