Django

Django Performance

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

24 câu hỏi phỏng vấn·
Senior
1

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

Câu trả lời

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?

Câu trả lời

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()?

Câu trả lời

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 câu hỏi phỏng vấn

Nắm vững Django cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí