Difference between select_related and prefetch_related in Django
Reported in Flipkart interview loops. Framework-specific optimization question for reducing N+1 queries.
Interview scenario
Often asked in Flipkart technical or coding rounds. Prepare a clear spoken answer plus key trade-offs.
Model answer
Try answering aloud first
Cover trade-offs, structure, and a concrete example before revealing the baseline response.
How to frame this at Flipkart: Connect your answer to measurable impact, clarity of thought, and trade-offs the team cares about. Below is a strong baseline response you can adapt with your own project examples.
select_related performs SQL joins and is best for single-valued relations like foreign key or one-to-one. It retrieves related rows in one query, reducing round trips for common object traversals.
prefetch_related runs separate queries and joins in Python, suitable for many-to-many and reverse foreign key relations where joins can explode row counts. Use Django debug toolbar or query logging to confirm actual impact.
Strong responses include caution: over-eager prefetching can increase memory usage and slow response if unused fields are loaded.
Discussion
Comments (0)
Share how this question came up in your loop, or add tips for others preparing.
Log in to comment on this question.