Optimizing your webapp by using django-debug-toolbar
Transcript
From DjangoCon US 2014. Published September 19, 2014.
Watch the video Markdown version 3,999 words · auto-generated · en
Summary
Chris Adams explains that Django’s abstractions, especially the ORM, make database access convenient but can hide expensive behavior. QuerySets are lazy and immutable, so database queries are often deferred until iteration, indexing, or conversion to a list; related model attributes can trigger additional queries when accessed. He demonstrates using Django Debug Toolbar to inspect the SQL generated by a view, identify an N+1 query pattern, and reduce a blog list from dozens of queries to two with `select_related`, which joins foreign-key data in SQL. For many-to-many and reverse relationships, he recommends `prefetch_related`, which performs separate queries and combines the results in Python; together, these methods reduce unnecessary database round trips in views and templates.
Transcript
Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.
0:00 Introduction and Tool Awareness
So just an introduction as was already done. I'm a software engineer at Venmo, my name is Chris Adams. My Twitter GitHub is AdamC64. Interestingly, I'm not the Chris Adams who's also a developer, Python developer, Django developer, and Django contributor. who works at the Library of Congress, AC D H A. So that's not me. And neither of us are the gentleman Chris Adams, a 90s-era professional wrestler who's here for disambiguation's sake. There he is. That's neither of us, just in case you're wondering. Yeah, it's kind of interesting having a very common name and seeing the collisions that can possibly happen. So we're here to talk about Django.
Django is great. We all love using Django, I'm assuming. But Django is really a set of tools. And they those two work together in interesting ways and helpful ways. But at the end of the day, what we're using when we're using Django, when we're writing Django code is a set of tools that work a certain way. So tools are great, and but tools can be used in good or bad ways. Okay. This in one sense is very obvious. I mean but when you're actually going into things it might not be so obvious to you when you're when you're tr when you're using them, especially for the first time. So the Django ORM is one of the tools in
that Django provides to us. It's an ob object relational model easy wrapper over SQL queries, which used to have to use for Used to be much more tedious to be writing. So one thing to remember is that whenever we're using a tool, especially new tool, is we should manage our own expectations for tools. So many people approach a new tool with a broad set of expectations as to what they think the tool will do for them. However, this may have little correlation with what the project actually has implemented. So this is just kind of a long way of saying that tools can be misused, especially if you're new to them. So you know, as amazing as it would be if
they did, unicorns don't exist. Tools are tools are not really perfect. They never can be perfect in this imperfect world. So we have to really understand them and not deceive ourselves, especially when encountering new tools. That this tool is the one panacea that's gonna really get everything. It's gonna understand me, it's gonna know me, it's really gonna, you know, I'm gonna be understood by this tool that I'm using. Actually, that's not true. It's your responsibility to understand the tool and to use it correctly. So the Django ORM among its other among other things you could say about it, is an abstraction layer. So not only is it set of tool is a tool, but it's an abstraction layer.
Abstraction layers are great because they take us away from messy details. But also risky for the same reason. They take us away from messy details. Same reason. Okay. So we don't have to do many things that we used to have to do before because we have the ORM. But we might forget or not understand that the ORM is doing things that we don't expect it to do. So don't forget you're far from the ground when using the ORM. You have You have you have the ORM API itself, you have Django's implementation, you have Python, you know, maybe you misunderstand some way about how di you know dictionaries or keyword arguments work. You have you have lots of layers when you're using the RM, but then you have the s SQL itself and you have your s
4:12 QuerySet Behavior
your specific database backend which might have its own quirks. Might you know manage indexes in a particular way, might do things slightly differently, load things from c return results from cache that you're not expecting things you know and that depends on your on your configuration. So you have a lot of layers here. So one thing that I really like about Django is the query set the programming interface that query sets implicitly give us. And explicitly give us. I mean we can use we can use the query, we can define query sets, we can do many things with them, we can tell them we want certain things to happen. When we when we at when we define query sets.
And so query sets have two features that's worth keeping in mind when you're thinking about them. One is that query sets are lazy. And the other is queries, query sets are immutable. So this is a review for people who might know this already. So what do these terms mean? Lazy means that a query set doesn't evaluate until it needs to. So many people you know don't get the you know really don't realize this. And when you do realize it it's a great aha moment because you're you're realizing what's going on behind the good behind the hood. When you define a query set, you know, you know, model. objects at all, what you're doing is you're you're defining an instruction, which is deferred. It doesn't get executed until it needs to. And there's certain cases when it does execute
And many cases where it doesn't execute. Query sets are also immutable, which means that if you chain them together, you say, you know, I have a query set and then I say dot filter, what's returned from that operation is a brand new query set. However, that new query set in as much as possible inherits all the features and the properties that you wanted of your old query set. So you can chain filters together, you return a query set apply conditional, have another filter that only happens in the case of that conditional. You can do all sorts of things. And every time the query sets are returning and you're getting a new one. So you don't have to worry about the references to the old query set you can you can store one
So c query sets are always new. And so, you know, the Quick examples here, you know, e each of these operations is returns a new query set and does not actually hit the database So worth keeping in mind that you that what's not happening in it it's the same level as what is happening These, however, are situations where query sets are evaluated. There are a few of them. They're in the Django documentation. These are just a sample of three. There are other ones also. Query sets are evaluated when you make a list out of them, when you use an index or range operator and when you iterate over them. So but the interesting thing to
7:21 Problematic Query Patterns
think about here is that you can define a query set very early in your view function. It's only at the for loop, for example, in the third in the third case here, that the query says query set is actually evaluated, the SQL is sent to the database server. So anyway, the things to keep in mind when dealing with query sets. Okay. So I want to highlight some easily some patterns which are problematic. And oftentimes people don't even know that the pattern is problematic because they're really doing things that look very straightforward. I'm writing my view function. Everything looks great. I mean, you know
But actually when I understand what's going on, cer certain things it's it's easy to miss. So let me let me kind of point these out here. So we have we have a sample app which is a blog app, kind of boilerplate. And I've I've taken away like various other attributes of these models. These are just the relations between the models. And One of the features of Django model objects is that when I when I act well look I'll I'll just go through the example so we can say. So we have we have basically blog post and a comment on a post. Submitter, which is a foreign key to user model, post
which has foreign key back to the blog, so you're posting on a blog. And you know, likers, many of the people who like that post, okay and comments. on the post which are done by a particular user and you know the foreign key back to the post that they're the comment on. Pretty basic model, pretty straightforward So here's a view, which can be used to just generate a list of the blogs that are on the site. Maybe this is a management view or something like that. Looks really simple. Here's the template that renders that that view.
Looks really simple. We have a you know a reference to the blog detail page, we put the blog name there, and we say submitted by the person who submitted it, right? Very simple. This seems like just home run, like really easy thing to write, the push it to production, get this thing working. And it generates something like this, okay? As would be expected. Alright, so and now I just want to talk about the detail view for a little bit. The detail view is a way of seeing a particular blog and I you know get the re Get the blog or 404 if the blog doesn't exist. I return I request the posts that are that are for that blog.
And then I render I render that the blog detail HTML This is a little bit more complicated because there's a lot of different features that maybe my users want to see. They want to see who likes the blog, they want to see the comments that are on the blog. And so this is a little bit more complex. So and it generates something like this. Okay, you know, I just use some sample Lorem Ripsum generator. So, you know, we have comments, we have people who liked it. Okay. So you this looks a little bit more complex in terms of how this is generated. So now as Django developers we're left thinking, okay, I've done my job, right? I've I've gotten something out.
11:14 Measuring ORM Performance
I've completed the work, right? It works, right? But really, at the end of the day, as I said, the Jenguram is a tool, and the tool is functioning a certain way One of the main interfaces that we have to towards our data is through the SQL quer the SQL query language. And Django is generating, the Orm is generating SQL. So how do we find out what SQL is being generated to in a particular view? And the problem is if you can't measure it, you never know if there are problems, right? You know, how do you know you there's there's i information lacking and we can theorize based on what we think it's doing, right
This magical, I know what the ORM is doing. Like, you know, I it's just super easy, you know, I I'm using dot all, so it's doing my query there, right? No, it's it's not. There there's lots of false assumptions we can make So really in general, and this goes not just for Django, it goes for most tools and most technology, you need to measure and you need to get an actual printout or a display and transparency into how the tool is functioning and what it's doing. And as precise as possible. The more information you can get here, the more the more power you have as a developer. And I think I think everyone would everyone would agree with me here. So how do we get this information? You know, we can't be naive with our tools. How do we get this information?
12:47 Django Debug Toolbar
Well, one really, really cool project is the Django debug toolbar. If you're not using it, I highly recommend you use it or have some way of getting information that's equivalent to the kind of things that this provides out of the box. So I'm not going to go over how to install it. They have good documentation. It's relatively straightforward. So let's use the Django Debug toolbar to take a look at our pages. So first we'll take a look at the blog list page. So here's our blog list. And notice that one of the cool things that the Django Debug toolbar is it gives us a panel, which is it basically injects HTML JavaScript into the page. and
also the data about the execution of the page. And it gives us these tabs that we can click on. So we can get information about, you know, the timing that it took to generate the HTML Details about the settings, the HTTP headers, the request object. Okay. These are I could speak for a long time about each of them. I'm going to focus for this for this talk on the SQL. And notice that there's 52 queries being generated and being executed to generate this to actually generate this page, which seems like a lot for just a list, right? So like we did we did the thing, right? We used the ORM, it looked really straightforward, we used it, and
what happened here is we actually we didn't realize it. We generated quite a lot of queries. And it this is a bad thing because there's a network round trip that has to happen. The server has to send the query, the And also the receives response and the database server has to process each individual query. So there's a lot of extra overhead for having all these things happening. And oh sorry, if we click on if we click on the SQL button, the page actually this Django Devoke tool guard the tubar actually gives us a list of all the queries that happen, which is kind of cool when you think about it. Here it is. Right here. Visualization gives us a timeline. I cut off a little bit more of the things just for the sake of
Displaying but you should definitely check it out. So you know now our next question is great we have we have the queries here. Where are they being generated? And actually you can click on the plus and you can get that information. The plus to the to the left side. Will give us the full query and also the actual line of code where the query is generated. But I should say here, this is not a query set that's generating it. That that that's already happened. In this particular case, these individual select statements are being executed through the foreign key, blog. submitter So Django, the query sets are lazy and also the model objects are lazy.
15:52 Optimizing Foreign-Key Queries
The foreign key relations aren't going to be accessed, aren't going to be loaded. until they need to be or if they need to be. So that's why it saves in terms of an optim optimization. Maybe you never reference the submitter object. Okay? And so you don't need to have information about that the off-user model object. So here it is, blog. submitter is being referenced, that's generating the these many, many, many individual queries. And the more objects you have, the more queries you have. So this is actually a very bad situation because there should be a way to do this much easier. So this is where Select Related comes in. Select related works by creating a SQL join and including the fields of the related object in the individual select statement.
for the for the model itself. So the auth user is going to be joined with the blog objects in this case. So the SQL is going to return back the blog c mo multiple times as many auth users as there are And you think, well, is this bad? Well, it's actually much more efficient because you you're batching it, you're doing it in one shot, and so there's only one network back and forth to get that information. So it's actually much better, much fewer queries. So it gets a related object in the same database query. And however, to avoid much larger result set that result from many relationships, so like a many to many field as opposed to a foreign key.
or maybe a reverse foreign key when you're where you're where you're getting the opposite side of the relation. Select related won't handle those cases. It'll just handle i think of it in terms of foreign keys and one-to-one fields. It's it's it's helpful and when you're going to be iterating over them in a loop. So here we use select related. We tell it we want to select the submitter and of the blogs and you can have commas and have multiple fields which are which it will join together and get all that all that information. And So now we do it and notice two queries and there they are. Okay, and notice the inner join. On the on the query. So we
18:08 Optimizing Related Collections
we've just completely r reduced the number of queries and even if we have much more users and much more blogs this isn't going to increase So this is really, really great optimization, one a great tool, especially when you're using loops. So all right, the blog detail page now is a little bit more difficult. So now notice we start with 44 queries and if we take a look at the queries in this case it's a it's a lot less straightforward, right? You don't just have a list And what I usually do is I start from the top, open my code, and I actually go down and see where these things are being invoked and I just try to be intelligent, try to identify patterns. Patterns are good. The same thing is happening over and over again. Might be a group of things happening over and over again. And in this case, I'm just going to
reveal it for you here. The patterns that you identify if you take a look at these things are that there is a submit or foreign key being accessed on the comment Okay, so now we're talking about comments that are being generated and who submitted that comment. And there's also a query on the likers attribute, which is if you remember is a many-to-many relation. as people who like the post. So we have comment dot submitter and post likers. So prefix related is an optimization that's used for this case. And instead of using an inner join What Django is going to do is going to pre-fetch those relations, those many to many relations. And it's it's all it then is going to do the operation we expect of our model and it's gonna join them in Python.
And that's actually more efficient than having database do it for you. So there's a reason why select related doesn't do this by default. Prefetch related is going to prefetch those many, many relations because it could just result in a massive you know, you know, result set. So it's much, much better to use it this way. So you remember prefetch related is for many a relation that has many possi many related members. It does a separate lookup for each relationship, does the joining in Python, and it allows it to this allows it to pre-fetch many to many and many to one objects in addition to the Farnkey and one-to-one that is in the Select related that Select related helps you use helps to helps you manage and it also supports generic relation gener generic foreign key I don't know what version of Django that is
That it does support that I would check that but it you can use it for those kinds of things So we put it in here. You can use double underscore syntax, and in this case, this is the most efficient our set of arguments that there that you know at i that that will help us in our particular example. There might be more that I that I overlooked. I didn't spend that much time trying to find the optimal, optimal. But these this simple optimization, this one line of code is gonna is gonna save us So if you see it's going to prefetch through the comments table and get the submitter, and it's also going to have that the comments information, which is going to be helpful for us.
21:12 Optimization Summary
It's going to implicitly do that. And it's going to select the likers. And one line and we're done. Well, you know, we only have seven queries now. Yeah, I'm sure you can get this even smaller. But Gino one line just amazing amount of work that th that you can do If you notice, it prefetches certain things before others. So, you know, it's it's just really, really helpful pattern to think about. and to bring into your own tool toolkit as you as you're working to make your cor make your views better. So in summary, the query set API method, select-related and prefetch related, automate some of these best practices to avoid extra queries in views and in templates.
Okay. And use select related for one-to-many or one-to-one relations, and prefetch related for many to many or many-to-one relations, including the reverse foreign key relations. So yeah that's basically it. So thank thanks. This is pretty straightforward. I hope this will allow you to start using those these tools more effectively. I have I have a code on GitHub, so please clone it. And yeah, thank you very much.