Optimise queries run for creating pagination links

We have been running in to the problem in
https://github.com/pallets/flask-sqlalchemy/issues/518 where
our page loads very slow when viewing a single page of notifications
for a service in the admin app. Tracing this back and using SQL
explain analyze I can see that getting the notifications takes about
a second but the second query to count how many notifications there
are (to work out if there is a next page of pagination) can take up
to 100 seconds.

As suggested in that issue, we do the pagination ourselves.
Our pagination doesn't need us to know exactly how many notifications
there are, just whether there are any on the next page and that can
be done without running the slow query to count how many
notifications in total by using `count_pages=False`.
This commit is contained in:
David McDonald
2021-12-03 17:07:03 +00:00
parent 989ef9c21a
commit c68d1a2f23
2 changed files with 33 additions and 10 deletions

View File

@@ -241,7 +241,8 @@ def get_notifications_for_service(
include_from_test_key=False,
older_than=None,
client_reference=None,
include_one_off=True
include_one_off=True,
error_out=True
):
if page_size is None:
page_size = current_app.config['PAGE_SIZE']
@@ -280,7 +281,8 @@ def get_notifications_for_service(
return query.order_by(desc(Notification.created_at)).paginate(
page=page,
per_page=page_size,
count=count_pages
count=count_pages,
error_out=error_out,
)