Skip to main content

Created by Stephen McDonald

Introduction

Up until PostgreSQL 9.2, COUNT queries generally required scanning every row in a database table. With millions of rows, this can become quite slow. One work-around for this is to query statistics in PostgreSQL for an approximate row count, which in many cases is an acceptable trade-off.

Given a table called bigdata, the following query will return an approximate row count:

SELECT reltuples FROM pg_class WHERE relname = 'bigdata';

You can read more information about slow COUNT queries in PostgreSQL in the PostgreSQL wiki.

What django-postgres-fuzzycount provides is a way of using this approach directly in your Django model managers. It was originally built for displaying statistics in the kouio RSS reader, a popular alternative to Google Reader, that acquired over 5 million news articles in its database during the first week of its launch.

Installation

The easiest way to install django-postgres-fuzzycount is directly from PyPi using pip by running the following command:

$ pip install -U django-postgres-fuzzycount

Otherwise you can download and install it directly from source:

$ python setup.py install

Usage

By using the fuzzycount.FuzzyCountManager on your Django models, its count() method will return an approximate value when querying PostgreSQL tables without any WHERE OR HAVING clauses:

from django.db import models
from fuzzycount import FuzzyCountManager

class BigData(models.Model):

    big = models.BooleanField(default=True)
    data = models.TextField()

    objects = FuzzyCountManager()

BigData.objects.count()  # Uses fuzzycount
BigData.objects.filter(id__gt=9000).count()  # Doesn't use fuzzycount

The fuzzycount.FuzzyCountManager also checks the database engine being used, and only applies the approximate count query when using PostgreSQL, so other database backends can be used and will behave as usual (for varying definitions of usual, depending on the database :-).

Inspiration

Release files for django-postgres-fuzzycount 0.1.7

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-postgres-fuzzycount 0.1.7
File Size Uploaded
django-postgres-fuzzycount-0.1.7.tar.gz 4.0 kB Details

Built distribution (wheel)

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

Total release size: 9.7 kB

Release files / django-postgres-fuzzycount-0.1.7.tar.gz

Download URL django-postgres-fuzzycount-0.1.7.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b9e2c431d013a73965d76a369086f3f8bc97d0774dff635b9881470d88aff821
BLAKE2b-256 checksum
How to use checksums
7dc193d41c0d9265094d58644b4287597e866885c9ecf021f5bc92aab26b8e64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_postgres_fuzzycount-0.1.7-py2.py3-none-any.whl

Download URL django_postgres_fuzzycount-0.1.7-py2.py3-none-any.whl
Size 5.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
aba4d96f31f57d2a5a50804e4ea6c3bca56ca6e255fe9515965134eae9b786a0
BLAKE2b-256 checksum
How to use checksums
ace465a109c081146b53436fcc333800fafbaf5a2b7361baa13b618a5a1d3971
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.7 This release

2 release files

0.1.6

2 release files

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

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