r/django 5d ago

Models/ORM django-fk-optimize

As you know, django has two database functions for N+1 resolutions,namely select_related and prefetch_related . These functions are useful when you model has fk’s that can cause N+1 issues, and tools such as django-auto-prefetch help you to automatically apply them to your queries to provide you the most optimal query.

However sometimes you need to actually know the shape of the data you are working with to decide which one of these functions you should use.
For example if you have many2one relationships, prefetch may win over select related if the relationship looks like owners of posts, where one owner porbably has many posts as opposed to a many2one relationship that had near identical counts.
So sometimes the function you need to use depends on your data shape, and thats why I am developing this tool called django-fk-optimize that is a management command that lets devs test their models and tables in prod enviroments to decide which function is the best for which field of a model.

Here is the link to the repo: github

I would love to hear every bit of criticism , thanks in advance!

6 Upvotes

4 comments sorted by

3

u/jvlomax 4d ago

How does this differ from the new fetch modes

1

u/turkcelestirgen 2d ago

Vastly, fetch modes is for when you want to fetch extra data but for specific conditions. This project is more for when the optimization depends when the shape of the data. So you just use this to know how the shape and distribution of your tables/relations look and then use that to optimize, though its fairly young and needs more development

1

u/Knudson95 4d ago

Thought this was gonna be a package slagging off optimization so disappointed

1

u/turkcelestirgen 2d ago

I mean its kind of an optimization layer on top of the existing ones