DevOps Articles

Curated articles, resources, tips and trends from the DevOps World.

Finding and Fixing Django N 1 Problems

4 years ago thenewstack.io
Finding and Fixing Django N 1 Problems

Summary: This is a summary of an article originally published by The New Stack. Read the full original article here →

One of its best features is the Object-relational mapper (ORM), which allows you to make queries to the database without having to write any SQL. Django will allow you to write your queries in Python and then it will try to turn those statements into efficient SQL.

All this page did was display a list of reports and the sum of all of the expenses on a report.

It was at this point that I saw that I had a single query to fetch all of the reports and then another query for each report to fetch all of the expenses —an N+1 problem.

This cuts the number of database queries in half, since we’re now making one query to fetch all of the Reports, one query to fetch all of the expenses, and then n queries in report.get_expense_total.

Made with pure grit © 2024 Jetpack Labs Inc. All rights reserved. www.jetpacklabs.com