1 00:00:02,230 --> 00:00:09,160 That's it for this module, we had a very detailed look at indexes. Indexes allow us to retrieve our data 2 00:00:09,160 --> 00:00:10,420 more efficiently, 3 00:00:10,420 --> 00:00:16,800 mostly we saw some exceptions to this module but generally, they speed up our queries and also the sorting. 4 00:00:17,110 --> 00:00:20,710 We can use single field compound multikey and text indexes, 5 00:00:20,710 --> 00:00:26,500 now there are a couple of other indexes and for example geospatial indexes are something we'll see in 6 00:00:26,500 --> 00:00:28,000 the geospatial module 7 00:00:28,030 --> 00:00:33,880 but these are the most important indexes for now and it's also important to understand that indexes don't 8 00:00:33,880 --> 00:00:34,990 come for free, 9 00:00:34,990 --> 00:00:36,940 they will slow down your writes, 10 00:00:36,940 --> 00:00:38,590 that is something you always have 11 00:00:38,620 --> 00:00:40,610 but this still worth the effort 12 00:00:40,690 --> 00:00:45,670 but this is something you should keep in mind because it means that you should not create the index 13 00:00:45,730 --> 00:00:47,380 for every single field, 14 00:00:47,380 --> 00:00:54,790 this will be inefficient because all these indexes need to be updated with every insert, update or delete. 15 00:00:54,820 --> 00:01:02,110 We can use indexes for querying and sorting and compound indexes have this special behavior where you 16 00:01:02,110 --> 00:01:03,710 can use them left to right, 17 00:01:03,760 --> 00:01:09,460 if you have a compound index made up of name and age, you can either use that index to optimize your queries 18 00:01:09,460 --> 00:01:16,150 where you look for name and age with some criteria or also for queries that just use the name, just the 19 00:01:16,150 --> 00:01:20,380 age is not possible due to the way this is stored. 20 00:01:20,380 --> 00:01:24,820 To find out whether your queries and indexes do their job in a good way, 21 00:01:24,850 --> 00:01:29,110 you can diagnose your queries with the explain method, 22 00:01:29,200 --> 00:01:36,090 also remember the options you could pass to explain, query planner, execution stats and all plans execution. 23 00:01:36,250 --> 00:01:42,860 The output there gives you a detailed look into what mongodb did and how it derived 24 00:01:42,910 --> 00:01:48,790 this plan it used and it also gives you an idea of how you might optimize your indexes, 25 00:01:48,790 --> 00:01:54,070 for example if you have a large gap between the keys it had to look at and the documents it actually 26 00:01:54,070 --> 00:01:56,850 retrieved. When creating your indexes, 27 00:01:56,860 --> 00:01:58,640 you can also configure them, 28 00:01:58,660 --> 00:02:03,290 for example you can add a time to live if the index is placed on a date time 29 00:02:03,370 --> 00:02:07,570 or you can turn them into unique or partial indexes. For text indexes, 30 00:02:07,570 --> 00:02:11,210 you can also assign weights and a default language. 31 00:02:11,210 --> 00:02:17,770 Now this is what we covered in this module and this hopefully helps you create efficient databases with 32 00:02:17,770 --> 00:02:22,060 efficient indexes and then writing efficient queries for that.