Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django SELECT COUNT(*) as "__count" for every query

I'm currently in the process of optimizing my Django app, which is acting as an API for my front-end with the Django Rest Framework. While running my server in debug mode, I've noticed that every time a queryset gets executed, there's a query run right before it that always looks like this:

SELECT COUNT('*') AS "__count" FROM "table_name WHERE ..."

The ... part always mirrors the query that returns the objects that I want. I'm unsure if this is only run in debug, something that the QuerySet object does innately, or an error with my code. Would appreciate some insight as to why this is happening and if it's something I need to worry about

like image 945
taylorc93 Avatar asked Oct 26 '25 13:10

taylorc93


1 Answers

This occurs in Django Rest Framework when you are using paging on a list view:

  • One query to fetch the data for your current page.
  • A second query to calculate the total number of records for the same queryset.
like image 158
Ashley 'CptLemming' Wilson Avatar answered Oct 29 '25 12:10

Ashley 'CptLemming' Wilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!