MongoDB索引定义策略 [英] MongoDB Index definition strategy

查看:61
本文介绍了MongoDB索引定义策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于MongoDB的数据库,内部包含大约10万到50万个文本文档,并且馆藏还在不断增长.系统应支持通过文档的不同字段进行查询,例如标题,类别,重要性等.

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.

该系统是近乎实时的系统,每5-10分钟就会收到一次新文档.

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

我的问题:
为了提高查询的性能,是否为文档的每个经常查询字段(字段类型:小文本,数字,日期)定义一个单独的索引是一个好主意吗?还是在MongoDB中还有其他提高查询性能的最佳实践?

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.

但是请记住,索引占用了您的RAM.您创建索引越多,它将使用您的RAM.在进行更好的搜索时,还应考虑索引的顺序.

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. 创建索引以支持您的查询 当索引包含该查询扫描的所有字段时,该索引就支持该查询.创建支持查询的索引可以大大提高查询性能.

  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.

使用索引对查询结果进行排序 为了支持有效的查询,在指定索引字段的顺序和排序顺序时,请使用此处的策略.

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.

确保索引适合RAM 当索引适合RAM时,系统可以避免从磁盘读取索引,从而获得最快的处理速度.

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.

创建可确保选择性的查询 选择性是查询使用索引缩小结果范围的能力.选择性使MongoDB可以将索引用于与完成查询相关的大部分工作.

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.

这篇关于MongoDB索引定义策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆