我们如何在MongoDB上创建索引? [英] How we can create an Index on MongoDB?

查看:614
本文介绍了我们如何在MongoDB上创建索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Mongo数据库上创建索引以提高性能,所以请您帮我该怎么做?

I want to create Index on Mongo database for performance prespective, so could you please help me how I can do it?

您的帮助将在这里得到感谢.

Your help will be appreciated here.

推荐答案

在mongodb集合中应用索引之前,您需要了解索引的以下方面:

Before applying indexing in mongodb collections you need to understand the following aspects of indexing:

索引策略:

  • 检查您的应用程序是否将其发送到mongodb的查询类型.
  • 列出所有可能的查询.
  • 根据操作数,操作类型定义索引类型
  • 根据应用程序的需求选择正确的索引类型.类型可以是单索引,复合索引,部分索引,TTL索引等
  • 您的查询是否涉及排序操作?遵循以下指南为具有排序的操作编制索引.
  • 关于索引策略的更详细指南,请此处.
  • Check your application for what type of queries does it send to mongodb.
  • List down all such possible queries.
  • Based on the number of operations, type of operations define index type
  • Choose the correct type of indexes for application needs. Type can be single index, compound index, partial index, TTL index and so on
  • Do your queries involve the sort operations? Follow this guide on indexing for operations with sort.
  • The more detailed guide on indexing strategy here.

测试索引:

  • 一旦有了要应用的索引列表,就可以使用解释.
  • 在您的数据库上生成示例应用程序调用,并启用事件探查器(在开发人员或stag中)以检查索引的性能.
  • Once you have the list of indexes to be applied, test your indexes performance using explain.
  • Generate a sample application calls on your database and enable profiler (in dev or stag) to check how your indexes are performing.

如何编制索引:

  • 创建索引在的背景.它将确保创建索引操作不会阻止其他操作.
  • 根据您的数据大小,如果要在大型集合上创建索引,请考虑在低流量时间内进行.或在计划的维护窗口中
  • 您可能需要考虑在某些用例中建立滚动索引,以最大程度地减少索引编制的影响.
  • Create indexes in the background. It will make sure that the create index operation does not block the other operations.
  • Depending on your data size, if the indexes to be created on large collections, consider doing it in low traffic hours. Or in a scheduled maintenance window
  • You may need to consider building rolling index in certain use cases to minimize the impact of indexing.

跟踪创建的索引:

  • 记录您的索引.这可能包括您创建这些索引的时间,原因等等.

衡量生产中的索引使用统计信息:

  • 一旦在生产中应用了这些索引,就需要一到两周的使用时间稳定索引以检查它们是否真正被使用
  • 如果根本不使用索引,请考虑删除它们.
  • Once you have applied these indexes in production, in a week or two check usage stas of your indexes to check whether they're really being used
  • Consider dropping the indexes if they're not used at all.

警告:

  • 索引会为写操作添加性能损失.设计和应用对您的应用程序必不可少的索引.
  • Indexes add performance penalty for write operations. Design and apply indexes which are must for your application.

这篇关于我们如何在MongoDB上创建索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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