MongoDB with Async Python Transcripts
Chapter: Performance Tuning
Lecture: Levers and Knobs of MongoDB Performance

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So you've heard that MongoDB is fast, and yet you go and run a query, and here you can see it's taking 0.7 seconds.
0:11 Now, I don't know where you're coming from or your perspective, that may seem actually somewhat fast, but to me, that's slow, slow, slow.
0:22 We should be able to do so much better than going and getting our data back in a second or so. So in this chapter, like I said,
0:30 we're gonna make it fast, we're gonna get that same answer back, but this time, it's going to fly.
0:37 You can see now we got that 700 millisecond request down to be 700 times faster to being just one millisecond. There we go.
0:49 So what knobs and levers do we have to turn to make MongoDB fast? What is available to us to actually affect and control how it runs?
0:59 We have indexes, indexes, indexes, indexes, never forget indexes. They're easy to add, although they are a limited resource for you per collection.
1:12 But they're really easy to add and they are like magic database fairy dust.
1:16 You sprinkle a little bit of index on and wham, your database is so, so much faster. That thing you just saw where it went 706 times faster.
1:28 because we added an index. Incredible. Another one we've touched on this, but not so much
1:35 from a perspective of performance is document design or just how we model our data. We did
1:41 talk about to embed or not to embed and all that. But reiterating that is a huge knob
1:45 that you can control on how you design your documents. Query style. Previously, when we
1:51 built that API a couple of chapters ago, what we did is we said, first, let's check and And make sure the package is there.
1:59 And then we're going to do a request to get the package back, make some changes in memory and push all those things back into the database.
2:07 That's one query style and extremely ODM object oriented style programming. And I do like that style and it has its place.
2:16 But not always, especially not when you're worried about performance.
2:20 So as we saw, there's atomic in place updates and those types of things we can do.
2:25 So we can change our query style for pushing more of the work straight into MongoDB instead of inside our Python app.
2:34 We can also do what are called projections. You may have heard that select stars a bad idea because it takes all the data and you
2:41 should select only what you need. Well when we just pull back documents by themselves, we are effectively saying give us all of the document.
2:50 But not just that also the embedded documents and possibly their embedded documents as well. So that can be a ton of data.
2:56 If you don't need it, don't ask for it. And two larger deployment topology, distributed database types of things you can do with MongoDB
3:05 that we are not going to cover in this course, 'cause it's not really about Python and async, you can look into this if you want,
3:11 is we can create what's called a replica set. Oftentimes this is done for uptime and durability. So there's sometimes three MongoDB servers
3:22 working in a cluster and you connect to all of them, if one goes down, then another one picks up and they're replicating within themselves,
3:29 in that scenario, you can set it up so that you can read from the other replicas. So for example, let's say you have five MongoDB servers
3:39 in a replica set, you could sort of 5X the performance or the capacity of your database by saying, I'm willing to read from any of the replicas.
3:49 You start to get into consistency And it's it's can be tricky, but under extreme needs for performance, it's an option.
3:56 Another one is sharding, which is to say, I'm going to take part of the data and put
4:00 it in one MongoDB server, have another one kind of like replica, but instead of making
4:04 a copy, we're going to put a slice of the data for each one, like, maybe if we're tracking
4:10 people by where they lived in US states, each state could have its own server assigned to it so that we spread out the work across many other servers.
4:21 That'll make reading and writing faster. Cool, but again, not something we're covering.
4:27 You can look into those two things over at, it's a pure MongoDB server side type of thing that you want to control there.
4:34 So interesting, knobs you can turn, not knobs that we will be turning in this course.


Talk Python's Mastodon Michael Kennedy's Mastodon