Performance: known risks¶
What is known to be slow, or could become slow, and is not fixed yet. Each entry says what
happens, when it matters, and the fix being considered. Measured numbers are in
bench/RESULTS.md (zig build bench -Doptimize=ReleaseFast).
Execution¶
Queries that write materialize rows between clauses. Read-only queries stream rows through their clauses (
operators.zig). A query with a writing clause has aneagerbetween each two clauses, so each clause produces all its rows before the next starts, and its memory grows with the intermediate result. Fix: keep theeageronly where a clause reads what an earlier one writes (before the first write, and between conflicting clauses).Some stages still need every row:
RETURNholds all the rows it returns, so the caller gets nothing until the query ends. Fix: hand rows to the caller as they come (an iterator in the bindings).A
WITHthat sorts, pages or removes duplicates keeps copies of every row.
RETURNprojects every row beforeORDER BY … LIMIT. The partial sort keeps k rows, but every row’s projected columns are computed first. Fix: compute the sort key first and the other columns only for the rows kept (late materialization).Pattern predicates,
EXISTSand pattern comprehensions collect every match. A predicate needs only the first. Fix: stop at the first match forEXISTSand pattern predicates.Unbounded variable-length paths (
-[*]->) enumerate every path, which grows exponentially on dense graphs. Fix: a bound or a warning; for reachability (EXISTS,count(DISTINCT)) a visited set instead of path enumeration. (Shortest paths already have their own breadth-first search,shortest_paths.zig.)Relationship uniqueness is a linear scan of the edges used on the current branch: fine for short patterns, quadratic for long variable-length ones. Fix: a hash set past a few edges.
Each property read decodes from the record. A record is found once per node, through the record cache (see storage). But each
n.propstill walks the record’s property list and decodes its value.
Planning¶
STARTS WITHandINscan. They check every node of the label, even on an indexed property. Fix: seeks forSTARTS WITH(a prefix range) andIN(one seek per element). (A range of booleans, durations, lists or of two kinds of value also scans, thoughEXPLAINshowsNodeIndexRangeSeek: the plan does not know a parameter’s type.)A range’s selectivity is a guess (a quarter of the label). Fix: statistics per property.
A count from an index reads every key in its range (
NodeIndexRangeCount): no node is read, but the time still grows with the matches (WHERE p.age > 60over 100,000 people: about 0.3 ms, against 4 ms reading each node). Fix: counts kept in the index’s B-tree pages, which LMDB does not offer.Each label change reads and writes the label’s counter (
c | label, kept for the planner). Creating many labeled nodes in one transaction pays one counter update per node, about 2 µs. Fix: keep the transaction’s counter changes in memory and write them at commit.Anchor choice is per node, greedy. Joins of several patterns are matched in written order, with no reordering by selectivity.
Edge keys carry the neighbour’s labels as text (about 11 bytes for one short label). Fewer keys fit in a page, so expansions that need no label check are about 10% slower than with empty values. Loading is about 30% slower, because each new edge reads both ends’ records. Fix: label ids (a dictionary of labels) instead of names, and a bulk loader that knows the labels.
Storage¶
Edge properties are one key each (node properties are one record per node). A query that reads several properties of many relationships pays one B-tree search for each. Fix: an edge record, as for nodes.
A commit with
fulldurability waits for the disk (about 8 ms on macOS, which usesF_FULLFSYNC). Many small writes should be batched (write(),UNWIND) or userelaxedordeferreddurability.Node record rewrites: setting one property rewrites the node’s whole record. Fine for normal records; slow for nodes with very large documents (map properties) updated often.
Cold reads go to disk. The storage engine maps the file into memory; the first read of each page in a process whose file is not in the operating system’s cache is a disk read (about 200 µs per point query on a laptop SSD, until the pages a query needs are cached). Normal for a disk database, but it makes the first queries after a start slow. Fix if needed: an optional warm-up (read the file, or
madvise), documented for latency-sensitive uses.Checked builds validate every record they read and write. In Debug and ReleaseSafe builds the storage boundary asserts the layout of each record, value and key it stores or trusts (pair assertions, see testing); ReleaseFast compiles them out. Their cost in ReleaseSafe has not been measured yet; it matters if a package is built in ReleaseSafe to keep them.
Full-text search¶
A full-text query reads an entity’s length for every posting it scores, one B-tree search each. Common words in a large index pay that for every entity that has them. Fix: keep the length in the posting.
Phrases check each candidate with one posting read per word. Fix: walk the words’ postings together, in entity order.
Fuzzy terms compare the query word with the index’s whole vocabulary (Levenshtein, bounded). Fix: a Levenshtein automaton over the sorted vocabulary, or a trigram index.
Writes to indexed entities re-analyze the changed property and read and write one posting per distinct term of its old and new text. Large texts updated often pay for every word.
Scoring is per index, not per property. A field clause (
title:x) uses the property’s length against the whole index’s average length.
Python bindings¶
A returned node costs about 1 µs in Python (its labels tuple and properties dict, with a new key string per property). Fix if it matters: reuse the key strings of a result (property names repeat across rows).
Results are built whole. A query returns a list of every row; a large result is all in memory at once. Fix: return an iterator that converts rows as they are read (needs the pipelined executor above to pay off fully).