Git Product home page Git Product logo

django-auto-prefetch's Introduction

django-auto-prefetch

Automatically prefetch foreign key values as needed.

Purpose

When accessing a ForeignKey or OneToOneField (including in reverse) on a model instance, if the field's value has not yet been loaded then auto-prefetch will prefetch the field for all model instances loaded by the same queryset as the current model instance. This is enabled at the model level and totally automatic and transparent for users of the model.

Usage

Change all these imports from django.db.models to auto_prefetch:

  • ForeignKey
  • Manager
  • Model
  • OneToOneField
  • QuerySet

For example, if you had:

from django.db import models


class Book(models.Model):
    author = models.ForeignKey('Author', on delete=models.CASCADE)

...swap to:

import auto_prefetch
from django.db import models


class Book(auto_prefetch.Model):
    author = auto_prefetch.ForeignKey('Author', on delete=models.CASCADE)

If you use custom subclasses of any of these classes, you should be able to swap for the auto_prefetch versions in your subclasses' bases.

After doing this, run python manage.py makemigrations to generate migrations, to set the Meta.base_manager_name option to prefetch_manager on every model you've converted. This is to make sure auto-prefetching happens on related managers. If you instead set Meta.base_manager_name on your models, make sure it inherits from auto_prefetch.Manager.

Background & Rationale

Currently when accessing an uncached foreign key field, Django will automatically fetch the missing value from the database. When this occurs in a loop it creates 1+N query problems. Consider the following snippet:

for choice in Choice.objects.all():
    print(choice.question.question_text, ':', choice.choice_text)

This will do one query for the choices and then one query per choice to get that choice's question.

This behavior can be avoided with correct application of prefetch_related() like this:

for choice in Choice.objects.prefetch_related('question'):
    print(choice.question.question_text, ':', choice.choice_text)

This has several usability issues, notably:

  • Less experienced users are generally not aware that it's necessary.
  • Cosmetic seeming changes to things like templates can change the fields that should be prefetched.
  • Related to that, the code that requires the prefetch_related() (e.g. the template) may be quite removed from where the prefetch_related() needs to be applied (e.g. the view).
  • Subsequently finding where prefetch_related() / select_related() calls are missing is non-trivial and needs to be done on an ongoing basis.
  • Excess entries in prefetch_related() calls are even harder to find and result in unnecessary database queries.
  • It is very difficult for libraries like the admin and Django Rest Framework to automatically generate correct prefetch_related() clauses.

On the first iteration of the loop in the example above, when we first access a choice's question field, instead of fetching the question for just that choice, auto-prefetch will speculatively fetch the questions for all the choices returned by the QuerySet. This change results in the first snippet having the same database behavior as the second while reducing or eliminating all of the noted usability issues.

Some important points:

  • ManyToManyFields are not changed at all.
  • Because these are ForeignKey and OneToOneFields, the generated queries can't have more result rows than the original query and may have less. This eliminates any concern about a multiplicative query size explosion.
  • This feature will never result in more database queries as a prefetch will only be issued where the ORM was already going to fetch a single related object.
  • Because it is triggered by fetching missing related objects it will not at all change the DB behavior of code which is fully covered by prefetch_related() and/or select_related() calls.
  • This will inherently chain across relations like choice.question.author. The conditions above still hold under such chaining.
  • In some rare situations it may result in larger data transfer between the database and Django (see below).

An example of that last point is:

qs = Choice.objects.all()
list(qs)[0].question

Such examples generally seem to be rarer and more likely to be visible during code inspection (vs {{ choice.question }} in a template). And larger queries are usually a better failure mode than producing hundreds of queries. For this to actually produce inferior behavior in practice you need to:

  • fetch a large number of choices
  • filter out basically all of them
  • in a way that prevents garbage collection of the unfiltered ones

If any of those aren't true then automatic prefetching will still produce equivalent or better database behavior than without.

See Also

P.S.

If you have concerns go look at the code, it's all in auto_prefetch/__init__.py and is fairly short.

django-auto-prefetch's People

Contributors

adamchainz avatar danpalmer avatar graingert avatar tolomea avatar

Watchers

 avatar

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.