Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Approximate query counts for MySQL and Django.

Project Description

When using MySQL, 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.

This only works with MySQL and behaves normally for all other engines.


Install the package django-mysql-fuzzycount from PyPI using pip:

$ pip install -U django-mysql-fuzzycount


There are a couple ways to use the FuzzyCountQuerySet.

You can import and use the provided FuzzyCountManager on your Django models:

from django.db import models

from mysql_fuzzycount.managers import FuzzyCountManager

class Choice(model.Model):
    objects = FuzzyCountManager()

    # ...

Then, doing a count on the Choice model, without any constraints, will approximate the total count:

>>> Choice.objects.count()  # approximation
>>> Choice.objects.filter(votes__gt=10).count()  # not an approximation

Another common issue is counts in the admin for a model. There is a base ModelAdmin class that you can subclass in your files to prevent expensive COUNT(*) queries upon loading the admin page. In an for one of your models:

from django.contrib import admin

from mysql_fuzzycount.admin import FuzzyCountModelAdmin

from myapp.models import Choice

class ChoiceAdmin(FuzzyCountModelAdmin):
    pass, ChoiceAdmin)

Now, when you load the admin page for the Choice model, the count for pagination will be approximate.


It has been tested in production, but there are no unit or integration tests.


Copyright © 2013, Educreations, Inc under the MIT LICENSE.

Release History

This version
History Node


History Node


History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(6.3 kB) Copy SHA256 Hash SHA256
Wheel 2.7 Sep 2, 2014
(4.1 kB) Copy SHA256 Hash SHA256
Source None Sep 2, 2014

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers DreamHost DreamHost Log Hosting