Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Index Layers

mq-db applies three complementary index layers, cheapest-first:

SQL Query
   │
   ▼
Layer 1 — Zone Maps (document skip) ────skip───▶ ✗ irrelevant docs
   │ relevant docs
   ▼
Layer 2 — Interval Index (section scope)
   │ candidate blocks
   ▼
Layer 3 — Secondary Indexes (block lookup)
   │  BitmapIndex · BTreeIndex · HashIndex
   │  (no hint ──▶ Full Scan)
   ▼
Result Rows

Layer 1 — Zone Maps (document-level skip)

Built once per document and stored in the .mq-db file. Checked before any block is read.

Via SQLSqlEngine derives a skip automatically from the WHERE clause, for a single, non-JOINed SELECT ... FROM blocks:

WHERE conjunctSkips documents where…
lang = 'X'code_languages doesn’t contain X
depth = N (N > 0)N exceeds max_heading_depth
block_type = 'heading' AND content = 'X'heading_contents has no case-insensitive match for X

Via the Rust APIstore.query().documents(|doc| ...) lets you filter on any zone-map field yourself (heading_slugs, frontmatter_keys, title, tags, …), not just the patterns SqlEngine recognizes automatically.

Layer 2 — Interval Index (section hierarchy)

Heading hierarchy is encoded as (pre, post) pairs via Pre-Post Order (Nested Set) traversal:

# Doc                 pre=0  · post=11
├── ## Section A      pre=2  · post=7
│   ├── Paragraph     pre=3  · post=4
│   └── Code          pre=5  · post=6
└── ## Section B      pre=8  · post=11
    └── Paragraph     pre=9  · post=10

A is_under BB.pre < A.pre AND A.post < B.postO(1), no tree traversal. This is exactly what the SQL under() function and the Rust .under_heading() query-builder method check.

Layer 3 — Secondary Indexes (block-level fast lookup)

IndexColumn(s)StructureComplexity
BitmapIndexblock_typeInverted list per typeO(1) key + O(k) iterate
BTreeIndexpre, postBTreeMapO(log n) point, O(log n + k) range
HashIndexcontent, lang, depthHashMapO(1) average

SQL predicate pushdown picks an IndexHint based on the shape of the WHERE predicate:

WHERE predicateIndex used
block_type = '...'BitmapIndex
pre = NBTreeIndex (point lookup)
pre BETWEEN N AND MBTreeIndex (range scan)
content = '...'HashIndex
lang = '...'HashIndex
depth = NHashIndex
anything elseFull scan