r/django • u/turkcelestirgen • 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!
1
3
u/jvlomax 4d ago
How does this differ from the new fetch modes?