Skip to content

Place MINUS / EXISTS / NOT EXISTS by estimated cost, not a fixed readiness rule #1986

Open

Description

@bplatz

reorder_patterns places MINUS, EXISTS and NOT EXISTS by a fixed readiness rule. Since #1965 the rule is: wait until every earlier pattern that binds any variable has been placed. The rule is sound, but it can hold a negation behind a pattern that cannot change its answer, such as a type check on a subject that is already bound. A tighter readiness rule is not a fix: measured, it wins on some shapes and loses about 3× on others. Placement should come from a cost estimate.

Measurements

The tighter rule was tried in review of #1965 (question, numbers). Under it, a negation waits on each variable its body reads only until a pattern binding it on every row is placed, or every pattern that could bind it is.

It was compared with #1965's rule on the query_hot_negation_count people dataset, plus five scratch scenarios with a redundant producer (a pattern that binds only variables already bound):

  • small scale, 10k people, 80% with an employer, three ex:knows edges each;
  • one EC2 host, both sides built with thin LTO;
  • two runs of the same build differed by up to 30%, so the table compares the two warmed-up runs.
Scenario #1965 rule Tighter rule
minus_count 1.34 ms 1.35 ms
not_exists_after_optional 25.7 ms 25.8 ms
?p ex:livesIn ?c . ?p a ex:Person + NOT EXISTS 7.9 ms 4.1 ms
same + MINUS 5.7 ms 2.4 ms
?p ex:name ?n . ?p a ex:Person + NOT EXISTS, rows 7.6 ms 7.7 ms
?p ex:knows ?f . ?f ex:knows ?p + NOT EXISTS 14.2 ms 40.7 ms
same + MINUS 13.1 ms 38.0 ms

Each negation body is { ?p ex:worksFor ?o }; the scratch scenarios otherwise count the rows shown.

Running the negation before the redundant producer drops rows early. That pays off when the producer is a per-row check such as the type triple. In the cycle it splits two triples that join far better together.

What a fix needs

  • Weigh how many rows the negation removes against the per-row cost of the joins it would run ahead of. Do not split joins the executor fuses or handles better together (same-subject stars, the cycle above).
  • A variable that comes in from a seed (initial_bound_vars) can be unbound on some rows, so it is not settled just because the seed provides it. The tighter rule failed fix(query): filter and negation placement, and point-in-time visibility after reindex #1965's seeded planner test for this reason.
  • MINUS removes from the solutions of the patterns written before it. EXISTS and NOT EXISTS are FILTERs and apply to the whole group. Any reordering must stay within those semantics. Either way, a negation may run ahead of a producer only once every variable it reads is bound on every row.
  • Add the scenarios above to query_hot_negation_count so both directions are guarded.

Activity

  1. added
    enhancement
    P3
    area:query
    performance
    on Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area:queryenhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions