-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
Use range-based pagination for versions #1066
Merged
Merged
Conversation
This file contains 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
Mr0grog
added a commit
that referenced
this pull request
Jan 24, 2023
Before we can actually do range-based pagination in any usefully workable way, we need to have indexes in place that support it (partially because the primary key for versions is a UUID, which is unordered -- this wouldn't be such a big deal with bigint). These indexes will take a while to build, so it's good to get these in place ahead of time. A future change will remove the old, non-compound indexes that will be redundant after these new ones finish building. Addresses part of #571, and required before deploying #1066.
Mr0grog
added a commit
that referenced
this pull request
Jan 24, 2023
Before we can actually do range-based pagination in any usefully workable way, we need to have indexes in place that support it (partially because the primary key for versions is a UUID, which is unordered -- this wouldn't be such a big deal with bigint). These indexes will take a while to build, so it's good to get these in place ahead of time. A future change will remove the old, non-compound indexes that will be redundant after these new ones finish building. Addresses part of #571, and required before deploying #1066.
This implements the logic for #579 in an ugly, inline way so I could test it out. It definitely needs a lot of cleanup before it can be merged. Also needs a migration to add indexes.
Mr0grog
force-pushed
the
579-performant-pagination-for-versions
branch
from
January 24, 2023 21:46
8e40414
to
0644db7
Compare
|
Mr0grog
added a commit
to edgi-govdata-archiving/web-monitoring-ops
that referenced
this pull request
Jan 25, 2023
Mr0grog
added a commit
to edgi-govdata-archiving/web-monitoring-ops
that referenced
this pull request
Jan 25, 2023
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.
This is a super rough first cut at the logic for range-based pagination for versions (see #579). It’s ugly, problematic, and absolutely not mergeable as-is, but lets us test things out. Still needs:
(capture_time, uuid)
,(created_at, uuid)
maybe also(page_uuid, capture_time, uuid)
,(page_uuid, created_at, uuid)
).Update 2023-01-25: I’m not going to extend this to other endpoints. While it would be nice, the real priority right now is doing the minimum required for edgi-govdata-archiving/web-monitoring#168
Overall Summary
The slow performance of offset-based pagination (when you get a batch of results with a query like
SELECT xyz FROM table OFFSET = n LIMIT = m
) is a serious problem on the theversions
table (really any large table, but in practice it’s justversions
for this app). This solves the problem by using range-based pagination (skipping to the place you want in the result set using aWHERE
clause that filters to a range of values from at the same field(s) you are sorting by).For the versions table, that means doing queries based on a different minimum/maximum
(capture_time, uuid)
or(created_at, uuid)
for each page/batch/chunk of results rather than using SQL'sOFFSET
clause to get to the chunk we want. This is more-or-less compatible with with any existing client, since thenext
link still works and thechunk_size
parameter also works the same.On the other hand, this introduces some new restrictions:
You can only sort by one field, and it must be either
capture_time
orcreated_at
instead of any field(s) you want. We need an index for the fields we want to sort by, and these two are the ones we chose.Clients shouldn’t usually be setting the
chunk
parameter on their own, and will run into problems if they do, since that field is no longer an integer (now it is either a version UUID or a combined"<timestamp>,<uuid>"
string).The
?include_total=true
parameter is no longer supported for versions. There’s just no performant way to do it without additional caching of that field somewhere (count()
in Postgres always requires a full table scan; the entire point of this change is to prevent us from doing large scans in order to paginate results).There’s also plenty in here that’s not perfectly clean, and not totally generalized. If we wanted to utilize this kind of pagination for other models/controllers, it would need some work to make more abstract.