Giter Site home page Giter Site logo

django-mysql-fuzzycount's Introduction

Django MySQL Fuzzycount

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.