Phuks-co / throat

Open Source link aggregator and discussion platform powering Phuks
https://phuks.co
MIT License
74 stars 32 forks source link

Speed up the get_notifications query for users with many notifications #445

Closed happy-river closed 3 years ago

happy-river commented 3 years ago

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.