Skip to content

query: run metadata-only filters before geometry filters - #43

Open
cldellow wants to merge 1 commit into
masterfrom
metadata-filters-first
Open

cldellow wants to merge 1 commit into
masterfrom
metadata-filters-first

Conversation

@cldellow

Copy link
Copy Markdown
Owner

Fixes #40.

Change

Query_Statement::apply_all_filters runs each constraint's expensive filter, filter(query, rman, into), in the order the constraints appear in the query. newer and the user filters (uid, user, uid_touched, user_touched) are expensive filters too, but they only read metadata. When a bbox, around or area constraint came first, its filter built geometry for every candidate, and only then did the metadata filter discard most of them.

  • The flag: Query_Constraint::filter_reads_only_metadata(), false by default and true for Newer_Constraint and User_Constraint, marks such filters.
  • The order: apply_all_filters runs the marked filters first, then the rest in their usual order.

Every filter only removes elements, so the order doesn't change the result.

Four files, about 20 lines. Based on fossgis master. No change to the database format or the query API.

Impact

CPU time on the Nova Scotia history database (#2), warm cache, 4-core VM, against fossgis master (8edf11b). Output is identical. Other jobs shared the VM during these runs, so treat small differences as noise.

Query master This PR
[out:csv(::count)]; way(43.3,-66.5,47.1,-59.6)(newer:"2026-11-08T00:00:00Z"); out count; 6.29 s 1.33 s (−79%)
[out:csv(::count)]; way(newer:"2026-11-08T00:00:00Z")(43.3,-66.5,47.1,-59.6); out count; (already in the fast order) 1.36 s 1.37 s
[out:csv(::count)]; nwr(44.60,-63.66,44.70,-63.52)(uid:N); out count; (N: the top local user) 0.51 s 0.41 s (−20%)
[out:csv(::count)]; way(43.3,-66.5,47.1,-59.6)(uid_touched:N); out count; 1.97 s 2.02 s (the user index already chooses the candidates)

Peak memory for the first query drops from 1,119.7 MB to 237.4 MB, because geometry is no longer built for 882,729 ways.

Testing

  • Differential fuzzer against master:

    • plain database: 2,000 queries
    • areas, areas,complete: 1,000 queries
    • history database, users,history,complete: 1,500 queries

    0 differences in all three (13 timeouts skipped in total). This PR against itself: 300 queries, all identical.

  • Targeted check: 400 queries applying the user filters with boxes before and after them, on input sets and in loops (53 returned data). 0 differences.

  • No mutation test: this change reorders filters rather than skipping work, so a mutant would have to drop or duplicate a filter, which the fuzzer's ordinary coverage already exercises.

Memory

No new allocation. Peak memory falls when the metadata filter is selective (see above).

Limitations and open questions

  • Metadata filters aren't always cheaper. If a bbox's geometry check would remove almost everything and the metadata filter almost nothing, running the metadata filter first reads metadata for elements the bbox would have dropped. Metadata is read per tile, so this is bounded by the tiles the bbox already selected. No such case was found here.
  • Other metadata-only constraints (changed, for example) could be marked too, but aren't yet.
  • Narrow test setup: one regional extract, warm cache, one VM.

Generated by Claude Code

Query_Statement::apply_all_filters runs each constraint's expensive filter in
the order the constraints appear in the query. newer and the user filters
(uid, user, uid_touched, user_touched) are expensive filters too, but they
only read metadata. When a bbox, around or area constraint came first, its
filter built geometry for every candidate, and only then did the metadata
filter discard most of them.

A new Query_Constraint::filter_reads_only_metadata() marks such filters, and
apply_all_filters runs them first. Every filter only removes elements, so the
order does not change the result.

On the Nova Scotia history database:

  [out:csv(::count)]; way(43.3,-66.5,47.1,-59.6)(newer:"2026-11-08T00:00:00Z"); out count;

drops from 6.3 s to 1.3 s, the time the same query already took with newer
written first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJUqyDw9zM3Bkh2W2QT
@cldellow cldellow added the query-perf Query performance change; collected on all-changes label Nov 21, 2026 — with Claude
@cldellow cldellow added the human-reviewed Colin has reviewed this. label Nov 21, 2026
@cldellow

Copy link
Copy Markdown
Owner Author

Colin's thoughts: low risk, medium confidence, high benefit.

The thrust is simple - rule things out cheaply before doing more expensive checks. The medium confidence comes from the case hypothesized in the PR -- where a bbox filter would rule out most things and a metadata filter few things. I suspect that's probably rare: many edits build up over time, many users edit the same area.

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

Labels

human-reviewed Colin has reviewed this. query-memory query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Filter order follows the query text: way(bbox)(newer:...) is 4x slower than way(newer:...)(bbox)

2 participants