OFFSET vs keyset pagination for sequential export of an append-only BigQuery table
0 reputation · 26 May 2021, 20:26 UTC
0 reputation · 26 May 2021, 20:26 UTC
Goal is to bound and page through a large append-only BigQuery table with stable monotonic ordering for sequential export. The constraint is per-page processed bytes and result consistency across pages, with no random access requirement.
Two documented approaches are OFFSET-based pagination with LIMIT and keyset pagination using a filter on the last seen ordered value. Client libraries also expose page tokens that preserve a single query execution snapshot for the result set lifetime.
For an append-only table with a unique ordering key, the trade-off is between simple offset arithmetic and stable per-page cost with keyset filtering, versus snapshot consistency provided by page tokens with limited lifetime.
Does a page token guarantee consistent rows across pages without re-evaluating skipped rows for the duration of the result set? How does token expiration affect deep sequential reads when the result set lifetime is exceeded? Is keyset pagination the only documented way to keep per-page processed bytes roughly constant for sequential access beyond token lifetime?
35275 reputation · 27 May 2021, 03:55 UTC
For a large append-only BigQuery table, the choice between page tokens and keyset pagination depends on the expected duration of the export process and the requirement for stable cost per page.
A page token provides a consistent view of the data because it indexes a materialized result set (the temporary table created by the query job). When you use a page token, BigQuery does not re-evaluate the query or re-scan skipped rows; it simply retrieves the next slice of the already-computed results.
Keyset pagination (using WHERE key > last_seen_value ORDER BY key LIMIT n) is the documented method for sequential access that must persist beyond the lifetime of a single query snapshot.
OFFSET, which requires BigQuery to scan and discard all preceding rows (leading to linearly increasing costs), keyset pagination allows the engine to seek directly to the last seen value.Keyset pagination is the only way to keep processed bytes roughly constant for deep sequential reads beyond the token lifetime, but this is conditional on the schema. To achieve constant cost, the ordering key must be a partitioning column or a clustering column. If the key is a standard column, BigQuery may still scan the entire column to evaluate the filter, though it will still be more efficient than OFFSET.
SELECT * FROM table ORDER BY key LIMIT 1000.key of the last row processed in the current batch.SELECT * FROM table WHERE key > [last_seen_key] ORDER BY key LIMIT 1000.To verify the cost behavior, compare the totalBytesProcessed in the job statistics for a page at the start of the table versus a page near the end. In a keyset approach with clustering, these values should remain relatively stable; with OFFSET, the value will grow significantly.
Diagnostic Detail Needed: Is the ordering key used for pagination a partitioning or clustering column? If not, the "constant processed bytes" benefit may be significantly reduced.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.