Indexing Strategies

The best indexes for your application must take a numberof factors into account, including the kinds of queries you expect,the ratio of reads to writes, and the amount of free memory on yoursystem.

When developing your indexing strategy you should have a deepunderstanding of your application’s queries. Before you build indexes,map out the types of queries you will run so that you can buildindexes that reference those fields. Indexes come with a performancecost, but are more than worth the cost for frequent queries on largedata sets. Consider the relative frequency of each query in theapplication and whether the query justifies an index.

The best overall strategy for designing indexes is to profile avariety of index configurations with data sets similar to the onesyou’ll be running in production to see which configurations performbest. Inspect the current indexes created for your collections toensure they are supporting your current and planned queries. If anindex is no longer used, drop the index.

Generally, MongoDB only uses one index to fulfill most queries.However, each clause of an $or query may use a differentindex, and in addition, MongoDB can use an intersection of multiple indexes.

The following documents introduce indexing strategies:

  • Create Indexes to Support Your Queries
  • An index supports a query when the index contains all the fieldsscanned by the query. Creating indexes that supports queriesresults in greatly increased query performance.
  • Use Indexes to Sort Query Results
  • To support efficient queries, use the strategies here when youspecify the sequential order and sort order of index fields.
  • Ensure Indexes Fit in RAM
  • When your index fits in RAM, the system can avoid reading theindex from disk and you get the fastest processing.
  • Create Queries that Ensure Selectivity
  • Selectivity is the ability of a query to narrow results using theindex. Selectivity allows MongoDB to use the index for a largerportion of the work associated with fulfilling the query.