简体   繁体   中英

MongoDB Index definition strategy

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, eg title, category, importance etc.

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

My question:
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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM