Skip to content

query: drop tag-index candidates outside the query's ranges during the lookup - #243

Open
cldellow wants to merge 1 commit into
masterfrom
tags-global-bbox-prune
Open

cldellow wants to merge 1 commit into
masterfrom
tags-global-bbox-prune

Conversation

@cldellow

Copy link
Copy Markdown
Owner

Fixes #242.

Change

Query_Statement::execute (statements/query.cc) computes the constraints' node, way and relation ranges before the tag lookup instead of after it (they don't depend on it), and passes them to progress_1 and collect_ids (data/tags_global_reader.h). At the current date, collect_ids now:

  • drops candidates outside the ranges after each lookup (keep_ids_inside), and stops when none are left. The global tag index stores coarse indices (multiples of 0x100), so a candidate is kept if its cell overlaps the ranges, as intersect_ranges does. A constraint never collects elements outside its ranges, so this drops nothing it would have returned.
  • looks the key and key-value filters up smallest first, counted in blocks of the global tag index. Block_Backend::count_blocks (template_db/block_backend.h) counts them from the block index alone, without reading or decompressing anything.
  • stops early and checks the remaining tags late (check_keys_late = prefer_ranges) when that reads fewer blocks than the remaining global lookups: the candidates' cells' skeleton and local tag blocks (count_late_check_blocks), against the remaining lookups' global blocks.

Past-date lookups are unchanged. No change to the database format, the query API or the results.

Impact

US northeast full-history extract (Geofabrik), warm cache, 4-core VM, builds alternating; median user+sys CPU and peak RSS. Base upstream/master a9d8f15.

The prompting query in three 1° × 1° boxes and their 0.99999° versions (just under 1 square degree, which collects the whole box instead), 5 runs:

Query CPU, master CPU, this PR change peak, master peak, this PR
dense_099 6.19 s 6.33 s +2% 608 MB 608 MB
dense_1 1.38 s 1.20 s -13% 13 MB 13 MB
empty_099 0.00 s 0.00 s — 9 MB 9 MB
empty_1 0.31 s 0.00 s -100% 11 MB 9 MB
sparse_099 0.69 s 0.69 s -0% 89 MB 90 MB
sparse_1 0.38 s 0.03 s -92% 12 MB 10 MB
total 9.0 s 8.2 s -7.8%

The 0.99999° versions don't use the global tag index and are unchanged. In the dense box (Worcester to Springfield, 1,025 results) the gain is smaller, since about a thousand candidates do lie in the box; the time there is now mostly reading their skeletons and printing them.

The 20 production-shaped queries (the 20 heaviest query templates on the public servers in June 2026, moved to Boston and New England coordinates), 3 runs, identical output on all 20:

Query CPU, master CPU, this PR change peak, master peak, this PR
p01 1.54 s 1.14 s -26% 13 MB 13 MB
p02 1.22 s 1.18 s -3% 20 MB 17 MB
p03 2.43 s 2.41 s -1% 19 MB 20 MB
p04 6.58 s 6.55 s -0% 237 MB 237 MB
p05 0.60 s 0.55 s -8% 11 MB 11 MB
p06 0.44 s 0.41 s -7% 19 MB 18 MB
p07 5.46 s 5.31 s -3% 33 MB 33 MB
p08 0.01 s 0.00 s — 9 MB 8 MB
p09 3.24 s 3.40 s +5% 53 MB 54 MB
p10 4.39 s 4.35 s -1% 36 MB 36 MB
p11 3.66 s 3.76 s +3% 15 MB 15 MB
p12 1.09 s 0.95 s -13% 12 MB 13 MB
p13 0.22 s 0.20 s -9% 23 MB 23 MB
p14 1.51 s 0.62 s -59% 11 MB 12 MB
p15 0.53 s 0.50 s -6% 22 MB 23 MB
p16 9.03 s 6.80 s -25% 77 MB 77 MB
p17 0.63 s 0.51 s -19% 15 MB 16 MB
p18 10.62 s 9.85 s -7% 19 MB 19 MB
p19 7.27 s 6.24 s -14% 16 MB 16 MB
p20 0.79 s 0.16 s -80% 12 MB 13 MB
total 61.3 s 54.9 s -10.4%
Queries

dense_1:

[out:json][timeout:90]; (way["maxheight"]["highway"](42.0,-72.0,43.0,-71.0); way["bridge"]["maxheight"](42.0,-72.0,43.0,-71.0); way["tunnel"]["maxheight"](42.0,-72.0,43.0,-71.0); node["maxheight"]["highway"](42.0,-72.0,43.0,-71.0); ); out center;

sparse_1:

[out:json][timeout:90]; (way["maxheight"]["highway"](41.0,-70.5,42.0,-69.5); way["bridge"]["maxheight"](41.0,-70.5,42.0,-69.5); way["tunnel"]["maxheight"](41.0,-70.5,42.0,-69.5); node["maxheight"]["highway"](41.0,-70.5,42.0,-69.5); ); out center;

empty_1:

[out:json][timeout:90]; (way["maxheight"]["highway"](39.0,-70.0,40.0,-69.0); way["bridge"]["maxheight"](39.0,-70.0,40.0,-69.0); way["tunnel"]["maxheight"](39.0,-70.0,40.0,-69.0); node["maxheight"]["highway"](39.0,-70.0,40.0,-69.0); ); out center;

The 0.99999° versions shrink each box's north and east edges by 0.00001°. p01 is dense_1.

p01 (maxheight bbox1deg)

[out:json][timeout:90]; (way["maxheight"]["highway"](42.0,-72.0,43.0,-71.0); way["bridge"]["maxheight"](42.0,-72.0,43.0,-71.0); way["tunnel"]["maxheight"](42.0,-72.0,43.0,-71.0); node["maxheight"]["highway"](42.0,-72.0,43.0,-71.0); ); out center;

p02 (amenities around union)

[out:json][timeout:30]; (node["amenity"="school"](around:3000,42.3601,-71.0589); way["amenity"="school"](around:3000,42.3601,-71.0589); node["amenity"="kindergarten"](around:3000,42.3601,-71.0589); way["amenity"="kindergarten"](around:3000,42.3601,-71.0589); node["amenity"="hospital"](around:5000,42.3601,-71.0589); node["amenity"="clinic"](around:5000,42.3601,-71.0589); way["amenity"="hospital"](around:5000,42.3601,-71.0589); node["amenity"="pharmacy"](around:3000,42.3601,-71.0589); node["shop"~"supermarket|convenience|mall"](around:2000,42.3601,-71.0589); way["shop"~"supermarket|convenience|mall"](around:2000,42.3601,-71.0589); node["highway"="bus_stop"](around:1500,42.3601,-71.0589); node["public_transport"="stop_position"](around:1500,42.3601,-71.0589); node["railway"="station"](around:5000,42.3601,-71.0589); node["railway"="halt"](around:5000,42.3601,-71.0589); way["natural"="water"](around:3000,42.3601,-71.0589); relation["natural"="water"](around:3000,42.3601,-71.0589); way["landuse"="forest"](around:3000,42.3601,-71.0589); relation["landuse"="forest"](around:3000,42.3601,-71.0589); way["leisure"="park"](around:2000,42.3601,-71.0589); way["highway"~"primary|secondary|tertiary"](around:2000,42.3601,-71.0589); way["highway"~"residential|unclassified|service"](around:500,42.3601,-71.0589); way["railway"~"rail|narrow_gauge"](around:1000,42.3601,-71.0589); way["landuse"="industrial"](around:3000,42.3601,-71.0589); node["landuse"="landfill"](around:5000,42.3601,-71.0589); way["landuse"="landfill"](around:5000,42.3601,-71.0589); node["amenity"="post_office"](around:3000,42.3601,-71.0589); node["amenity"="fuel"](around:3000,42.3601,-71.0589); ); out center body;

p03 (admin in area)

[out:json][timeout:30]; area(3600061315)->.country; (way["boundary"="administrative"]["admin_level"="8"](area.country); relation["boundary"="administrative"]["admin_level"="8"](area.country); ); out geom;

p04 (brands bbox ci)

[out:json][timeout:25]; (node["amenity"~"fuel|truck_stop"]["brand"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); node["amenity"~"fuel|truck_stop"]["name"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); way["amenity"~"fuel|truck_stop"]["brand"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); way["amenity"~"fuel|truck_stop"]["name"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); node["shop"~"convenience|supermarket"]["brand"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); node["shop"~"convenience|supermarket"]["name"~"travelcenters of america|petro stopping center|love's travel stop|ta travelcenters|pilot flying j|sapp brothers|pilot travel|seven eleven|road ranger|sapp bros|race trac|flying j|ta petro|buc-ee's|racetrac|speedway|circle k|7-eleven|7 eleven|flyingj|buc ees|am best|circlek|7eleven|love's|bucees|ambest|ambest|loves|pilot|petro|7-11|7 11|wawa|ta",i](42.16,-71.26,42.56,-70.86); node["leisure"~"fitness_centre|sports_centre"]["brand"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); node["leisure"~"fitness_centre|sports_centre"]["name"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); way["leisure"~"fitness_centre|sports_centre"]["brand"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); way["leisure"~"fitness_centre|sports_centre"]["name"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); node["amenity"="gym"]["brand"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); node["amenity"="gym"]["name"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); way["amenity"="gym"]["name"~"planet fitness|planet-fitness|crunch fitness|crunch-fitness|planetfitness|crunch gym",i](42.16,-71.26,42.56,-70.86); ); out center tags;

p05 (global data center)

[out:json][timeout:300]; (node["telecom"="data_center"]; way["telecom"="data_center"]; ); out center tags;

p06 (food around regex)

[out:json][timeout:40]; (node["amenity"~"restaurant|cafe|ramen|sushi|izakaya|fast_food|bar|pub|food_court"](around:500,42.3601,-71.0589); way["amenity"~"restaurant|cafe|ramen|sushi|izakaya|fast_food|bar|pub|food_court"](around:500,42.3601,-71.0589); node["amenity"~"school|university|college|kindergarten"](around:1000,42.3601,-71.0589); way["amenity"~"school|university|college|kindergarten"](around:1000,42.3601,-71.0589); node["amenity"~"hospital|clinic|doctors|pharmacy"](around:5000,42.3601,-71.0589); way["amenity"~"hospital|clinic|doctors|pharmacy"](around:5000,42.3601,-71.0589); node["railway"~"station|halt"](around:1000,42.3601,-71.0589); way["railway"~"station|halt"](around:1000,42.3601,-71.0589); node["highway"="bus_stop"](around:1000,42.3601,-71.0589); node["amenity"="parking"](around:500,42.3601,-71.0589); way["amenity"="parking"](around:500,42.3601,-71.0589); node["shop"="convenience"](around:500,42.3601,-71.0589); way["shop"="convenience"](around:500,42.3601,-71.0589); node["tourism"~"hotel|motel|hostel|guest_house|ryokan"](around:2000,42.3601,-71.0589); way["tourism"~"hotel|motel|hostel|guest_house|ryokan"](around:2000,42.3601,-71.0589); ); out center;

p07 (global seamark recurse)

(rel['seamark:type']; >; way['seamark:type']; >; node['seamark:type']; ); out meta;

p08 (global cameras)

[out:json][timeout:30]; (node["camera:type"="traffic_monitoring"]["camera:url"]; node["camera:type"="traffic_monitoring"]["camera:image"]; node["camera:type"="traffic_monitoring"]["image"]; node["camera:type"="traffic_monitoring"]["url"]; node["surveillance:type"="traffic_monitoring"]["camera:url"]; node["surveillance:type"="traffic_monitoring"]["camera:image"]; node["surveillance:type"="traffic_monitoring"]["image"]; node["surveillance:type"="traffic_monitoring"]["url"]; node["man_made"="surveillance"]["camera:type"="traffic_monitoring"]["camera:url"]; node["man_made"="surveillance"]["camera:type"="traffic_monitoring"]["camera:image"]; node["man_made"="surveillance"]["camera:type"="traffic_monitoring"]["image"]; node["man_made"="surveillance"]["camera:type"="traffic_monitoring"]["url"]; ); out body;

p09 (3d buildings bbox)

[out:json][timeout:360][bbox:42.34,-71.08,42.38,-71.04]; (nwr["building"]; nwr["building:part"]; relation["type"="building"]; nwr["highway"]; nwr["landuse"]["landuse"!="salt_pond"]; nwr["natural"]["natural"!="coastline"]["natural"!="bay"]["natural"!="strait"]; nwr["leisure"]; nwr["water"]["water"!="bay"]["water"!="ocean"]["water"!="sea"]["tidal"!="yes"]; nwr["waterway"]["waterway"!="tidal_channel"]; nwr["amenity"]; nwr["tourism"]; nwr["bridge"]; nwr["railway"]; nwr["roller_coaster"]; nwr["barrier"]; nwr["entrance"]; nwr["door"]; nwr["power"]; nwr["historic"]; nwr["emergency"]; nwr["advertising"]; nwr["man_made"]; nwr["aeroway"]; nwr["3dmr"]; way["place"]["place"!~"^(ocean|sea|bay|strait|sound|fjord)$"]; way; )->.relsinbbox; (way(r.relsinbbox); )->.waysinbbox; (node(w.waysinbbox); node(w.relsinbbox); )->.nodesinbbox; .relsinbbox out body; .waysinbbox out body; .nodesinbbox out skel qt;

p10 (tourism around25k)

[out:json][timeout:45]; (node["tourism"](around:25000,42.3601,-71.0589); way["tourism"](around:25000,42.3601,-71.0589); node["amenity"~"restaurant|cafe|bar|pub|nightclub|fast_food|food_court|biergarten|ice_cream|bakery"](around:25000,42.3601,-71.0589); way["amenity"~"restaurant|cafe|bar|pub"](around:25000,42.3601,-71.0589); node["amenity"~"theatre|cinema|museum|arts_centre|library|community_centre|concert_hall|studio"](around:25000,42.3601,-71.0589); way["amenity"~"theatre|cinema|museum|arts_centre"](around:25000,42.3601,-71.0589); node["leisure"~"park|garden|stadium|sports_centre|swimming_pool|beach_resort|water_park|marina|golf_course"](around:25000,42.3601,-71.0589); way["leisure"~"park|garden|stadium|nature_reserve"](around:25000,42.3601,-71.0589); node["shop"~"mall|department_store|supermarket|marketplace|clothes|books|gift|art|antiques|wine|craft|boutique"](around:25000,42.3601,-71.0589); way["shop"~"mall|department_store|marketplace"](around:25000,42.3601,-71.0589); node["historic"](around:25000,42.3601,-71.0589); way["historic"](around:25000,42.3601,-71.0589); node["natural"~"peak|beach|spring|cave_entrance|volcano|waterfall"](around:25000,42.3601,-71.0589); node["man_made"~"tower|lighthouse|windmill"](around:25000,42.3601,-71.0589); ); out body center 500;

p11 (brownfield bigbbox)

[out:json][timeout:180]; (way["landuse"="brownfield"](40.5,-80.0,47.5,-67.0); way["landuse"="landfill"](40.5,-80.0,47.5,-67.0); node["man_made"="dump"](40.5,-80.0,47.5,-67.0); way["abandoned:landuse"="industrial"](40.5,-80.0,47.5,-67.0); ); out geom;

p12 (clinic name ci around25k)

[out:json][timeout:30]; (node["amenity"="clinic"]["name"~"cosmetic|aesthetic|beauty|skin|plastic",i](around:25000,42.3601,-71.0589); way["amenity"="clinic"]["name"~"cosmetic|aesthetic|beauty|skin|plastic",i](around:25000,42.3601,-71.0589); node["healthcare"~"aesthetics|cosmetics",i](around:25000,42.3601,-71.0589); way["healthcare"~"aesthetics|cosmetics",i](around:25000,42.3601,-71.0589); ); out body center qt;

p13 (map call meta)

[bbox:42.355,-71.065,42.365,-71.055]; (node(42.355,-71.065,42.365,-71.055); way(bn); node(w); ); (._; (rel(bn)->.a; rel(bw)->.a; ); rel(br); ); out meta;

p14 (area by name worship)

[out:json][timeout:90]; area["name"="Massachusetts"]->.searchArea; (node["amenity"="place_of_worship"]["religion"="jewish"](area.searchArea); way["amenity"="place_of_worship"]["religion"="jewish"](area.searchArea); rel["amenity"="place_of_worship"]["religion"="jewish"](area.searchArea); ); out body 200;

p15 (tiny bbox many keys geom)

[out:json][timeout:25][bbox:42.3580,-71.0620,42.3624,-71.0565]; (node[amenity]; way[amenity]; relation[amenity]; node[building~"^(commercial|retail|office|public|civic|school|college|university|kindergarten|hospital|clinic|transportation|train_station|church|chapel|mosque|temple|synagogue|hotel|supermarket)$"]; way[building~"^(commercial|retail|office|public|civic|school|college|university|kindergarten|hospital|clinic|transportation|train_station|church|chapel|mosque|temple|synagogue|hotel|supermarket)$"]; relation[building~"^(commercial|retail|office|public|civic|school|college|university|kindergarten|hospital|clinic|transportation|train_station|church|chapel|mosque|temple|synagogue|hotel|supermarket)$"]; node[entrance]; node[highway~"^(bus_stop|crossing|elevator)$"]; way[highway~"^(bridleway|bus_guideway|cycleway|footway|living_street|motorway|motorway_link|path|pedestrian|primary|primary_link|residential|residential_link|road|secondary|secondary_link|service|steps|tertiary|tertiary_link|trunk|trunk_link|unclassified)$"]; relation[highway~"^(bridleway|bus_guideway|cycleway|footway|living_street|motorway|motorway_link|path|pedestrian|primary|primary_link|residential|residential_link|road|secondary|secondary_link|service|steps|tertiary|tertiary_link|trunk|trunk_link|unclassified)$"]; node[historic]; way[historic]; relation[historic]; node[indoormark="beacon"]; way[indoormark="beacon"]; relation[indoormark="beacon"]; way[landuse="construction"]; relation[landuse="construction"]; node[leisure]; way[leisure~"^(pitch|park|garden|playground|sports_centre|stadium|golf_course|marina|water_park)$"]; relation[leisure~"^(pitch|park|garden|playground|sports_centre|stadium|golf_course|marina|water_park)$"]; node[office]; way[office]; relation[office]; node[railway~"^(platform|station|subway_entrance|tram_stop)$"]; way[railway~"^(platform|station)$"]; relation[railway~"^(platform|station)$"]; node[shop]; way[shop]; relation[shop]; node[tourism]; way[tourism]; relation[tourism]; way[zoo="enclosure"]; relation[zoo="enclosure"]; ); out geom;

p16 (hgv restrictions bbox)

[out:json][timeout:120]; (way["maxheight"](41.2400,-73.5100,42.8900,-69.9300); way["maxweight"](41.2400,-73.5100,42.8900,-69.9300); way["maxlength"](41.2400,-73.5100,42.8900,-69.9300); way["maxwidth"](41.2400,-73.5100,42.8900,-69.9300); way["hazmat"](41.2400,-73.5100,42.8900,-69.9300); way["hgv"](41.2400,-73.5100,42.8900,-69.9300); node["maxheight"](41.2400,-73.5100,42.8900,-69.9300); node["maxweight"](41.2400,-73.5100,42.8900,-69.9300); ); out geom;

p17 (anchored regex around)

[out:json][timeout:15]; (node["amenity"~"^(restaurant|fast_food|cafe|pharmacy|school|food_court)$"](around:4828,42.3601,-71.0589); way["amenity"~"^(restaurant|fast_food|cafe|pharmacy|school|food_court)$"](around:4828,42.3601,-71.0589); node["shop"~"^(supermarket|grocery|convenience|bakery)$"](around:4828,42.3601,-71.0589); way["shop"~"^(supermarket|grocery|convenience|bakery)$"](around:4828,42.3601,-71.0589); node["leisure"~"^(park|recreation_ground|fitness_centre|fitness_center|dog_park|playground)$"](around:4828,42.3601,-71.0589); way["leisure"~"^(park|recreation_ground|fitness_centre|fitness_center|dog_park|playground)$"](around:4828,42.3601,-71.0589); relation["leisure"~"^(park|recreation_ground)$"](around:4828,42.3601,-71.0589); ); out center tags;

p18 (fuel bigbbox)

[out:json][timeout:420]; (node["amenity"="fuel"](40.5,-80.0,46.6,-69.7); way["amenity"="fuel"](40.5,-80.0,46.6,-69.7); ); out center;

p19 (venues bbox name ci)

[out:json][timeout:90]; (node["amenity"~"community_centre|arts_centre|theatre|cinema|concert_hall|events_venue|convention_centre|conference_centre|social_facility|public_building"](41.80,-72.00,43.05,-70.10); node["leisure"~"sports_hall|sports_centre|ice_rink|arena"](41.80,-72.00,43.05,-70.10); node["building"~"gymnasium|sports_hall|civic|hall|public|community_centre|grandstand|sports_centre"](41.80,-72.00,43.05,-70.10); node["amenity"="gym"](41.80,-72.00,43.05,-70.10); node["name"~"town hall|community center|community centre|recreation center|civic center|function hall|grange hall|vfw post|american legion|masonic|banquet|convention center|fairgrounds|exhibition center",i](41.80,-72.00,43.05,-70.10); way["amenity"~"community_centre|arts_centre|theatre|cinema|concert_hall|events_venue|convention_centre|conference_centre|social_facility|public_building"](41.80,-72.00,43.05,-70.10); way["leisure"~"sports_hall|sports_centre|ice_rink|arena"](41.80,-72.00,43.05,-70.10); way["building"~"gymnasium|sports_hall|civic|hall|public|community_centre|grandstand|sports_centre"](41.80,-72.00,43.05,-70.10); way["name"~"town hall|community center|community centre|recreation center|civic center|function hall|grange hall|vfw post|american legion|masonic|banquet|convention center|fairgrounds|exhibition center",i](41.80,-72.00,43.05,-70.10); relation["amenity"~"community_centre|arts_centre|theatre|cinema|concert_hall|events_venue|convention_centre|conference_centre|social_facility|public_building"](41.80,-72.00,43.05,-70.10); relation["leisure"~"sports_hall|sports_centre|ice_rink|arena"](41.80,-72.00,43.05,-70.10); relation["building"~"gymnasium|sports_hall|civic|hall|public|community_centre|grandstand|sports_centre"](41.80,-72.00,43.05,-70.10); ); out center tags;

p20 (office around25k)

[out:json][timeout:30]; (node["office"="hr"](around:25000,42.3601,-71.0589); way["office"="hr"](around:25000,42.3601,-71.0589); node["office"="consulting"]["name"~"hr|human resource|talent|workforce|people",i](around:25000,42.3601,-71.0589); way["office"="consulting"]["name"~"hr|human resource|talent|workforce|people",i](around:25000,42.3601,-71.0589); ); out body center qt;

Testing

  • Byte-identical output to upstream/master on the 6 and 20 queries above.
  • An earlier version compared the candidate's coarse index with the ranges as a point, and lost 27 ways in the dense box whose cell only partly overlaps them; the overlap test fixes that. Another counted only local tag blocks for the late check and switched node["maxheight"]["highway"] to it in the dense box, which then read the node skeletons of every candidate cell (+7% instructions overall); counting skeleton blocks as well fixes that (dense box now 13.21 → 11.17 G instructions).
  • CI: fuzz, history and props run on this PR. This change reorders work for every tag-filtered query statement, so their results matter most.

Limitations and open questions

  • The block counts are estimates of work, not measurements: a key whose index range spans few blocks is assumed cheap.
  • Past-date ([date:]) lookups keep the old order and don't drop candidates by range.
  • The constraints' ranges are now computed even when the tag lookup finds nothing, a small extra cost for constraints whose ranges are expensive to compute.


Generated by Claude Code

…e lookup

With a bbox of 1 square degree or more, a query statement with tag filters
looks up ids in the global tag index (collect_ids) and filters by the bbox
afterwards. It looked keys up in the order written, and each further key
read its whole index range, binary-searching every entry against the
candidates so far. For way["maxheight"]["highway"] over open ocean that
meant collecting every maxheight way in the database and then reading the
whole highway index, though none of them could be in the box.

Query_Statement::execute now computes the constraints' node, way and
relation ranges before the tag lookup (they don't depend on it) and passes
them to progress_1 and collect_ids. At the current date:

- keep_ids_inside drops candidates whose cell lies outside those ranges
  after each lookup; when none are left, the lookup stops.
- The key and key-value lookups run smallest first, counted in blocks of
  the global tag index (Block_Backend::count_blocks, from the block index
  alone).
- When checking the remaining tags late would read fewer local tag blocks
  than the remaining lookups would read global ones, the lookup stops and
  the query checks all tags late (check_keys_late = prefer_ranges).

The global tag index stores coarse indices, so a candidate is kept if its
cell overlaps the ranges, as intersect_ranges does.

On a US northeast full-history extract (Geofabrik), warm cache, 4-core VM,
median CPU of 5 runs:

  [out:json][timeout:90]; (way["maxheight"]["highway"](39.0,-70.0,40.0,-69.0); way["bridge"]["maxheight"](39.0,-70.0,40.0,-69.0); way["tunnel"]["maxheight"](39.0,-70.0,40.0,-69.0); node["maxheight"]["highway"](39.0,-70.0,40.0,-69.0); ); out center;
      0.24 s -> 0.00 s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01736kJWpJaGbt8ML6jz9N35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

query-perf Query performance change; collected on all-changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tag-filter queries with a large bbox read whole global tag index ranges for candidates outside the box

2 participants