Database Manual / Time Series

Time Series Indexes时间序列索引

Indexes on time series collections generally behave like indexes on regular collections, but with several additional considerations and limitations.时间序列集合上的索引通常表现得像常规集合上的索引,但还有一些额外的考虑和限制。

If there are secondary indexes on time series collections and you need to downgrade the feature compatibility version (FCV), you must first drop any secondary indexes that are incompatible with the downgraded FCV. 如果时间序列集合上有辅助索引,并且需要降级功能兼容性版本(FCV),则必须首先删除与降级的FCV不兼容的任何辅助索引。For more information, see setFeatureCompatibilityVersion.有关更多信息,请参阅setFeatureCompatibilityVersion

Starting in version 6.0, you can add a secondary index to any field in a time series collection. 从6.0版本开始,您可以向时间序列集合中的任何字段添加辅助索引。MongoDB indexes time series collections by buckets of documents as opposed to individual documents. MongoDB按文档而不是单个文档对时间序列集合进行索引。Time series buckets contain documents with shared metaField values, ordered by timeField values that are close together. MongoDB indexes the minimum and maximum values of all fields, except the metaField. 时间序列桶包含具有共享metaField值的文档,按接近的timeField值排序。MongoDB对除metaField之外的所有字段的最小值和最大值进行索引。Indexing buckets instead of individual documents reduces index size and improves query efficiency.索引桶而不是单个文档可以减小索引大小并提高查询效率。

Tip

To improve query performance, you can manually add secondary indexes to any field in your time series collection.为了提高查询性能,您可以手动向时间序列集合中的任何字段添加辅助索引

Clustered Collection群集集合

By default, MongoDB clusters time series collections based on bucket time.默认情况下,MongoDB基于桶时间对时间序列集合进行聚类。

Compound Indexes复合索引

New in version 6.3.在版本6.3中新增。

Starting in MongoDB 6.3, MongoDB creates a default compound index on both the metaField and timeField of a time series collection. MongoDB uses this index to improve query performance and speed.从MongoDB 6.3开始,MongoDB在时间序列集合的metaFieldtimeField上创建默认的复合索引。MongoDB使用此索引来提高查询性能和速度。

You can add a compound index on the timeField, metaField, or measurement fields.您可以在timeFieldmetaField或度量字段上添加复合索引

Partial Indexes部分索引

New in version 6.0.在版本6.0中新增。

Starting in MongoDB 6.0, you can use the $or, $in, and $geoWithin operators with partial indexes on a time series collection.从MongoDB 6.0开始,您可以在时间序列集合上使用带有部分索引$or$in$geoWithin运算符。

You cannot create partial indexes on the metaField and timeField.您无法在metaFieldtimeField上创建部分索引

TTL IndexesTTL索引

New in version 7.0.在版本7.0中新增。

Starting in MongoDB 7.0, you can create a TTL index with a partialFilterExpression that relies only on the metaField. In versions prior to 6.3, you can only create TTL indexes based on the expireAfterSeconds parameter.从MongoDB 7.0开始,您可以使用仅依赖于metaFieldpartialFilterExpression创建TTL索引。在6.3之前的版本中,只能基于expireAfterSeconds参数创建TTL索引。

If your time series collection doesn't use the expireAfterSeconds option to expire documents, creating a partial TTL index sets an expiration time for matching documents only. 如果时间序列集合不使用expireAfterSeconds选项使文档过期,则创建部分TTL索引仅为匹配的文档设置过期时间。If the collection uses expireAfterSeconds for all documents, you can use a partial TTL index to expire matching documents sooner.如果集合对所有文档都使用expireAfterSeconds,则可以使用部分TTL索引使匹配的文档更快过期。

Prohibited Indexes禁止索引

MongoDB does not allow the following index types on time series collections:MongoDB不允许在时间序列集合上使用以下索引类型:

You cannot create sparse indexes on the metaField.您无法在metaField上创建稀疏索引。

Indexing Best Practices索引最佳实践

  • Use the metaField index for filtering and equality.使用metaField索引进行筛选和相等。
  • Use the timeField and other indexed fields for range queries.使用timeField和其他索引字段进行范围查询。
  • General indexing strategies also apply to time series collections. For more information, see Indexing Strategies.一般索引策略也适用于时间序列集合。有关更多信息,请参阅索引策略

For more information and examples, see Add Secondary Indexes to Time Series Collections.有关更多信息和示例,请参阅向时间序列集合添加辅助索引