So why would we use an index
and what is index to begin with? An index can speed up our find, update or delete queries,
So all the queries where we are looking for certain documents that should match some criteria. Consider
this find query, here I'm looking for all products where the seller is Max.
Now let's say this is my collection of documents, the products collection, now by default if I don't
have an index on seller set,
mongodb will go ahead and do a so-called collection scan,
now that simply means that mongodb to fulfill this query will go through the entire collection,
look at every single document and see if seller equals Max
and as you can imagine for very large collections with thousands or millions of documents, this can take
a while and we'll see this in practice with an example in a second.
So this is the default approach mongodb takes or the only approach it can take when you have no index
set up in order to retrieve maybe two documents out of your thousand documents.
Now you can create an index though, an index is not a replacement for a collection but an addition you
could say,
so you would create an index for the seller key of the products collection here and that index then
exists additionally to the collection and the index is essentially an ordered list of all the values
that are placed or stored in the seller key for all the documents.
So it's not an ordered list of the documents, just of the values for the field for which you created that
index
and it's not just an ordered list of the values, of course every value, every item in the index has a
pointer to the full document
it belongs to.
Now this allows mongodb to do a so-called index scan to fulfill this query, which means it sees that
for seller, such an index exists and it therefore simply goes to that seller index and can quickly jump
to the right values because there, unlike for the collection,
it knows that the values are sorted by that key,
so it doesn't have to look at the first three records if it's only looking for records starting with
M or to be precise, records equal to Max.
So it can very efficiently go through that index and then find the matching products because of that ordering
and because of that pointer, every element in this index has, so mongodb finds the value for this query
and then finds the related documents it can return this,
so it's this direct access that mongodb can use here and that speeds up your queries.
So this is the end how an index works and it also answers the question why you would use one because
creating such indexes can drastically speed up your queries.
However you also shouldn't overdo it with the indexes because you could of course think well ok I got
my product selection and every product has ID, name, age and hobby
and then I could simply create indexes for all fields and I will have the best performance ever, right?
Because no matter for what I look, I got an index for that.
Well this might speed up your find queries,
that is correct, indexes on all fields would speed up find queries because you can query for every field
efficiently but an index does not come for free,
you will pay some performance cost on inserts because that extra index that has to be maintained needs
to be updated with every insert,
makes sense right
because we have an ordered list of elements with pointers to the documents.
So if you add a new document, you also have to add a new element to the index and that might sound simple
and it won't take super long but if you've got 10 indexes for your documents in your collection
and you have to update all 10 indexes for every insert, then you might quickly run into issues because
you'll have to do a lot of work for all these fields, for every insert and for every update too. Therefore,
indexes don't come for free and you really have to find out which indexes makes sense and which indexes
don't and this is exactly what I will also do in this module with you.
I will walk you through all the different index types that exist in mongodb and I will also show
you how you can measure whether an index makes sense or does not make sense.