In this lesson · topic 1 of 7
Topic 1
Finding one row without reading them all
3 min read
Scan or look up
A table is stored in the order of its primary key. A query by that key walks a tree. A query by any other column has nothing to walk, so the engine reads every page and checks every row. In the lab a table of 5000 rows is 157measured pages, and a scan reads all of them.
A secondary index is another sorted tree. Its keys are the values of the column and its entries point back at the rows. Looking up a value now takes 4measured page reads in the lab, including the read of the row itself. The gap grows with the table. The scan grows in step with the row count. The index grows with its logarithm.
The second problem
Searching for a key in an LSM tree has a different version of this problem. A key may be in any of many files, and the engine does not know which. The same idea helps: keep a small summary so most files can be skipped. A Bloom filter is the summary that costs the least memory.
Predict first