taylorc93
taylorc93

Reputation: 3716

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

Upvotes: 4

Views: 898

Answers (1)

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.

Upvotes: 4

Related Questions