MongoDB Indexing: Multiple single-field vs single compound?

℡╲_俬逩灬. 提交于 2021-01-27 13:23:09

问题


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:

  • Location: [geojson object]
  • Date: [Date object]
  • ZoomLevel: [int32]
  • EntryType: [ObjectID]

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.

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:

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.

...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.


回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/37354307/mongodb-indexing-multiple-single-field-vs-single-compound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!