Time Series Indexes
Indexes on time series collections generally behave like indexes on regular collections, but with several additional considerations and limitations.
如果在时间序列集合上具有二级索引,并且您需要降级特征兼容性版本 (FCV),必须先删除与降级 FCV 不兼容的任何二级索引。有关详细信息,请参阅 setFeatureCompatibilityVersion
。
Starting in version 6.0, you can add a secondary index to any field in a time series collection. MongoDB indexes time series collections by buckets of documents as opposed to individual documents. 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. Indexing buckets instead of individual documents reduces index size and improves query efficiency.
提示
To improve query performance, you can manually add secondary indexes any field in your time series collection.
Clustered Collection
By default, MongoDB clusters time series collections based on bucket time.
复合索引
6.3 版本中的新功能。
Starting in MongoDB 6.3, MongoDB creates a default 复合索引 on both the metaField and timeField of a time series collection. MongoDB uses this index to improve query performance and speed.
You can add a 复合索引 on the
timeField
, metaField
, or measurement fields.
部分索引
6.0 版本中的新功能。
Starting in MongoDB 6.0, you can use the $or
, $in
, and
$geoWithin
operators with partial indexes on a time series collection.
You cannot create partial indexes on the metaField and timeField.
TTL Indexes
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.
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. If the collection uses
expireAfterSeconds
for all documents, you can use a partial TTL
index to expire matching documents sooner.
Prohibited Indexes
MongoDB does not allow the following index types on time series collections:
You cannot create sparse indexes on the metaField.
Indexing Best Practices
Use the metaField index for filtering and equality.
Use the timeField and other indexed fields for range queries.
General indexing strategies also apply to time series collections. For more information, see 索引策略.
For more information and examples, see 向时间序列集合添加从节点索引.