Understanding MongoDB Index

Getting a performance boost with the best usage of indexes, by understanding what’s the data structure, how it work’s/stored, how is it loaded into memory. How Query optimization make’s decision to select indexes.

Basic understanding of indexes is required i.e what are indexes, index types, creating them. https://docs.mongodb.com/manual/indexes/

  • Data Structure
  • Storage on disk
  • Memory Allocation.

Data Structure

Index on a filed/fields is stored in order that we specify using B-Tree data structure. Stored in ordered Let see what does it mean’s and how it help’s.

  • Index is created on the value of the filed referencing to the actual document stored.
snipped from: MognoDB university
  • Using B-Tree indexes significantly reduces the number of comparison to find the document.


Photo by Braden Collum on Unsplash

Keep it simple

Why wait, fail it’s the simplest way to prepare for success.

It’s better to fail again and again, to wait to be ready.

It’s better to run, than not to run because of the fear that I am not prepared and might fail. It’s the best, fastest and easiest way possible to get prepared. It’s an open opportunity to learn from the winner who is running with you and all the people aspiring to be winner.

Ever tried. Ever failed. No matter. Try again. Fail again. Fail better — Samuel Beckett

If I want something I will jump on it with whatever I have that’s SIMPLE, The thing’s that are missing…


MongoDB Storage Optimization

We can save on disk/memory depends on the document size number of fields their length and the size of the value assigned.

This is an easy small straightforward change not used much, We might not want to shorten the field-name/enum values present for the existing collection, but keeping this in mind when creating a new collection will help, will see how.

Why?

  • Filed name size/length contributes to the document size as MongoDB stores all field names in every document. The contribution ratio depends on the number of fields their length and the size of the value and document size.

MongoDB documentation for the same: https://docs.mongodb.com/manual/core/data-model-operations/

Pros:

  • Storage optimization [ 15–30 percent depending on the document/field/value size]
  • Reduced Cache Pressure [smaller document loaded into memory/cache]


MongoDB multitenancy.

MongoDB with Spring Boot, Spring MVC multi-tenant DB with implementation pushed in www.github.com.

https://github.com/arun2pratap/multitenant-spring-mongodb

Why multi-tenant database ?

Few of the reasoning we had gone with multi-tenant DB.

  • Noisy-neighbor — If the nature of the data where there is clear separation(where you won’t aggregate/operate the two data together) ex- mars(citizens), earth(citizens) so even for citizens collection will keep that in there own database.

Yes, Horizontal Scaling using sharding is an option but that comes with it’s own complexity/querying/maintenance/migration/backup/transaction… overhead. Avoid using if possible, though you might have to Shard at some point but the scope would be cut down.

  • Maintenance — Backup/Restore/Querying/Debugging/Analyzing will have smaller dataset to work on.
  • Migration — We can easily separate out a…


MongoDB Data Modeling

Model Tree Structures with Materialized Paths and how to use.

There different way’s to model tree structure. Will be looking materialized paths.

Don’t ever store the embedded tree as it is, if you wish to operate on nodes. This will add lot’s of complexity operating on nodes in the tree and you will be forced to handle on service side.

The rules hold’s the same when modelling we should know(not going in to much detail’s high level):

  • Usage…


MongoDB Cluster

MongoDB setting up Sharded cluster for windows with a double click.

Please find Github repository to configuration file and script to set-up the environment at the end.

mongodb version 3.6+ , link to mongodb documentation wherever necessary for further reading.

Introduction to Sharded cluster.

Sharding is a method for distributing data across multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations.

Database systems with large data sets or high throughput applications can challenge the capacity of a single server. For example, high query rates can exhaust the CPU capacity of the server. Working set sizes larger than the system’s RAM stress the I/O capacity of disk drives.


MongoDB Cluster

MongoDB Read from secondaries, Use cases, reading stale-data/up-to-date-data from secondaries pro’s and con’s with example.

mongodb version 3.6+ , link to mongodb documentation wherever necessary for further reading.

Why do we want to read from Secondary node’s ?

By default mongodb read/write on the primary for consistency(considering all preferences and configuration).

Use Cases to read from secondaries:

  • Reporting/analytics where in stale data is acceptable.
  • Write is significantly low compared to Reads(still have to take care of stale data).
  • Providing local reads for geographically distributed applications

Read from secondary (stale-DATA):

All read preference modes except primary may return stale data because secondaries replicate operations from the primary in an asynchronous process. [1] Ensure that your application can tolerate stale data if you choose to use a non-primary mode, but there…


Every thing start/stay’s simple until we make it complex/difficult.

I always did thing when it get’s most difficult to do, even if I was told to do simple thing’s daily… I knew that but for some reason I don’t like simple things……….. Still struggling to get the simple things done before it piles up to become difficult/complicated/undo-able.

For me I am still trying to keep thing’s simple :) Yes simple super simple. Just start cleaning your room from the place you see the closest slow and steady, don’t waste your time thinking analyzing from where to start to be most efficient.

Arun Pratap

Programmer, Database enthusiast currently exploring/learning MongoDB.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store