Approximate Counting in Django and Postgres

Posted on 3 June 2024
As your Django project grows, you may notice that the admin site takes increasingly longer to load. This is mainly due to Django's paginator, which relies on the slow SELECT COUNT(*) query to calculate the number of rows in a table. At some point, this query itself will account for more than 95% of the page's loading time.
But the issue isn't limited to the admin site. Simply calling count() on your queryset will result in the same problem.
Publisher: testdriven.io
Topics:
#code
#django
#python