Internals

Life of a read

How a point read, a filter scan, an index-assisted query and a sorted page are answered, in the in-memory and lsm engines.

Point read, in-memory engine#

  1. The connection reader receives Get (raftdb-proto). The shard follows from the key hash.
  2. If the connection has no unfinished batches for that shard, the reader answers on its own thread under the table's read lock (D66); otherwise the read is queued behind the writes, so a read always sees the writes sent before it on the same connection.
  3. raftdb-store hashes the key, probes the slot table (raftdb-table): one SSE2 compare of 12 fingerprints in the home bucket, then the next bucket, then a real key comparison in the arena, and returns the document bytes.
  4. The reply Found carries the stored binary form unchanged, or JSON text if asked.

Pipelined Gets are served in windows of 32 keys (get_batch): the buckets of all 32 are requested first so cache misses overlap. Measured on the development PC: 5.97 M/s with the window, 4.70 M/s one at a time, and 5.56 M/s over loopback TCP with 4 clients and 4 shards.

Filter scan#

  1. The reader compiles the JSON or SQL filter against the field dictionary into a plan (raftdb-exec); an unknown field name compiles to an id nothing has.
  2. It first hands each shard the requests it has already read, so the filter sees pipelined writes, then asks every shard to scan.
  3. A shard takes a snapshot of its live records (4 bytes each, cached between scans in a SlotCache and rebuilt only when records were added, removed or moved) and runs the plan 1024 documents at a time over bit masks. Large count scans use worker threads inside the shard.
  4. Counts are summed. A page uses limit (default 100, at most 10,000) and a cursor shard << 48 | matches already returned; the scan grows in steps from 1 K to 64 K documents so a page needing a few hundred documents does not scan the whole shard.

Measured: 57.2 M documents/s on a count over 1 M documents with 4 shards x 3 scan threads. Pages are consistent only while the data does not change; there is no snapshot isolation in the server (MVCC).

Index-assisted query#

If a required comparison of the plan can use a secondary index, the table asks the index for record positions, takes the smallest candidate list, and the executor runs the full plan on only those records. The index may return extra candidates but never misses one. See Secondary indexes. On LSM tables the index is used only when it points to at most about 3 % of the rows; otherwise a scan is faster.

Sorted page#

For ORDER BY, each shard selects every match, reads the sort values once and returns its first start + page rows by partial sort; the connection merges, cuts the page and answers. The cursor is the number of rows already returned. This reads every match unless an index narrows the filter: 40 ms for the top 100 of 1 M rows, about 0.9 s for OFFSET 500000.

Projection#

Project (protocol version 2) returns each matching document cut down to the requested field ids with raftdb_doc::project, copied as stored. A column list in SQL over JSON output, in contrast, parses and writes each row again, so it is slower than SELECT *.

In the same process#

The MySQL front-end in one-process mode calls raftdb_server::Local: a key read or an index-backed filter runs on the session's own thread under the table's read lock and the row is decoded where it is stored, with no round trip. See Pushdown and the executor.

lsm point lookup#

On an LSM table a lookup goes: memtable, then the file list, one bloom probe per candidate file (the key's hash is computed once), a binary search over the file's block index, the cached block, one hash-table probe, one entry. Measured on test server A on 130-byte rows with one thread: 822 ns for a present key and about 280 ns for an absent key. A scan merges all sources in key order and does not fill the block cache. See The LSM engine.