In this lesson · topic 5 of 7
Topic 5
Why reads cost more here
4 min read
Hits, misses and scans
A read that finds its key stops at the first file that has it. Reads of recent keys are cheap, because new data is in the upper levels or in memory. In the lab the average present key needs 2.6measured files with leveled compaction and 4.0measured with tiered.
A miss is the expensive case. The engine cannot stop early, so it checks every file: 3measured with leveled compaction, 6measured with tiered. Many real workloads ask for keys that are not there, as when a service checks whether an item exists before creating it.
A range scan needs the keys in order from all files at once. The engine opens an iterator on each file and merges them as it goes. Every extra file is another iterator to advance, so a scan over many files is slower than one over few. A filter on each file cannot help here, because a filter answers whether one key is present, not what comes next.
Where the cost goes
A file check is a read of an index and, if the index says the key might be inside, a data block. Indexes are small and are usually cached, so the true cost of a file check is mostly the block read when there is a chance. For a miss, most checks end at the index. For a hit, there is one block read at the right file.
The cheap way to turn a miss into almost no I/O is to keep a small summary of each file’s keys in memory, and consult it before touching the file. That summary is a Bloom filter, and it is the subject of the last lesson of this module.