Category: Database

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.

Related Articles