Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7900973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:06:03+00:00 2026-06-03T09:06:03+00:00

Every time I use Admin to list the entries of a model, the Admin

  • 0

Every time I use Admin to list the entries of a model, the Admin count the rows in the table. Worse yet, it seems to be doing so even when you are filtering your query.

For instance if I want to show only the models whose id is 123, 456, 789 I can do:

/admin/myapp/mymodel/?id__in=123,456,789

But the queries ran (among others) are:

SELECT COUNT(*) FROM `myapp_mymodel` WHERE `myapp_mymodel`.`id` IN (123, 456, 789) # okay
SELECT COUNT(*) FROM `myapp_mymodel` # why???

Which is killing mysql+innodb. It seems that the problem is partially acknowledged in this ticket, but my issue seems more specific since it counts all the rows even if it is not supposed to.

Is there a way to disable that global rows count?

Note: I am using django 1.2.7.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:06:04+00:00Added an answer on June 3, 2026 at 9:06 am

    Okay, I think I found a solution. As Peter suggested, the best approach is to work on the count property and it can be done by overriding it with custom query set (as seen in this post) that specialises the count with an approximate equivalent:

    from django.db import connections, models
    from django.db.models.query import QuerySet
    
    class ApproxCountQuerySet(QuerySet):
        """Counting all rows is very expensive on large Innodb tables. This
        is a replacement for QuerySet that returns an approximation if count()
        is called with no additional constraints. In all other cases it should
        behave exactly as QuerySet.
    
        Only works with MySQL. Behaves normally for all other engines.
        """
    
        def count(self):
            # Code from django/db/models/query.py
    
            if self._result_cache is not None and not self._iter:
                return len(self._result_cache)
    
            is_mysql = 'mysql' in connections[self.db].client.executable_name.lower()
    
            query = self.query
            if (is_mysql and not query.where and
                    query.high_mark is None and
                    query.low_mark == 0 and
                    not query.select and
                    not query.group_by and
                    not query.having and
                    not query.distinct):
                # If query has no constraints, we would be simply doing
                # "SELECT COUNT(*) FROM foo". Monkey patch so the we
                # get an approximation instead.
                cursor = connections[self.db].cursor()
                cursor.execute("SHOW TABLE STATUS LIKE %s",
                        (self.model._meta.db_table,))
                return cursor.fetchall()[0][4]
            else:
                return self.query.get_count(using=self.db)
    

    Then in the admin:

    class MyAdmin(admin.ModelAdmin):
    
        def queryset(self, request):
            qs = super(MyAdmin, self).queryset(request)
            return qs._clone(klass=ApproxCountQuerySet)
    

    The approximate function could mess things up on page number 100000, but it is good enough for my case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out something - Every time I use AVAudioPlayer I
Every time i pass some parameters to a JavasScript or jQuery functon, i use
I have to type rvm use ruby-1.9.3 every time I login to my server.
I use VS 2005 and VSS 2005. Every time I close VS I get
My script does not seem to be working. Every time i try to use
OK I love Python's zip() function. Use it all the time, it's brilliant. Every
I can't really ever think of a time when I would use git merge
Everytime I use MySQL's CREATE TABLE AS SELECT ... all the tables/indexes being selected
I want to add few fields to every model in my django application. This
How do I execute a function every time a node is being updated? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.