
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
django-mysql-fuzzycount
Advanced tools
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
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.
It has been tested in production, but there are no unit or integration tests.
Copyright © 2013, Educreations, Inc under the MIT LICENSE.
.. _counting all rows
: http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/
.. _pip
: http://www.pip-installer.org/
FAQs
Approximate query counts for MySQL and Django.
We found that django-mysql-fuzzycount demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.