Skip to main content

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.

Installation

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

$ pip install -U django-mysql-fuzzycount

Usage

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
100
>>> Choice.objects.filter(votes__gt=10).count()  # not an approximation
28

Another common issue is counts in the admin for a model. There is a base ModelAdmin class that you can subclass in your admin.py files to prevent expensive COUNT(*) queries upon loading the admin page. In an admin.py 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

admin.site.register(Choice, ChoiceAdmin)

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

Testing

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

License

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

Release files for django-mysql-fuzzycount 0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-mysql-fuzzycount 0.4
File Size Uploaded
django-mysql-fuzzycount-0.4.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-mysql-fuzzycount 0.4
File Interpreter ABI Platform
django_mysql_fuzzycount-0.4-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 8.5 kB

Release files / django-mysql-fuzzycount-0.4.tar.gz

Download URL django-mysql-fuzzycount-0.4.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
95bf50b3698ab15a1d3be0d22e87d932f70b6eebd3ebfeb66bafc03b9e064939
BLAKE2b-256 checksum
How to use checksums
fad412e9c5a5081ec484210c9d42382e69b95e75179d870feaae0c9c67fa69fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/2.7

Release files / django_mysql_fuzzycount-0.4-py2.py3-none-any.whl

Download URL django_mysql_fuzzycount-0.4-py2.py3-none-any.whl
Size 4.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ef4538aead1f36b350331294e56d1f5e2e826188c5ba11b89f2df40cb226a1e0
BLAKE2b-256 checksum
How to use checksums
7ebd703ceabbe4de242ba73fe7e4f8e2c622b33e9aed618f7af4ab775487234b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/2.7

Release history Release notifications | RSS feed

This release

0.4 This release

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page