Don’t Forget to do this if you’re using MongoDB | INDEXING

Shrihari Mohan
3 min readMay 14, 2022

--

🗂 Whats inside this blog ?

  1. What is Indexing
  2. When to do Indexing
  3. Seeing differences between indexed and unindexed queries
  4. COLLSCAN and IXSCAN
  5. How to do indexing

🙇🏽‍♂️ What is indexing ?

  • Indexing is a way to improve fetch speed by arranging the document in some way so that retrieval is going to be faster.
  • id of every document is unique and it is stored in a certain order ( ascending by default ) so any documents that is fetched using id will be faster when compared to other fields. This is because id is automatically index by mongodb
  • MongoDB indexing uses B-Tree , B+Tree to sort the indexes which is similar to Binary Search Tree. More info on the below link

👍🏽 When should we use indexes ?

  • Whenever the document insertion operations are less than fetch operatation, indexes are pretty good.
  • That is because, Mongodb internally organizes the indexed documents in certain order that follows B-Tree ( What is B-Tree , thats for another day). So for each insertion the structure of tree changes , MongoDB has to arrange the tree with respect to the inserted document and its really time consuming when the number of documents grow exponentially.
  • It is recommended to use it whenever you’re querying without ids.

🥳 Fun Fact -> Firestore won’t allow querying without indexing

🌞 🌚 Seeing Differences between unindexed and indexed query

I have a db called FORUM and collection called accountDetails that has 3 documents having userId as one of the fields.

Let me just fetch one of the documents.

Unindexed Query
  1. Blue — Querying using mongoDb compass for one document using userId
  2. Brown — mongoDb searches for 3 documents and finds a match then returns 1 matched document
  3. Red — Warning for no index
  4. Violet — COLLSCAN is called.

🪓 Now let us create indexes for userId

Create Indexes

You can create these from index tab either from mongoDB compass or from web version also. Both will have the same user interface.

Just select the fields that is to feteched and there are several layers to it such as compound indexing and other stuff. More Info on MongoDB Indexes

🤯 Query the same userId now

Indexed Query
  1. You can see now we’re using index to fetch.
  2. Only one document is fetched and that document is returned.
  3. Using IXSCAN instead of COLLSCAN

🤓 COLLSCAN and IXSCAN

  • COLLSCAN uses the entire collection to fetch a query.
  • An average business that has 100,000 documents on a collection. To query a un indexed document, mongoDB has to go through 100,000 docs to get one doc.
  • Which will be really be heavy on processing and will take several hundred milliseconds to execute a query.

On the other hand

  • IXSCAN uses the arranged b-tree structure and just returns the exact document needed. No need to go through the entire collection
  • Because thats why we use index , duh 🤷🏽! To know where stuff is.

There is a parameter in the above images Actual Query Execution time (ms) : 0. In both cases it is 0 but if the number of documents is higher ( let’s say 100,000 ) Then also IXSCAN is going to be near 0ms but not the COLLSCAN (several hundred milliseconds).

🔖 Don’t forget to bookmark this for future references.
Give me a follow on medium if you like this blog.

Thanks to Sebastian Grebe for making this blog more polished to all readers.

Cheers🍻 !

--

--

Shrihari Mohan

If you don’t have a subscription/used free articles , Visit https://dev.to/shrihari for the free blogs.