MongoDB groups documents with matching metaFields to optimize the storage and query latency of time series data. MongoDB使用匹配的metaField对文档进行分组,以优化时间序列数据的存储和查询延迟。Your choice of 您对metaField has the biggest impact on optimizing queries in your application.metaField的选择对优化应用程序中的查询影响最大。
Querying the metaField查询元字段
You query a time series collection the same way you query a standard MongoDB collection. For an example query and example aggregation pipeline, see Query a Time Series Collection. 查询时间序列集合的方式与查询标准MongoDB集合的方式相同。有关示例查询和示例聚合管道,请参阅查询时间序列集合。For a list of querying best practices, see Query Best Practices.有关查询最佳实践的列表,请参阅查询最佳实践。
Queries against time series data typically focus on a single time series in the collection. For example, consider a time series collection that tracks stock data using the following schema:对时间序列数据的查询通常集中在集合中的单个时间序列上。例如,考虑一个使用以下模式跟踪股票数据的时间序列集合:
{
_id: 573a1397f29313caabce8347,
"ticker": "MDB",
"timestamp": ISODate("2024-07-24T13:45:00.000Z"),
"price": 248.21,
"volume": 6930
}
The collection has the following settings:该集合具有以下设置:
timeseries: {
timeField: "timestamp",
metaField: "ticker",
granularity: "seconds"
}
MongoDB groups documents with matching MongoDB使用匹配的ticker values. ticker(股票代码)值对文档进行分组。Instead of having to check for matches across all fields in all documents, the server only has to check against the 服务器不必检查所有文档中所有字段的匹配情况,只需检查metaField, in this case ticker, to narrow down the search range to a unique time series. metaField(在本例中为ticker),即可将搜索范围缩小到唯一的时间序列。This fits the expected use case, which is searching for activity on a single stock. A user searching for information on MongoDB stock (MDB) doesn't need to consider results for Amazon (AMZN).这符合预期的用例,即搜索单个股票的活动。搜索MongoDB股票(MDB)信息的用户不需要考虑亚马逊(AMZN)的结果。
Querying the timeField查询时间字段
The second major dimension for querying time series data is time. Since MongoDB groups documents that have both an identical 查询时间序列数据的第二个主要维度是时间。由于MongoDB将具有相同metaField value and close timeField values, this further narrows the scope of a query to a range of buckets. metaField值和关闭timeField值的文档分组,这进一步将查询的范围缩小到一系列桶。Recent transactions are kept in memory, so it's easy to stream data in real time.最近的事务保存在内存中,因此很容易实时传输数据。
Block Processing块处理
Starting in version 8.0, MongoDB may execute certain time series queries using block processing. This performance improvement processes queries in "blocks" of data, rather than individual values. Block processing improves query execution speed and throughput when working with time series collections.从8.0版本开始,MongoDB可以使用块处理执行某些时间序列查询。这种性能改进以数据的“块”而不是单个值来处理查询。在处理时间序列集合时,块处理可以提高查询执行速度和吞吐量。
Block processing significantly improves performance and reduces overhead for long running aggregation pipelines that begin with the following stages:块处理显著提高了性能,并减少了从以下阶段开始的长时间运行的聚合管道的开销:
Compared to time series queries run on MongoDB 7.0 or earlier, block processing for time series data in MongoDB 8.0 may handle higher volumes of data and improve throughput in some cases by more than 200% for 与在MongoDB 7.0或更早版本上运行的时间序列查询相比,MongoDB 8.0中时间序列数据的块处理可以处理更大的数据量,在某些情况下,对于$group operations and analytical queries. $group操作和分析查询,吞吐量可以提高200%以上。To learn more about performance improvements in MongoDB 8.0, see Performance Improvements.要了解有关MongoDB 8.0性能改进的更多信息,请参阅性能改进。
Note
The amount of performance improvement can vary depending on the configuration of your workloads and database instances.性能提升的幅度可能因工作负载和数据库实例的配置而异。
MongoDB automatically enables block processing for eligible time series queries. You cannot manually specify whether a query uses block processing. MongoDB自动为符合条件的时间序列查询启用块处理。您不能手动指定查询是否使用块处理。To see if your time series query uses block processing, see 要查看时间序列查询是否使用块处理,请参阅解释计划输出中的阶段。stages in the explain plan output.