Skip to content

[Performance]: FULLTEXT2 natural/MUST prefix overhead and BOW WAND iteration costs #29205

Closed

Description

@VioletQwQ-0

Is there an existing issue for performance?

  • I have checked the existing issues.

Related work: #27801 (CJK phrase evaluation), #27250 (query-local metadata reuse), #27155 (deferred positions), #27245 (pure-term MUST). This issue consolidates measured evidence for three slow FULLTEXT2 query shapes and identifies two optimization tracks; it does not claim a new correctness defect.

Environment

  • Measured commit: 6690f1af97977ad5671235e9204296cd2d504ad5.
  • Linux amd64, native Go 1.26.4 kernel harness on shared mo-129; C1, GOMAXPROCS=4, 6 GiB cap. Accepted profiles had CPU idle above 92%, low iowait and no swap-out. Resource-gated, not host-exclusive.
  • Real 100k-document subset; ten 10k-document positional segments; BM25, TopK100, no business filter. Heap-backed deserialization, not the SQL service/cache path.
  • Frozen query packs from nightly 35241042673. CPU profiles compare pre-parsed search-only with query-construction-plus-search; allocation and counters use separate runs.

Details of Performance

Main finding

The selected queries are search-bound, not primarily tokenizer-bound. There are two distinct problems to investigate:

  1. ngram natural/MUST: short CJK prefixes trigger expensive posting materialization; MUST also repeats phrase matching for global DF before final intersection.
  2. gojieba BOW: WAND spends substantial CPU evaluating block bounds and aligning cursors, with little whole-block skipping for the selected query.

Source check on 2026-09-22, main 3727c5c5436ca136d7b0312f75bc223a11692a85: these search implementations are unchanged from the measured commit. The FULLTEXT2 package delta only changes INCLUDE predicates/tests. Main was source-checked, not runtime-benchmarked.

1. ngram: prefix work dominates natural and MUST

Selected query Key evidence Interpretation
natural_mixed, human #104 mergeStarPostings ~78% cumulative CPU in both entrypoints. Separate allocation profile: decodeTermEntry 38%, merge itself 20%, FST state decoding 20%, position materialization 10% of allocated bytes. Prefix enumeration, repeated metadata lookup and posting/position merging are costly even when the final result is empty.
boolean_must, human #153 Nine CJK operands, four short. unionDocIDs ~73% cumulative CPU; phraseHits and phraseDf each ~47%. Both entrypoints return empty results. Operands become phrase/prefix clauses, not atomic-term MUST. The full evaluator computes clause matches before intersection; global DF repeats phrase search.

Cumulative CPU percentages overlap and must not be added. Allocation percentages describe allocation sources, not retained memory or a leak.

The query builder and Boolean routing/evaluation explain why adding CJK MUST operands does not necessarily produce cheap posting intersection. Prefix fallback and global phrase DF identify the repeated work.

2. gojieba BOW: expensive WAND iteration, limited block skipping

Selected query: human #42, eight SHOULD terms, 100 returned results. CPU profiles show searchWAND ~98%, blockMax 29–32%, skipTo ~21%; end-to-end query construction is 0.13% and gojieba tokenization/CGo 0.10%.

Request-local diagnostic counts, summed across ten segments:

Event Per request
Pivots found 57,605
Block upper-bound calls 205,271
Block-prune branch executions 397
Cursor jumps (skipTo) 38,468
Whole intervening blocks skipped without decoding 0
Block decode calls 1,686
Candidates actually scored 19,137
Individual term scoring calls 62,605

Only 0.69% of pivots take the block-prune branch; 38,071 jumps are alignment. This supports investigating repeated upper-bound and cursor work in the WAND loop, rather than changing the tokenizer.

Three diagnostic requests produced identical counts, and every segment's PK/score/include results exactly matched the uninstrumented implementation. Counters are request-local; the performance binary was unchanged. Zero whole-block skips does not mean zero pruning benefit: it excludes the already decoded current block, within-block movement and unvisited tails after early termination. Counts alone do not establish whether bounds are too loose.

Next work and acceptance

  1. Prefix track: reduce repeated FST/metadata work and unnecessary posting materialization; investigate bounded candidate-first MUST evaluation and repeated DF work. Do not reintroduce an unbounded hits cache or change global scoring semantics.
  2. WAND track: test request-local reuse of repeated block bounds, then inspect cursor alignment and threshold progression. Bound tightness and threshold evolution still need evidence before changing pruning logic.
  3. Validate each change: same frozen inputs/configuration, ordinary timing plus separate profiles; exact result/score checks including zero/nonzero hits, phrase/prefix semantics, liveness, multiple segments and TopK ties; bounded memory and lifecycle behavior. Then validate through SQL on the full corpus.

Not yet proven: a speedup, a regression against an older release, a WAND correctness bug, pack-wide behavior, or the cause of the full nightly QPS magnitude. No production optimization has been implemented. The evidence supports mechanisms for three representative queries, not completion of performance acceptance.

Additional information

Evidence identity and availability

Collected 2026-09-21/22. All query indices above are one-based.

  • Full cleaned corpus: 2,303,643 rows, SHA256 548adc995d00fbc13138c74ecb6259f53b681cb4695cb75dd1137211cc3e77e5.
  • 100k subset SHA256: eaa37328d81eedecf1b8e526d88d2c8fe3b9b7b92e05edbea4731520701cad3b.
  • Natural pack (188 queries): a73f75109c0d460d020f2c587aec997fc7edd47150b46633535511cf28f91e94; selected query hash prefix 5b744c972b5e.
  • Ngram MUST pack (200): 750fb61fc2cf67336880ad5937605fdf871b445ab827cea24516da101c67b859; selected query hash prefix cfe01ee6366d.
  • Gojieba BOW pack (200): f6ce089380452686462329a91b48da58e19d9399ec57ddc9c12fd8f224852934; selected one-query pack including newline: 1aa425e8425ba4e5bbf2888784ecd8c270407c0ce9393346a72be55cb2e43393.
  • CPU/allocation binary Build ID: c60716fa4a0942e40986a9026b8fd4f6a7fbfc5b.
  • Counter binary SHA256: 788025dc3f3bb5122b655de833b685fc81bcfe17284638abbe2a0b9a9991728a.
  • Evidence directories in the controlled experiment: results/target-cpu-v1, results/followup-profile-v1, results/bow-profile-v1, results/bow-counts-v1. Contention-failed attempts are excluded. Allocation uses separately sampled allocs(end)-allocs(base) and is subject to GC reporting lag.
  • Counter validation: native build, counter boundary tests and selected-query equivalence PASS; diagnostic terminated successfully with MainPID=0, no swap-out, peak RSS about 412 MiB.

The assignee retains structured results, profiles and manifests. Raw query text/corpus/profiles are not publicly attached; the test-only harness is not committed upstream. This issue is therefore an evidence summary, not a standalone turnkey reproducer.

Activity

  1. self-assigned this
    on Sep 21, 2026
  2. changed the title [-][Performance]: short CJK prefixes dominate FULLTEXT2 natural/MUST queries through posting materialization and repeated DF scans[/-] [+][Performance]: FULLTEXT2 natural/MUST prefix overhead and BOW WAND iteration costs[/+] on Sep 22, 2026
  3. VioletQwQ-0 commented on Sep 24, 2026

    @VioletQwQ-0
    CollaboratorAuthor

    Follow-up preflight for #29205 — corrected evidence scope and next gates (diagnostic only, not PR-head or SQL/nightly validation).

    • Identity: frozen search/tokenizer source 6690f1af97977ad5671235e9204296cd2d504ad5; source-equivalent paths on checked main 780ef933f2479aa1679faf13d08850ce6c8f7c2f. Shared mo-55 used Linux amd64 / Go 1.26.6 and a verified 100,000-document subset (eaa37328d81eedecf1b8e526d88d2c8fe3b9b7b92e05edbea4731520701cad3b) in ten loaded segments per parser. This is a different host/toolchain from the earlier mo-129 profile; no paired wall-time comparison exists.
    • Correctness: normal-tokenizer phrase-start/TF and controlled global-DF oracles passed. Small heap/mmap fixtures already include a non-empty mixed star+exact phrase (alpha 中国) and M1/M2 matched baseline there. A separate loaded-index single-star control returned 9,616 documents with identical per-segment TF; it is a correctness control, not a measured non-empty allocation gain. The injected duplicate-position TF counterexample is not known to be reachable through normal SQL: for a fixed phrase slot, the same phrase start implies the same document position, while the tested normal ngram producer emitted one term per position. Other legitimate producers would need a concrete same-position witness before this becomes a public correctness claim.
    • M1 passes the FST prefix iterator's value to the existing directory decoder, removing the second FST get, not the directory decode. M2, tested on M1, omits only the resident blockMaxTf / blockMinDocLn arrays on the phrase path; it still parses the fields and retains blockLastDoc, blockOff, and blockPosOff. For natural Missing Create Table Statement #104, ten requests allocated 912.5 MB Go bytes / 15.62M mallocs at baseline, 651.4 MB / 13.36M with M1, and 642.2 MB / 11.10M with M1+M2. M1 saves ~28.6% bytes; M2 adds ~1.0% of baseline bytes (~1.4% relative to M1) and ~16.9% fewer mallocs relative to M1. The historical decodeTermEntry and FST flat-allocation shares cannot be added as removable work. A new per-source allocation profile is needed to explain residual costs. None of these numbers is a wall-time speedup.
    • Counter denominator: natural Missing Create Table Statement #104 expanded 127,337 terms in one explicit pass across ten segments, while 912.5 MB covers ten complete requests. Dividing the allocation by ten and then by the one-pass term count gives only a rough ~717 B/expanded term at baseline and ~205 B/term saved by M1; even these are not per-term costs because the numerator includes work outside the counted pass. They do not prove DF skew. Natural Missing Create Table Statement #104 and MUST Complement the details about the data type #153 both returned zero hits on this subset, so their allocation/event counts cannot represent non-empty or pack-wide behavior.
    • BOW Add branchless implementation of AND/OR #42 in this follow-up recorded 5,470 pivots, 19,256 block-upper-bound calls, 22,996 block-index lookups, 22,830 consecutive same-block returns, 97,544 binary-search iterations, and exact per-segment PK/score equality in three diagnostic repetitions. A consecutive same-block return is not a proven valid-range cache hit or time saved. The earlier diagnostic reported 57,605 pivots and 205,271 bounds for a nominally corresponding query; reconcile request count, counter scope/version, query identity, corpus/index layout, and toolchain before comparing the two rounds. In particular, multiplying old pivots by eight terms and comparing with new lookups is not a valid cost estimate.

    Updated order: (1) keep M1 as a minimal production-path prototype candidate, not a merge-ready performance fix; test a non-empty mixed star+exact phrase on the real loaded subset for result/score and allocation; (2) profile baseline/M1/M1+M2 allocations by FST, directory, and materialization/merge source before deciding M2's value; (3) reconcile BOW counters and measure blockIndexAt's actual share within blockMax before attempting a request-local valid-block-range cache; (4) require clean-host paired timing, non-empty controls, full query packs, and then production SQL/nightly evidence before claiming a speedup. The shared host failed the 60-second idle/iowait/no-swap admission check, so wall-time/QPS remains BLOCKED_ENVIRONMENT / NOT_MEASURED. No production algorithm or index format was changed by this preflight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions