-
Notifications
You must be signed in to change notification settings - Fork 1
perf: replace OFFSET pagination with ID-range batching in async jobs #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kneckinator
wants to merge
3
commits into
19.0
Choose a base branch
from
worktree-perf+phase6-keyset-pagination
base: 19.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Part of OpenSPP. See LICENSE file for full copyright and licensing details. | ||
| """Keyset pagination utilities for async job dispatch. | ||
|
|
||
| OFFSET-based pagination causes PostgreSQL to scan and discard N rows for | ||
| OFFSET N, making later batches progressively slower (O(N) per batch). | ||
|
|
||
| This module provides ID-range batching using the NTILE window function, | ||
| which pre-computes (min_id, max_id) boundaries in a single query. Each | ||
| job then uses WHERE id BETWEEN min_id AND max_id, which is O(1) via the | ||
| primary key index regardless of batch position. | ||
| """ | ||
|
|
||
| import math | ||
|
|
||
|
|
||
| def compute_id_ranges(cr, table, where_clause, params, batch_size): | ||
| """Compute ID-range boundaries for parallel job dispatch. | ||
|
|
||
| Uses PostgreSQL's NTILE window function to split matching rows into | ||
| roughly equal-sized buckets, then returns the (min_id, max_id) of each. | ||
|
|
||
| :param cr: Database cursor | ||
| :param table: Table name (e.g. 'spp_program_membership') | ||
| :param where_clause: SQL WHERE clause without 'WHERE' keyword | ||
| (e.g. 'program_id = %s AND state IN %s') | ||
| :param params: Tuple of parameters for the WHERE clause | ||
| :param batch_size: Target number of rows per batch | ||
| :return: List of (min_id, max_id) tuples, ordered by min_id | ||
| """ | ||
| # Get total count to calculate number of batches | ||
| cr.execute( | ||
| f"SELECT COUNT(*) FROM {table} WHERE {where_clause}", # noqa: S608 # nosec B608 | ||
| params, | ||
| ) | ||
| total = cr.fetchone()[0] | ||
| if total == 0: | ||
| return [] | ||
|
|
||
| num_batches = math.ceil(total / batch_size) | ||
| if num_batches <= 1: | ||
| cr.execute( | ||
| f"SELECT MIN(id), MAX(id) FROM {table} WHERE {where_clause}", # noqa: S608 # nosec B608 | ||
| params, | ||
| ) | ||
| row = cr.fetchone() | ||
| return [(row[0], row[1])] if row and row[0] is not None else [] | ||
|
|
||
| # Use NTILE to split rows into equal-sized buckets, then get | ||
| # the min/max ID of each bucket as the range boundaries. | ||
| cr.execute( | ||
| f""" | ||
| SELECT MIN(id) AS min_id, MAX(id) AS max_id | ||
| FROM ( | ||
| SELECT id, NTILE(%s) OVER (ORDER BY id) AS tile | ||
| FROM {table} | ||
| WHERE {where_clause} | ||
| ) sub | ||
| GROUP BY tile | ||
| ORDER BY min_id | ||
| """, # noqa: S608 # nosec B608 | ||
| (num_batches, *params), | ||
| ) | ||
| return cr.fetchall() |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kneckinator is this really not intended to be added to the init.py?