Reuse a boundary layer across queries#
When several spatial queries use the same boundary, retain it in one session so later queries can reuse work. The checked Q10 example prepares a zone boundary once, then runs the same trip-to-zone query repeatedly.
Decide what to retain#
A RegisteredTable keeps an input available across queries. For an operation that accepts prepared geometry, Session.prepare_geometry can also retain decoded geometry and an index. It accepts a Table describing a Parquet file or dataset without reader options. It does not accept a RegisteredTable.
Choose what to retain based on the number of queries remaining and the device memory needed by the boundary, live results and the next query together. A session is bound to one device, and its memory limit covers retained results as well as running query memory.
Run repeated queries against one boundary#
The Q10 fixture joins trips to zones with ST_Within, groups by zone and orders the result deterministically. Point fixture at the checked fixture directory in a source checkout.
from pathlib import Path
import cugeo.geometry as geometry
fixture = Path("cugeo-geometry/crates/execution/tests/fixtures/spatialbench-q10-local-files")
sql = (fixture / "q10.sql").read_text()
zone = geometry.table_from_file(fixture / "zone.parquet")
trip = geometry.table_from_file(fixture / "trip.parquet")
with geometry.Session() as session:
prepared = session.prepare_geometry(zone, geometry="z_boundary")
try:
print(prepared.stats())
first = session.sql(sql, tables={"trip": trip, "zone": prepared})
print(prepared.stats())
warm = session.sql(sql, tables={"trip": trip, "zone": prepared})
assert first.rows == warm.rows
warm_again = session.sql(sql, tables={"trip": trip, "zone": prepared})
assert first.rows == warm_again.rows
print(prepared.stats())
finally:
prepared.release()
Preparing the handle reads nothing. The checked test observes materialized as false and decode, geometry-hit and index-preparation counts of 0, 0, 0 before the first query. After the first query, (decodes, geometry_hits, index_preparations, index_hits) is (1, 0, 1, 0). After two repeats it is (1, 2, 1, 2). The prepared boundary has eight rows in one file. The equal ordered rows show result parity across the repeated queries. The hit counts show reuse of the geometry and index.
The test also compares the projected rows with the fixture’s expected results, normalizing duration values to microseconds. These are checked test results, rather than output from a new run of the example.
Release or replace the boundary#
Release the prepared handle after its last query. If the boundary files change, create a fresh preparation. The mutation test replaces a source file and checks for a native IO_ERROR. Do not rely on an existing preparation for changed files.
For an already resident input, registration is a separate choice. With Session.table_from_cudf(..., copy=False), retain the producer’s immutable buffers through the final consumer. With Session.table_from_arrow(..., copy=False), a single CUDA batch is borrowed, its event is waited on, and the immutable batch is retained through release. Neither registered table is an input to prepare_geometry.
Memory for the next query#
If the next query no longer fits, see Process a result that strains GPU memory to assess the memory held by live results.