DEV Community

Shrihari Mohan
Shrihari Mohan

Posted on • Updated on

Don't Forget to do this if you're using MongoDB

πŸ—‚ 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 MongoDB Indexes


πŸ‘πŸ½ 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 q*uerying 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.

No Index

  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

Creating Index

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 1 Lak documents on a collection. To query a un indexed document, mongoDB has to go through 1 lak 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 1,00,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 follow on devto if you like this blog.

If you are interested in web design check out Top CSS sites
Are you working as web developer ? Check out PWA sites I use on my laptop
You planning to create a new portfolio site ? Shrihari Mohan template at the bottom of website.

Follow me on Twitter

Thanks & Cheers🍻 !


Try Our new product for free!

DocsAI - Create AI support agents with your documents in the most affordable price, starts at 0$. Don't need a bot , but need ai help on your docs just upload and start chating !

Using for a company ? Check out our pricing Just contact me for personalized pricing !

docsAi


Top comments (2)

Collapse
 
andrewbaisden profile image
Andrew Baisden

Great tips!

Collapse
 
shrihari profile image
Shrihari Mohan

Thanks man πŸ˜„ !