Try
↑ ↓ move↵ openesc close
In this lesson · topic 2 of 7
  1. 1. Finding one row without reading them all
  2. 2. Secondary indexes and Bloom filters
  3. 3. Tune a filter and add indexes
  4. 4. Each index is a tax on every write
  5. 5. Bits per key against false positives
  6. 6. Where filters help and where they do not
  7. 7. False positives, hashes and index cost

Topic 2

Secondary indexes and Bloom filters

6 min read

A secondary index

A secondary index is a B-tree whose keys are the values of one column. Each entry holds the value and either the primary key or the location of the row. A lookup walks the index to the entry, then fetches the row. If the query only needs columns that are in the index, the engine can skip the row fetch. Such an index is called covering.

Because the index is a tree of its own, it has its own pages, its own splits and its own log records. Any change to the indexed column changes the index.

An index on several columns is sorted by the first, then the second within it. So put the column you compare with equality first, and a range column after it.

A Bloom filter

A Bloom filter is an array of bits, all zero, and a few hash functions. To add a key, hash it with each function and set the bits they point to. To ask about a key, hash it the same way. If any of those bits is zero, the key was never added. If all are set, the key might have been added.

The answer “no” is always right, because an added key has all of its bits set. The answer “maybe” can be wrong. Other keys can have set the same bits by chance. That wrong “maybe” is a false positive. A filter never gives a false negative.

What it costs

The array is sized per key. The lab gives it 10 bits for each key, so 5000 keys need 6,250 bytesmeasured. With 7 hash functions, about 51%measured of the bits end up set, and the measured false positive rate is 0.9%measured. The textbook formula gives 0.82%measured for the same settings.

The best number of hashes for 10 bits per key is 7measured. It is about 0.69 times the bits per key, which is the point where half the bits are set.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms