<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Shreetesh M</title>
    <description>The latest articles on Forem by Shreetesh M (@shree675).</description>
    <link>https://forem.com/shree675</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F942895%2Fb7df6b5a-917f-477c-bc0a-b30aac68f6a8.jpeg</url>
      <title>Forem: Shreetesh M</title>
      <link>https://forem.com/shree675</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shree675"/>
    <language>en</language>
    <item>
      <title>10 Best Practices While Using MongoDB Indexes</title>
      <dc:creator>Shreetesh M</dc:creator>
      <pubDate>Mon, 14 Aug 2023 15:32:31 +0000</pubDate>
      <link>https://forem.com/shree675/10-best-practices-while-using-mongodb-indexes-48d3</link>
      <guid>https://forem.com/shree675/10-best-practices-while-using-mongodb-indexes-48d3</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Here are some points on optimization and best practices while using MongoDB indexes to ensure good performance of queries in deployment.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ⓘ &lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
What are indexes? Check out &lt;a href="https://www.mongodb.com/docs/manual/indexes/"&gt;this page&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The below points will generally work for most applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;p&gt;Always index each of your collections, it makes a huge difference in query performance.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Use the &lt;strong&gt;&lt;a href="https://www.mongodb.com/docs/manual/tutorial/equality-sort-range-rule/"&gt;ESR (Equality, Sort, Range)&lt;/a&gt;&lt;/strong&gt; rule while building compound indexes. These almost always support index scans for both query predicate and sorting.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;&lt;a href="https://dev.to/shree675/partial-indexes-in-mongodb-a-brief-overview-261h"&gt;partial indexes&lt;/a&gt;&lt;/strong&gt; at the first opportunity available in your application. They can reduce the size of your index.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Remove indexes that are already &lt;strong&gt;prefixes of a compound index&lt;/strong&gt;. For example, if a compound index such as:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;stock:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;date:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;-1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;price:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;is available, then its valid prefixes:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;stock:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;date:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;-1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;stock:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;-1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;date:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;stock:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;stock:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;-1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;need not be separately created as indexes. The first compound index can serve the same functionality as these indexes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Build compound indexes such that most of your queries are &lt;strong&gt;&lt;a href="https://www.mongodb.com/docs/v5.0/core/query-optimization/#:~:text=A%20covered%20query%20is%20a,are%20in%20the%20same%20index."&gt;covered using a projection&lt;/a&gt;&lt;/strong&gt;. This will prevent the need to fetch documents entirely and will reduce query time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Avoid using unrooted or right-anchored regex&lt;/strong&gt; in the query field. This kind of regex will increase query time due to the variability in the beginning of the value, even if an index is present for that field.&lt;br&gt;
Some examples of unrooted expressions:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;world&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Limit to &lt;strong&gt;50 indexes per collection&lt;/strong&gt; as a thumb rule. Remember that even indexes will reside in the memory, thus reducing the size of your working set. Having a lot of indexes will also degrade the database write performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Before deleting an index, &lt;strong&gt;hide&lt;/strong&gt; the index to check the performance impact of deleting the index. Hidden indexes will continue to be updated like normal indexes, but will not be used by MongoDB.&lt;br&gt;
To hide an index:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hideIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;my_index_1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;code&gt;$indexStats&lt;/code&gt; to check how well and how frequently an index is being used by MongoDB.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexStats&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;my_index_1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;&lt;a href="https://www.mongodb.com/docs/manual/core/index-wildcard/"&gt;wildcard indexes&lt;/a&gt;&lt;/strong&gt; when you cannot know the schema in advance. Do not ignore indexing in such cases. In other words, use them when fields in a document can have arbitrary names that are decided at runtime.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Prefer using &lt;strong&gt;compound indexes over relying on &lt;a href="https://www.mongodb.com/docs/manual/core/index-intersection/"&gt;index intersection&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Note
&lt;/h2&gt;

&lt;p&gt;All the above points are compiled from the documentation and official MongoDB sources.&lt;/p&gt;

&lt;p&gt;Cover image credits: &lt;a href="https://www.google.com/url?sa=i&amp;amp;url=https%3A%2F%2Fwww.educba.com%2Findexes-in-mongodb%2F&amp;amp;psig=AOvVaw3QEo_8BvffjP9IJqDGSwp8&amp;amp;ust=1692113082418000&amp;amp;source=images&amp;amp;cd=vfe&amp;amp;opi=89978449&amp;amp;ved=0CBEQjRxqFwoTCLDPssu63IADFQAAAAAdAAAAABAE"&gt;EDUCBA&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>database</category>
      <category>cloud</category>
      <category>index</category>
    </item>
    <item>
      <title>Partial Indexes in MongoDB: A Brief Overview</title>
      <dc:creator>Shreetesh M</dc:creator>
      <pubDate>Wed, 21 Jun 2023 12:16:30 +0000</pubDate>
      <link>https://forem.com/shree675/partial-indexes-in-mongodb-a-brief-overview-261h</link>
      <guid>https://forem.com/shree675/partial-indexes-in-mongodb-a-brief-overview-261h</guid>
      <description>&lt;p&gt;Cover image credits: &lt;a href="https://www.google.com/url?sa=i&amp;amp;url=https%3A%2F%2Fwww.digitalocean.com%2Fcommunity%2Fconceptual-articles%2Funderstanding-mongodb-advantages-of-a-document-oriented-nosql-database&amp;amp;psig=AOvVaw0NaXABp3gV2lkl0lfFSU7n&amp;amp;ust=1687435492262000&amp;amp;source=images&amp;amp;cd=vfe&amp;amp;ved=0CBEQjRxqFwoTCIC1oZup1P8CFQAAAAAdAAAAABAE"&gt;Digital Ocean&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you have used &lt;a href="https://www.mongodb.com/docs/manual/indexes/"&gt;indexes&lt;/a&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Solution
&lt;/h2&gt;

&lt;p&gt;Partial indexes can be used to mitigate this problem to some extent.&lt;/p&gt;

&lt;p&gt;A partial index is a space-efficient technique to &lt;strong&gt;index only those documents that are most frequently queried&lt;/strong&gt;. 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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;Here's how to create one, assuming that the collection name is &lt;em&gt;book&lt;/em&gt; and &lt;em&gt;genre&lt;/em&gt; is a field in the document's schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;genre&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;partialFilterExpression&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$gte&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;partialFilterExpression&lt;/code&gt; option can be used in any kind of index, like a single-key index, a multi-key index or even a compound index.&lt;/p&gt;

&lt;p&gt;And here's how to make use of the index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;genre&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Astronomy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$gt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;3.5&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Caveats
&lt;/h3&gt;

&lt;p&gt;There &lt;strong&gt;must&lt;/strong&gt; 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 &lt;em&gt;rating&lt;/em&gt; will definitely result in documents that make up a proper subset of the documents that were indexed using the above &lt;code&gt;createIndex()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
The intention of this post is only to make developers aware of partial indexes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For more details, features and use cases of partial indexes, visit the &lt;a href="https://www.mongodb.com/docs/manual/core/index-partial/"&gt;official documentation&lt;/a&gt; for this.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>index</category>
      <category>database</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
