Django Optimization: Subqueries and Search Relationships

I have legacy code that uses an ORM subquery that produces an SQL SELECT query with JOIN and conditions that also contain SELECT and JOIN. This request takes a long time to complete. By the way, when I execute this query in raw SQL taken from Django_ORM_query.query, it executes with a reasonable amount of time.

What are the best optimization methods in such cases?
Will the query run faster if I use the ManyToManyand relationships ForeignKey?

+5
source share
2 answers

Django , . django-debug-toolbar, , , , . , django, , .

, select_related() prefetch_related().

20-30 , . 5-10 . , . , 100 .

, - , . , , defer() only(), , , , .

+7

raw SQL. Django.

+5

All Articles