The Django Python framework allows people to build websites extremely fast. 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. Most of the time the ORM creates the SQL flawlessly, but sometimes the results are less than ideal.

One common database problem is that ORMs can cause N+1 queries. These queries include a single, initial query (the +1), and each row in the results from that query spawns another query (the N). These often happen when you have a parent-child relationship. You select all of the parent objects you want and then when looping through them, another query is generated for each child. This problem can be hard to detect at first, as your website could be performing fine. But as the number of parent objects grows, the number of queries increases as well — to the point of overwhelming your database and taking down your application.

Recently, I was building a simple website that kept track of expenses and expense reports. I wanted to use Sentry’s new Performance tool to assess how the application was performing in production. I quickly set it up using the instructions for Django and immediately saw results.

The first thing I noticed was that the median root transaction was taking 3.41 seconds. All this page did was display a list of reports and the sum of all of the expenses on a report. Django is fast and it definitely shouldn’t take 3.41 seconds.

#data #development #contributed #sponsored

Finding and Fixing Django N+1 Problems
1.15 GEEK