Skip to main content

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.

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

Run the tests with:

pip install -r requirements.txt
python runtests.py

License

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

Project details


Download files

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

Source Distribution

django-mysql-fuzzycount-0.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

django_mysql_fuzzycount-0.1-py2.py3-none-any.whl (6.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-mysql-fuzzycount-0.1.tar.gz.

File metadata

File hashes

Hashes for django-mysql-fuzzycount-0.1.tar.gz
Algorithm Hash digest
SHA256 adbdf4024fe12394cd91af3777bab3f7ef4b895e79e5ce56257d09508ed2f4b3
MD5 a9e67c6022b36c9bdd76992663a45deb
BLAKE2b-256 f60540ded2f32a01af97f9ea581c79b8af4baeab375f73798ccf48f7fa91bdc3

See more details on using hashes here.

Provenance

File details

Details for the file django_mysql_fuzzycount-0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_mysql_fuzzycount-0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 47b84aca6e94e1f752227b13794c42facf41bbc0da0ba551c5042ec4846e05f5
MD5 e723323748bd561b523940e75c0f6ddb
BLAKE2b-256 a4d8edd62309b8a0445fb5978ae9c89cb82b834a3b38ea300b7e4272d12e89d2

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page