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