MongoDB for Developers with Python Transcripts
Chapter: Course conclusion
Lecture: Lightning review: Performance tuning MongoDB

Login or purchase this course to watch this video and the rest of the course contents.
0:01 At this point, we pretty much had MongoDB doing everything we needed it to do, and we'd heard MongoDB was fast,
0:08 but it turned out it didn't really seem to be behaving as quickly as maybe we hoped, we put a ton of data from our dealership in there,
0:15 and we were getting query times of like one second, 700 milliseconds, stuff like that. It was okay, but really, we saw it can do much better.
0:21 What levers and knobs do we have to turn to make this faster? The most important one, even more important than in relational databases,
0:29 are the indexes, we'll see MongoEngine as well as PyMongo in the shell all have really good ways to deal with this.
0:36 Document design is really important, mostly around this embedding question but there are many ways to think about document design,
0:43 there's a lot of really non intuitive and powerful patterns, design patterns you can apply here.
0:49 What is your query style, maybe one query is better than another and using projections to only pull back a subset of responses,
0:57 suppose we have a car that has a ton of those service histories and we don't care about them for a particular query
1:03 we could suppress returning those from the database which saves us a lot of bandwidth on the network,
1:08 disks reads on the database server and deserialization processing on our side. We also saw there is some network apology things we can do,
1:16 replication and sharding, and those are both interesting and powerful
1:20 but not part of this course, so go check that out on your own if you're interested. For indexes, we took an example like our car
1:28 and we said let's suppose we have make here that we're interested in querying by a service history,
1:33 and if you look below how service history is defined as the service record objects and they have a description and a customer rating
1:40 and things like this, price for example, so our goal is to query these things, the make, the service history and stuff, quickly,
1:46 so we saw adding an index which really a powerful way to do that, so all we've got to do is go to our meta object, our meta element here
1:53 and say these are the index as an array now these indexes can simply be the name of the thing, like make that's super straightforward,
2:02 they could traverse the hierarchy using the Javascript style, using the dot, so we'll service_history.customer_rating
2:09 and that would go down and let us do queries deep into these cars and say let's find the ones that are either good or low customer ratings
2:18 and we can even do composite indexes, so here we're having a composite index on price and description,
2:23 within the service history, so we do that by having this fields dictionary thing and the fields are an array, so you can use the simple version
2:30 or if you need to, you can get a more complex definition of the index there.

Talk Python's Mastodon Michael Kennedy's Mastodon