MongoDB Index definition strategy
Asked Answered
P

1

5

I have a MongoDB-based database with something about 100K to 500K text documents inside and the collection keeps growing. The system should support the queries by different fields of the documents, e.g. title, category, importance etc.

The system is a near real-time system, which got new documents every 5-10 minutes.

Is it a good idea, in order to boost the queries' performance, to define a separate index for each frequently queried field (field types: small text, numeric, date) of the document? Or there are another best practices for queries' performance boosting in MongoDB?

Passed answered 27/1, 2016 at 11:48 Comment(1)
docs.mongodb.com/manual/applications/indexesHasa
R
7

You should use/make indexes depending on the result you are trying to find. It's very good idea to have different indexes for different field you are trying to find at different times.

But keep in mind that indexes occupies your RAM. More you make indexes more it will use your RAM. Also consider ordering of index while making for better Search.

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

The best overall strategy for designing indexes is to profile a variety of index configurations with data sets similar to the ones you’ll be running in production to see which configurations perform best.Inspect the current indexes created for your collections to ensure they are supporting your current and planned queries. If an index is no longer used, drop the index.

Some of the Strategies to choose while creating:

  1. Create Indexes to Support Your Queries An index supports a query when the index contains all the fields scanned by the query. Creating indexes that supports queries results in greatly increased query performance.

  2. Use Indexes to Sort Query Results To support efficient queries, use the strategies here when you specify the sequential order and sort order of index fields.

  3. Ensure Indexes Fit in RAM When your index fits in RAM, the system can avoid reading the index from disk and you get the fastest processing.

  4. Create Queries that Ensure Selectivity Selectivity is the ability of a query to narrow results using the index. Selectivity allows MongoDB to use the index for a larger portion of the work associated with fulfilling the query.

Refutation answered 27/1, 2016 at 12:3 Comment(1)
Thanks for the detailed explanation.Passed

© 2022 - 2024 — McMap. All rights reserved.