A Related Matter: Optimizing your webapp by using django-debug-toolbar, ... with Christopher Adams
Transcript
By Christopher Adams, Speaker 2, Speaker 3. From DjangoCon US 2024. Published December 6, 2024.
Watch the video Markdown version 4,058 words · auto-generated · en
Summary
Django’s ORM provides a useful abstraction over SQL, but its lazy QuerySets can hide when database queries are actually executed. Christopher Adams uses Django Debug Toolbar to expose an N+1 query problem in a sample blog application, then shows how `select_related` reduces queries for foreign-key and one-to-one relationships, while `prefetch_related` batches many-to-many and reverse foreign-key lookups. He also recommends inspecting database logs and warns that prefetching can load too much data for unusually active users or objects, so performance should be measured at high percentiles rather than by averages alone.
Transcript
Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.
0:00 Introduction to Django Debug Toolbar
Christopher Adams
Hello everyone. Hello Django Khan. This is this talk is about Django Debug Toolbar. And a few optimizations that may be helpful for you. To introduce myself, my name is Christopher Adams. I'm currently at GitHub, previously at Venmo. I'm at Adam C64. Interestingly, there's quite a few people named Chris Adams involved in the Python space. In fact, even in the Django space. I am not A C D H A, who works at the Library of Convent of Congress. I don't know if he's here. I gave a talk years ago where a friend of his was here and thought that he had come to hear him, but he didn't he didn't. So I'm I'm also not the gentleman Chris Adams, a 19
Aries nine nineties era professional wrestler. Some may remember him, but I guess when you have a common name, this is the situation you're in. Django is great, but Django, as we know, is really a set of tools that compose the framework. Tools are great, we all love tools, or we wouldn't be software engineers, but tools can be used in good or bad ways The Django RRM is itself a set of tools or APIs interfaces that you use to interact with the database and represent data. But the key point that you often have to remember is to manage your own expectations for tools.
Many people approach a new tool with a broad set of expectations as to what they think it will do for them. But this may have little correlation with what the project act actually has implemented. As amazing as it would be if they did, unicorns don't exist. You have to look at the thing you're working with and not just assume that it can solve all your problems and that it's completely going to work exactly as you expect it does and never have any never be any mismatch there. The Django RM is an abstraction layer over the database. And abstraction layers are great because they take us away from messy details. You don't have to write SQL anymore. But they're also risky because they take us away from messy details.
2:40 QuerySet Laziness and Immutability
Christopher Adams
So don't forget when working with the ORM, you're far from the ground. You're writing queries using ORM, you're defining models, you're you're making query sets, you're executing the query sets, and that's going to be making the queries. So you're a bit you're a bit far away sometimes from the from the things that are actually going on under the hood, so to speak. So what is the query set interface? Well or before we talk Query sets are objects, okay, and they have certain properties, one of which they are lazy. Another property that query sets have is that they are immutable.
So what does this mean? A lazy object doesn't evaluate until it needs to. And an immutable object never itself changes. At least it's designed never itself to change. It's supposed to represent a query as it will happen, not as it has happened. Many people think of query sets as if they're queries that already happened, but they're actually future instructions in a sense. So in each of these, none of these actually makes a database query. Model objects all doesn't hit doesn't hit the database. If I have a query set and I filter on that, that doesn't hit the database in most cases. And values doesn't hit the database either. It just simply defines a new query set.
And so far no queries have been made. However, these statements do hit the database. List query set. Query set slice. If you iterate over the query set, or if you bool the query set, if query set is an implicit bool operation. These all perform SQL queries. So you see, even just here, many people don't realize this. They're they're confused what's going on. Why isn't this query happening? Or why is this query happening? You're you're far away from kind of what's going on underneath in many cases. So let's take a deep dive here into an example app. A hosting site for blogs. Okay. Take a look at a few models here. Sorry about that blue text that may be a little hard to hard to read actually.
4:49 Example Blog Application
Christopher Adams
We have we're very simplified. We have a blog class, we have posts on the blog and comments on the posts. The blog class, the blog model has a submitter, which is just an auth-user object who submitted the blog. Each post it is references back to the blog that it's it's it's a post on, it's a foreign key. And also is a repres has a representation of a many to many to many representation with users who like that post. And then there's comments. And you have people who submitted the comments and also the post that the comment is on. Okay.
So the first quick view we might write is something like this. Let's say this is a management page where we're seeing all the blogs and not just the ones owned by you. or maybe our application allows users to see other people's blogs here. And but this is you know very simplified. So we're we're we're getting all the blogs. We're iterating over them all. We're printing the name and we're saying it's submitted by this username. Okay. Seems pretty straightforward. And it seems like this is the right way to do things without really thinking very much about it. Reading the Django documentation. Okay, just this seems like a straightforward way to do it. Here's some just sample data I used with the Ipsum lorem
generator text. And some names. And here's our page. Okay, no CSS. Great website here. Okay, so that's our blog list. Now let's look at blogs, the detail page, get the object or 404, filter for the blog we're looking at, so it's like slash one. Okay, the blog ID is gonna be one, and it's gonna get blog one. And then it's going to return the posts for blog one. It's going to render with those posts and the blog. So it's just going to render this page here. Okay, this is the page that it renders. But just take a look. This is the our Django template, okay? We're we're going through all the posts, we're printing the post name, we're saying that if people like this post, it's gonna say liked
by and the name of all the users that like it And then it's going to print all the comments out in bullets. And these are the comments on that on that. Okay, so again, pretty straightforward, reading the Django docs, and it seems like this is the way to do it. And so this is this is what this generates. Okay, we could see this is kind of structured data. Each post has comments, and then there's there's people who like who like the po the post. Okay. Now Stepping back for a second a lot of developers when they're in the beginner intermediate phase will often start wondering what is happening when I do something
8:09 Inspecting Database Queries
Christopher Adams
And but not really know how to figure that out. Okay. What 's happening with the with the database layer. So what SQL queries are happening when I do X? Whatever that thing is, whether it's making HTTP request res on a web page, or it's just in the say the Django shell. You just Like what is going on? I'm defining a query set. Did that hit the database or didn't it? I don't even know. Because the shell doesn't doesn't tell us by default. There's a kind of a few workaround solutions to this. One is this is one I use often. I just import logging, get the logger for the Django for the database backend. Set it to debug level and just stream the results.
And sorry, that text is a little small, but basically now whenever I'm the j when I'm on the Django shell, I t I perform an operation. It the logger will just spew out to standard out if there was a database query. There it is. That's the database query that was used. And if there wasn't a database query, it won't spew anything out. So you can kind of get information dynamically using the Django shell as to whether or not an event happened hitting the database. Another solution, this is something that can be implemented on in production systems with caution because you're gonna be filling up the logs. With new kinds of information, which can kind of over overwhelm the logs sometimes.
So think about it. You might want to set it up on a staging environment so that you know you don't you're not polluting your production logs. Or so but basically if you use Postgres or my or MySQL for example, there's something called statement logging or my MySQL calls it the general log, where you could have your database back and just print all the queries that are happening. And I I've been in situations where we just put it on for say a minute, collect the all the queries that are happening in production, and we can do an analysis on all those queries. So that's kind of helpful. You can also set that up in your development environment by default just to have the general log or the query log on. So you know you can be developing in one window and your other window you have you're tailing
tailing this log and you're just seeing as you're as you're performing actions, you're seeing the queries that are happening. So you can get immediate feedback there. There's other statements you also need besides these are just the main ones. You also want to set up your log destination and things. So you could look at the documentation for your database backend and put some more put some more options in there for to configure these things. But a third solution, which is one thing I'm going to talk about today, and thank you to the last speaker who gave a call-out to this. To help introduce my talk is the Django debug toolbar. This is really a great, great tool, as the previous speaker also said, available with Pip
Install. If you're going to set it up I would in your settings install conditionally so you're not you're not installing it in production. Do not install this on production. So you say if debug equals true, then you kind of will install your the toolbar. And it operates through middleware and Let's see, our first page again, the blog list page that we talked about. Notice on the right here there's this new toolbar that appears, this kind of JavaScript widget that pops out and gives you all sorts of like really interesting information. On a basic level and what is going on when this HTTP request just happened. So the HTTP re HTTP request happened and you're getting information on the right about the thing that just happened.
12:01 N+1 Queries and select_related
Christopher Adams
There's all sorts of things you can dig into, but we're just for this talk, we're gonna look at this the SQL. Notice there's 52 queries here for a single page load. So that that should be the first thing that seems curious. And if we click in, we can take a look. Wow, that's a lot of things going on just for one page load. Okay. And this concept known as the n plus one query where a system runs an operation to fetch a li a list of items, but runs an additional query for each item. So Basically, you know, the in this case we only really want one so I we one list of blogs and that we should be able to
Do that in one query, but we're actually getting other things as well here. And this leads to inefficient performance and can result in a unnecessarily large number of queries. And it's unfortunately an easy bug to introduce using RM frameworks like Django and Rails because of that levels of separation. And here's the culprit, actually, we're iterating all over all the blogs. Now the all the blogs come in one query, but then as we go through each blog, we're dot submitter Is a is a foreign key to auth user. So what that's doing is Django is making another query every iteration for every blog. Oh, get user one, oh get user five, oh get user eleven, oh get user fourteen.
So it's it's making all these unnecessary queries. And so this is where SelectRelated comes in. SelectRelated uses SQL joins that include fields from related objects, okay, the auth user in this case, in a single select statement. This allows Django to fetch related objects in the same database query. And this is only effective for single-value relationships such as foreign key or one-to-one relationships. So in our case, we have the blog, the submitter. Yeah, there's a user, okay, that that has submitted the thing and select related, we see the implementation here, select related submitter. Select related will just now turn our many queries into two.
One that selects the users. And when it selects the blogs. So that's it. We went from 60 queries or 50 queries to two just in very quickly. Each query it i if you're wondering why this is even a problem, has latency. It goes back and forth from your application to the database. So even if the queries themselves are really quick That latency is a killer, especially if your database is in another data center. Okay. Okay, so and it uses an inner join. And as we see there's there's two queries here. Okay. Now this is second problem here. It's a little bit more tricky. We have 44 queries that are used to serve this page.
15:07 Many-to-Many Prefetching
Christopher Adams
Again, looks like a big mess. Looks like a time bomb if this were to go in production on a high-volume website. So I just want to look at a quick thing here. Each post has many likers associated with it, people who liked it. So it's when it's rendering those people's names or the yeah, the likers usernames it what is what Django is doing is it is making many queries. for each post, it's grabbing all the likes, the likers, every single time, every time it goes over the posts. Now how can we make this more optimized? This is what prof prefetch related where prefetch related comes in. This is useful when dealing with many-to-many
relationships. So select related is useful when dealing with foreign keys or one-to-one relationships. Prefetch related is useful when dealing with many-to-many relationships. It's also useful when dealing with reverse foreign keys, because if you think about it, a user has many blogs that they may have, but a blog only has one submitter. So the reverse of that, the user can have many blogs. That's similar to a many-to-many relationship in that there's a lot of things from the from the other side of the relationship. So prefretch related is an optimization where you're dealing with getting many things at once in two from two tables, from two sources. And without this function, Django does a query for each user who likes the comment.
Sorry, I think this is wrong. Django does basically It finds all the likers for each blog. And this causes additionally causes this n plus one problem. Okay. So basically This is going to I'm I'm going to show you. So I if I use prefetch related here, and I should just say this select related and prefetch related are operations on a query set. So in both cases we have the posts query set. I'm using prefetch related and I'm saying what I want to pre-select. And it's it's it's returning another query set. That represent that that that again is I'm calling it posts because it's just add is composing this operation and saying
do this efficiently and do it and pay by fetching the users Who like the posts at the same time. And let's see, hold on a second. So there's another application here. Yeah, there's another optimization. I'm using this to demonstrate that you can do this i it with multiple operations at once or multiple entities at once and that is that a comment has a post And implicitly, a postmodel object has that related name pointing back to it. So a postmodel object has dot comments, post dot comments.
Okay. And what this related name does is it creates an attribute on each post object. So if I say post dot comments, it's gonna trigger a query where it gets those comments, okay? However, the magic here is that once I use SELECT related or prefetch related, the query set that's returned from select related or preset or prefetch related. It's gonna kind of magically know what to do. Because I've told it, I've told it, I want you to pre-fetch the users here, or the likers, in which case they're called. Or I want I want you to pre-fetch I'm going back here. The submitter in the case of the blog. So it's telling Django, get all these things at once.
And Django just makes a complex query, but one query. It runs that query, hits the database in a single query or two queries instead of 50, and it's going to compose those results. In an efficient in a more efficient way than an N plus one situation. Sorry, sorry. Yeah, and here's the point here. Using pre-fetch related, Django fetches all the users for the comments in a single query and then links them in Python. And this can ironically be more efficient than having the database do it for you because there's in situations like this.
Where you had these complicated kinds of relationships. This way, instead of running a new query for each comment, it just runs two queries: one for the comment and one for the related users, avoiding the N plus one problem. Okay. So let's see here. I want to point this out just because it's a little confusing. Sorry. Okay. So Our ultimate solution for this list page is going to be this: likers and comments underscore submitter. So likers represents the users that like my posts. Comment submitter represents the users who submitted the comments
20:31 Combining ORM Optimizations
Christopher Adams
for the posts. And what I'm telling, this is the most complicated of all, and if you don't understand this, you can dive into it in your own time. What I'm doing here though. is I'm telling prefetch related to get all the possible users that are involved in my that I'm going to need to generate my template information. And so I have some users who are the likers of these posts and some users that are submitters of the comments of the post. And as strange as this seems, this is telling Django, these are all the things, so you only have to make a limited set of queries to the users table, not a query for every user. Django is gonna is going to
now render this page in seven queries, whereas before it was 50 or 60. Okay, so again we have an optimization where Django is selecting all these users in batches and not just individually. So select related and prefletch prefetch related are very helpful in these cases where you're doing these implicit queries that seem to come out of nowhere that you didn't understand and that they have to do with just doing simple things where you're where you're accessing attributes And causing these queries by mistake. So Django debug toolbar
can be a tool where you can discover these problems. You see those N plus one queries just stacking up, and you say, This, I think I know how to solve this. You should always think is when you see plus one queries, would select related solve this situation? So besides SQL queries, Django Debug Toolbar gives a bunch of other information that that may be useful to you in your development, but just for the sake of Time being out and just maybe getting a few questions. Just a summary, the query set APIs here implement best practices to reduce unnecessary queries. Select related used for one-to-many or one-to-one relationships, and prefetch related for many-to-many or reverse foreign key relations.
Thank you very much.
Speaker 2
Are there any questions for Chris?
23:03 Questions
Speaker 3
Thank you for the talk. For the pre -fetch related where it's doing the join in Python or in the application. Is could there be a situation where there's too much data and is that something that the developer should be aware of when they're writing?
Christopher Adams
yes. And i especially so there there's many cases especially in large applications with I mean at Venmo we call these certain kinds of users pathological users because they had just thousands of payments in short periods of time which we had to list and query. Or you know they were very active on they were very active on the site. And it especially in users like this, you there's certain assumptions you might make. You say, well, you know, you know a blog post in most cases may only get a certain number of comments before it becomes kind of irrelevant and the comments stop happening.
But there's cases where especially when something goes viral or there's a user on your system that's especially active. you have to kind of monitor or for these things. And so you always have to be looking at the top 99 percentile, 95th percentile in your in timings, for example, when you're looking at HTTP request timings. You shouldn't be just looking at the average. You should be looking on a high volume site, also at how long the 95% and 99% take to happen. In the cases where the like the data topology is unique for a subset of users or subset of entities. And that can be kind of a hard situation. Yeah. But you have to kind of look at those top tier, top percentile situations
And monitor it kind of not only the average response times and things like that.
Speaker 2
Thank you. That's all the time we have. If you have any more questions, he's going to also take questions on at the Hallworth lobby. So round of applause for Christopher, please.