Improve Chunked Insert Performance - #167
Open
GALHP wants to merge 11 commits into
Open
Conversation
This was referenced Aug 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces
LIMIT/OFFSETchunk pagination with keyset (seek) pagination forchunked reads, removing the deep-OFFSET scan cost that made large-table clones
slow down as they progressed.
Motivation
A ~25M-row table cloned for hours and was aborted. #165's timings showed why:
SELECT … LIMIT ? OFFSET ?slows as the offset grows — the DB scans anddiscards all skipped rows, so per-chunk select time climbs with the page number
regardless of chunk size. (Refs:
hackernoon,
mariadb,
readyset.)
Result: the same table now clones in ~7 min at constant pace, independent
of page number.
What's changed
CloningRunOrchestrator::buildChunkQuery()now builds a seek query — orderby a stable key, fetch the next chunk via a bound row-value comparison
(
WHERE (k1, k2, …) > (?, …) ORDER BY … LIMIT n) instead of a growing OFFSET,advancing a cursor from each chunk's last row. Applies to both the live-transfer
and dump paths.
keysetColumns()):fullseeks by PK;first/lastseekby
sort_by(defaultid) with PK columns appended as a tiebreaker for atotal order. Direction follows the strategy (
ASC/DESC); the rowlimitisstill respected across chunks.
— it transparently uses the original
LIMIT/OFFSET(offsetChunkQuery()).Keyset covers MySQL, MariaDB, PostgreSQL, SQLite.
Values are passed as bindings, not interpolated. Behavior is unchanged for
no-PK tables and SQL Server (old path); the speedup targets the common PK case.
Testing
CloningRunOrchestratorTestcovers the keyset query construction and the OFFSETfallback. Verified end-to-end that row output matches the previous behavior.