Some of our users have accumulated several thousand notifications, and some of those users have made upwards of 50,000 votes. This is causing the query in Notification.get_notifications to take several seconds to complete for those users. A query plan generated by Postgres showed that most of that time was spent joining the vote tables, and then most of that work was discarded by the pagination limit.
Improve the performance of this query by breaking it into two queries, one which returns most of the notification information and the second which fetches vote information for only the 50 notifications which will be shown on the page.
Some of our users have accumulated several thousand notifications, and some of those users have made upwards of 50,000 votes. This is causing the query in
Notification.get_notifications
to take several seconds to complete for those users. A query plan generated by Postgres showed that most of that time was spent joining the vote tables, and then most of that work was discarded by the pagination limit.Improve the performance of this query by breaking it into two queries, one which returns most of the notification information and the second which fetches vote information for only the 50 notifications which will be shown on the page.