This article documents how to improve MongoDB query performance using index selectivity. At the end of this article, you will know the importance of index selectivity and how you can leverage it in improving query performance.


Problem Statement

Let’s explore the scenario we’re facing currently. We have a flight database with a booking collection. Refer to the screenshot below for the schema.

Image for post

Schema for booking

Given a flight database with a booking collection, the admin from the flight company would want us to look into the query performance. Here is what the admin would do:

  • The admin would like to know how queries for all the flight bookings to a specific destination where there is more than one stop performed. The information that the admin would like to know is their respective booking_noorigindestination, and the number of stop.

Next, I will analyze and list what I need in order to carry out the research.


Solution Analysis

I always start with finding out what I need in order to continue the research. These items are what I need:

  • A collection with more than 500K documents, in order to make the query time more significant
  • 25K bookings to destination “Gerlachmouth”
  • 12K bookings to destination “Gerlachmouth” which are more than one stop.

At the end of the article, we will know the importance of index selectivity in query performance.

Without further ado, let’s start the experiment to check performance.

#mongodb #software-engineering #devops #programming #database

Improve Your MongoDB Performance Using Index Selectivity
1.45 GEEK