Optimise queries run for creating pagination links

We have been running in to the problem in
pallets/flask-sqlalchemy#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 analagous to
c68d1a2f23

The only difference is that in that case, the pagination links are
used to show prev and/or next links in the admin app. In this case,
the pagination links are only used to see if there is a page 2, and
if there is, say that we are only showing the first 50 results.
This commit is contained in:
David McDonald
2021-12-10 12:06:55 +00:00
parent edadeb9131
commit 7d8eed8228
3 changed files with 21 additions and 3 deletions

View File

@@ -629,6 +629,7 @@ def dao_get_notifications_by_recipient_or_reference(
statuses=None,
page=1,
page_size=None,
error_out=True,
):
if notification_type == SMS_TYPE:
@@ -679,7 +680,7 @@ def dao_get_notifications_by_recipient_or_reference(
results = db.session.query(Notification)\
.filter(*filters)\
.order_by(desc(Notification.created_at))\
.paginate(page=page, per_page=page_size)
.paginate(page=page, per_page=page_size, count=False, error_out=error_out)
return results