Django

Django Performance

Query optimization, N+1 problem, database indexes, connection pooling, profiling, monitoring

24 mülakat soruları·
Senior
1

What is the most common performance issue related to Django ORM queries?

Cevap

The N+1 problem is the most common performance issue with Django ORM. It occurs when an initial query fetches N objects, then N additional queries are executed to fetch related objects. For example, iterating over articles and accessing their author generates one query per article instead of a single joined query.

2

Which method should be used to solve the N+1 problem with a ForeignKey relationship?

Cevap

select_related() is the appropriate method to solve the N+1 problem with ForeignKey and OneToOneField relationships. It performs a SQL join and fetches related objects in a single query. prefetch_related() is used for ManyToMany relationships or reverse relations (ForeignKey reverse).

3

When should prefetch_related() be used instead of select_related()?

Cevap

prefetch_related() is designed for ManyToMany relationships and reverse relations (ForeignKey reverse). Unlike select_related() which performs a SQL join, prefetch_related() executes a separate query and performs the join in Python. This avoids data duplication that would occur with a SQL join on many-to-many relationships.

4

How to use Prefetch() to customize prefetch_related() queries?

5

What is the main benefit of adding a database index on a field?

+21 mülakat soruları

Bir sonraki mülakatın için Django'de uzmanlaş

Tüm sorulara, flashcards'a, teknik testlere, code review alıştırmalarına ve mülakat simülatörlerine eriş.

Ücretsiz başla