MongoDB 索引:多个单字段与单个复合? [英] MongoDB Indexing: Multiple single-field vs single compound?

查看:57
本文介绍了MongoDB 索引:多个单字段与单个复合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组地理空间+时间数据和一些附加属性,我将在地图上显示这些数据.该集合目前有几百万个文档,并且会随着时间的推移而增长.

I have a collection of geospatial+temporal data with a few additional properties, which I'll be displaying on a map. The collection has a few million documents at this point, and will grow over time.

每个文档都有以下字段:

Each document has the following fields:

  • 位置:[geojson 对象]
  • 日期:[日期对象]
  • 缩放级别:[int32]
  • 条目类型:[ObjectID]

我需要能够通过位置(通常为 geowithin 查询)、日期(通常为 $gte/$lt)、ZoomLevel 和 EntryType 的任意组合快速查询此集合.

I need to be able to rapidly query this collection by any combination of location (generally a geowithin query), Date (generally $gte/$lt), ZoomLevel and EntryType.

我想知道的是:我应该创建一个包含所有四个字段的复合索引,还是每个字段的单个索引,或者它们的某种组合?我在 MongoDB 文档中阅读了以下内容:

What I'm wondering is: Should I make a compound index containing all four fields, or a single index for each field, or some combination thereof? I read in the MongoDB docs the following:

对于包含 2dsphere 索引键的复合索引以及其他类型的键,只有 2dsphere index 字段决定是否索引引用一个文档.

For a compound index that includes a 2dsphere index key along with keys of other types, only the 2dsphere index field determines whether the index references a document.

...这听起来意味着将 Location 的 2dsphere 索引作为复合索引的一部分可能毫无意义?

...Which sounds like it means having the 2dsphere index for Location be part of a compound index might be pointless?

对此的任何澄清将不胜感激.

Any clarity on this would be much appreciated.

推荐答案

对于您的用例,您将需要使用多个索引.

For your use case you will need to use multiple indexes.

如果您创建了一个覆盖文档所有字段的索引,则您的查询只有在包含索引中的第一个字段时才能使用它.

If you create one index covering all fields of your documents your queries will only be able to use it when they include the first field in the index.

由于您需要通过这四个字段的任意组合进行查询,我建议您分析您的数据访问模式,并准确查看您实际使用的过滤器,并为每个或其中的一组创建特定索引.

Since you need to query by any combination of these four fields I suggest you to analyze your data access patterns and see exactly what filters are you actually using and create specific index for each one or group of them.

对于您关于 2dsphere 的问题,让它们复合是有意义的.

For your question about 2dsphere, it does make sense to make them compound.

此注释指的是稀疏"选项.稀疏索引仅引用包含索引字段的文档,对于 2dspheres,唯一会被排除的文档是不包含 geojson/point 数组的文档.

This note refers to the 'sparse' option. Sparse index references only documents that contains the index fields, for 2dspheres the only documents that will be left out is the ones that do not contain the geojson/point array.

这篇关于MongoDB 索引:多个单字段与单个复合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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