Skip to content

perf: answer chunk item searches from one query at open time - #93

Closed
earlybard wants to merge 1 commit into
developmentseed:mainfrom
earlybard:perf/query-items-once
Closed

earlybard wants to merge 1 commit into
developmentseed:mainfrom
earlybard:perf/query-items-once

Conversation

@earlybard

Copy link
Copy Markdown
Contributor

Every chunk read runs one DuckDB search per time step. Each takes ~30-40 ms here, and they run one at a time on lazycogs' single DuckDB executor, so they cap how fast a chunked load can go.

open() now runs one search with the array's bbox, datetime, filter, ids and sortby, and keeps the results in a small ItemCache. Each chunk's search is answered from it. The cache has the same search() signature, so the backend, explain() and their tests are unchanged.

Results. A year of Geoscience Australia's Sentinel-2 ARD over Canberra, one band at 20 m, chunks={"time": 1, "y": 1024, "x": 1024}, threaded scheduler, two runs each:

Area Items Before After
10 km 140 5.0–6.5 s 2.7–3.4 s
40 km 418 44.9–51.5 s 26.2–27.1 s

The outputs are identical. Before, the 40 km load ran 738 DuckDB searches, taking 31.6 s in all.

Same items as DuckDB.

  • Times: matched as rustac does: start_datetime/end_datetime coalesced with datetime, and a bare date running from midnight to 23:59:59.
  • Footprints: an exact polygon–box test, as ST_Intersects does, holes and MultiPolygons included, rather than a bbox comparison.
  • Order: results keep the open-time query's order, so sortby and the mosaic order don't change.
  • Test: test_item_cache.py checks the cache returns the same items in the same order as DuckdbClient.search, over 80 combinations of awkward footprints and datetime filters.

Also: the backend no longer holds a DuckDB client, so arrays pickle, and dask.distributed workers don't need to read href. That supersedes #92, which I'll close if you take this.

Tradeoffs

  • Memory: every matching item's id, footprint, times and assets are held in memory. That's small for regional loads (418 items here), but a continental year of Sentinel-2 is ~100k items. Happy to make it opt-in, or fall back to per-chunk queries above some item count, if you'd rather.
  • The README pitch: this changes its "no upfront scan" line. open() already scans every matching row to find the time steps; now it also reads their footprints and assets.
  • One extra query at open.

Alternatives we considered

  • Faster DuckDB queries. rustac's search took ~33 ms, a raw query on read_parquet ~26 ms, and a query on a table pre-loaded into DuckDB's memory ~10 ms. That's at best 3× faster, still one at a time, and the client still can't be pickled.
  • A cache per time step. The first chunk to need a time step queries it for the whole array, and later chunks filter that list in memory. That's about 20 lines, with DuckDB keeping all the semantics, but it saves nothing when there's one spatial chunk (the 10 km case stays at 67 searches), and workers still need DuckDB and href.
  • Matching by bbox instead of footprint. It's simpler, but it reads extra items, and can change pixels where a COG has valid data outside its STAC footprint.
  • Pickling alone (fix: make opened arrays picklable for dask.distributed #92). It fixes dask.distributed for default clients, but keeps the per-chunk queries and needs href readable on every worker.
  • In-memory or Arrow input (Support Arrow tables as input #90). It's complementary: it skips the parquet file, but the per-chunk DuckDB queries would stay.

This merges cleanly onto #88, and the combined test suite passes.

🤖 Generated with Claude Code

Every chunk read ran one DuckDB search per time step, about 30-40 ms each
and one at a time on lazycogs' single DuckDB executor. On a year of
Sentinel-2 over 40 km at 20 m with 1024-pixel chunks, those 738 searches
took 31.6 s of a 50 s load.

open() now runs one search, with the array's bbox, datetime, filter, ids
and sortby, and keeps the results in an ItemCache. Each chunk's search is
answered from it, matching footprints and times as DuckDB does, in the
query's order. The cache holds no DuckDB client, so arrays pickle and
dask.distributed workers no longer need to read href.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earlybard

Copy link
Copy Markdown
Contributor Author

Closing in favour of #94: this is a big change to how lazycogs queries, so it's better discussed there first. The branch stays on my fork for reference.

@earlybard earlybard closed this Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant