Logo

dev-resources.site

for different kinds of informations.

Partial Indexes in MongoDB: A Brief Overview

Published at
6/21/2023
Categories
mongodb
index
database
backend
Author
shree675
Categories
4 categories in total
mongodb
open
index
open
database
open
backend
open
Author
8 person written this
shree675
open
Partial Indexes in MongoDB: A Brief Overview

Cover image credits: Digital Ocean

Introduction

If you have used indexes in MongoDB, then you must be knowing their importance and the huge performance gain they provide. It is part and parcel of almost every project that uses MongoDB and indexes play a vital role in improving the query execution performance.

The Problem

However, indexes don't come without cost. Indexes are generally stored in the memory, but this may not be true all the time. It may happen that your project contains many indexes or a few large indexes, and your computer's available memory may not be sufficient to accommodate all of them.

In such cases, some amount of an index is stored in the memory while the remaining part of it is stored in the storage. Storage accesses can be extremely slow compared to memory accesses. So, this can slow down your query execution.

A Solution

Partial indexes can be used to mitigate this problem to some extent.

A partial index is a space-efficient technique to index only those documents that are most frequently queried. For example, if an online book store has their customers searching for books with a rating of 3⭐ or above most of the times, then indexing only those documents with a rating of 3⭐ or above is sufficient to satisfy low latency queries for most searches. This reduces the size of the index required.

Usage

Here's how to create one, assuming that the collection name is book and genre is a field in the document's schema:

db.books.createIndex(
  { genre: 1 },
  { partialFilterExpression: { rating: { $gte: 3 } } }
)
Enter fullscreen mode Exit fullscreen mode

The partialFilterExpression option can be used in any kind of index, like a single-key index, a multi-key index or even a compound index.

And here's how to make use of the index:

db.books.find({ genre: "Astronomy", rating: { $gt: 3.5 } })
Enter fullscreen mode Exit fullscreen mode

Caveats

There must be a matching filter expression in the query to trigger the usage of any partial index. In the above example, the partial index will be used because the query's filter on rating will definitely result in documents that make up a proper subset of the documents that were indexed using the above createIndex() function.

If the query can result in a few documents outside the indexed ones, then the partial index will not be used, not even for the ones that can be fetched using the partial index. So, your query needs to ensure that no document outside the partial index has the slightest possibility of being scanned.

Conclusion

In essence, partial indexes can be used on documents that are queried frequently and you are not very concerned about the P90 or P99 latencies for those queries. This will help reduce the memory requirements to store the index.

Note
The intention of this post is only to make developers aware of partial indexes.

For more details, features and use cases of partial indexes, visit the official documentation for this.

index Article's
30 articles in total
Favicon
Avoiding the Pitfalls of Duplicate Indexes in MySQL
Favicon
SQL Performance: A Deep Dive into Indexing with examples
Favicon
Tried-and-True Methods to Immediately Get Google to Index Your Website
Favicon
Mysql Database Index Explained for Beginners
Favicon
Database: Indexing issues with random UUIDs
Favicon
Nol indekslangan to'plamlar
Favicon
Quantitative trading strategy using trading volume weighted index
Favicon
Apache SOLR
Favicon
How to Replace Character at Nth Index in Python String
Favicon
Postgres partitioning performance: Hash vs. List
Favicon
A Novice Guide to Azure AI Search
Favicon
PostgreSQL index Correlation with UPDATE
Favicon
fetch vs index methods when working with arrays in Ruby
Favicon
How To Quickly Define an Efficient SQL Index for GROUP BY Queries
Favicon
Optimize Mongo DB Performance By Adding Indexes to Your Collection
Favicon
Optimizing Database Performance with Concatenated Indexes
Favicon
A Guide to Sargable Queries
Favicon
The Most Annoying Optimizer Fail in Postgres βœ… Best index solved it.
Favicon
To speed up the search process, let’s set an index for array data within a JSON-formatted column
Favicon
How To Get Google To Index Your Site Fast in 2024 | Index your website Readmorr.com
Favicon
Detectando Γ­ndices no MongoDB
Favicon
10 Best Practices While Using MongoDB Indexes
Favicon
Summarize Heap-Only Tuple and Index-Only Scans in PostgreSQL
Favicon
Uncovering the Significance of Indexes in Apache AGE
Favicon
Partial Indexes in MongoDB: A Brief Overview
Favicon
Making sure our database queries are fast
Favicon
Principles of Database Index Design
Favicon
The Simple SQL Question You May Be Asked on Interview and Fail
Favicon
Index usage monitoring in YugabyteDB & PostgreSQL
Favicon
Database Indexing with PostgreSQL

Featured ones: