banner



How To Store And Update Time Series Data In Mongodb

MongoDB 5.0 Time Series CollectionsIn a previous article, I tested a new feature of MongoDB 5.0: resharding. Today, I take a look at another new feature: the Time Series collections.

The Time Series collection is an astonishing new feature available in MongoDB 5.0. Based on the beginning tests I take done, the Time Series support provides comparable operation to the index usage on regular collections only saves a lot of disk and retentiveness space. Aggregation pipelines, which are common queries you can run on fourth dimension series information, tin can get even more do good.

Let'southward offset the tests.

What is a Fourth dimension Series Database?

More often than not speaking, a Time Series database is a specialized database designed for efficiently storing data generated from a continuous stream of values associated with a timestamp. The typical utilize case is when you need to store data coming from sensory equipment that transmits data points at fixed intervals, but now they are used in support of a much wider range of applications.

Typical apply cases are:

  • IoT data
  • Monitoring web services, applications, and infrastructure
  • Sales forecasting
  • Understanding financial trends
  • Processing self-driving auto information or other physical devices

A Fourth dimension Series specialized database utilizes compression algorithms to minimize the space requirement and also provides access paths to dig more efficiently into the data. This improves the performance of retrieving information based on time range filters and accumulation data. They are more efficient than using a common relational database.

Usually, the values of a Time Serial shouldn't change in one case recorded, they are divers every bit INSERT only, also known equally immutable information points. Once the information is stored the update operation is really uncommon.

Another characteristic of Time Series is that every detail should have a single value (a single temperature, a stock cost, and and then on).

Popular Fourth dimension Series databases are InfluxDB, Prometheus, Graphite. At that place are likewise many others. VictoriaMetrics in item is a popular fork of Prometheus and is used in our Percona Monitoring and Direction software.

The New Time Series Collections in MongoDB 5.0

MongoDB, too as relational databases, has been widely used for years for storing temperature information from sensors, stock prices, and any other kind of unchanging data over time. MongoDB version 5.0 promises that this can exist done more than efficiently, so let's take a look at how it works.

A Time Series collection appears as a regular collection and the operations you can practise are exactly the same: insert, update, find, delete, aggregate. The main deviation is backside the curtain. MongoDB stores data into an optimized storage format on insert. Compared to a normal drove, a Time Serial is smaller and provides more query efficiency.

MongoDB treats Fourth dimension Series collections every bit writable non-materialized views. The information is stored more efficiently, saving disk space, and an automatically created internal index orders the information by fourth dimension. By default, the data is compressed using the zstd algorithm instead of snappy. The new compression provides a higher ratio, less CPU requirements, and it is well suited for time series information where there are few variations from one document to the side by side 1. You can somewhen modify the pinch algorithm, but it is not really recommended.

A Time Series drove is non implicitly created when y'all insert a document, the aforementioned every bit regular collections. Y'all must create it explicitly.

Let's do some tests.

Create a Time Series Collection for Storing Stock Prices

We need to apply the createCollection() method, providing some parameters.

The name of the collection is stockPrice1week and the only required parameter is timeField. The other parameters are optional.

timeField: the proper noun of the field where the date is stored. This will exist automatically indexed and used for retrieving information.

metaField: the field containing the metadata. It can be a uncomplicated scalar value or a more than circuitous JSON object. It's optional. It cannot be the _id or the aforementioned as the timeField. For example, the metadata for a temperature sensor could be the code of the sensor, the type, the location, and then on.

granularity: possible values are seconds, minutes, and hours. If not set, it defaults to seconds. If you specify the closest match between two consecutive values this volition assist MongoDB to store data more than efficiently and improve the query performance.

expireAfterSeconds: you tin can automatically delete documents after the specified time, the same equally TTL index. If non specified the documents will non expire.

Allow's insert some random data for three stocks: Apple, Orange, and Assistant. Data is collected once per minute.

We tin can query to check the inserted documents:

Check the Collection Size

At present, let'southward create a regular collection having the aforementioned exact data.

Let's check the full size of the two collections.

As expected, the Time Serial collection is four times smaller than the regular one. Also, consider the regular collection doesn't accept any secondary alphabetize at the moment.

Query the Collections

Allow'due south run a simple query to find out the stock values for a specific timestamp. We exam the query on both collections.

We've got the aforementioned issue, but what is important here is looking at the explain() to run into the execution plan. Here is the explain() of the regular collection.

We didn't create whatever secondary index, so the winning program is a COLLSCAN, all documents must be examined. The query takes 200 milliseconds.

The following is the explain() of the Fourth dimension Series collection instead.

Surprisingly it is a COLLSCAN, but with different numbers. The number of documents examined is at present only eight and execution time is two milliseconds.

As already mentioned, the Fourth dimension Series is a non-materialized view. It works as an abstraction layer. The actual information is stored into another system collection (system.buckets.stockPrice1week) where documents are saved in a slightly unlike format. It'due south not the goal of this article to dig into the internals, just keep in heed the dissimilar storage format permits mongod to fetch only a few buckets of data instead of reading everything, even if it is flagged equally a COLLSCAN. That'due south astonishing.

What Happens if I Create an Index on the Regular Collection?

Let's effort.

Now the winning plan is an IXSCAN, the new index is used. Simply three keys examined, three docs examined, and 3 docs returned. The query takes two milliseconds.

So, information technology is every bit fast as the Time Series collection. There is not such a big difference; the order of magnitude is the aforementioned.

Likewise notice the same performance comes at the toll of having a larger collection at the finish because we have created a secondary index.

For getting a comparable execution time, now the regular collection is five times larger than the Time Series.

A Query with a Time Range Filter

Let'south examination a different query looking for a range of timestamps. The following are the explicate() outputs.

The same as before. The execution fourth dimension is basically the same for both queries. The main problem remains the size of the regular drove that is significantly larger.

Only six documents are plain returned by the Fourth dimension Series, but it's not. If you execute the query for real yous'll get 4320 documents. The six documents mentioned by explain() refer to the documents that must be returned by the real drove below the non-materialized view.

Assemblage Test

On our Time Serial data, we would like to do some aggregation. This is a typical task: summate averages over a menses, find min and max values, and other kinds of statistics.

Let's suppose nosotros demand to calculate the average stock toll on a daily basis. We can use the post-obit aggregation pipeline for example:

As usual, let's take a look at the explain() of the aggregate against the two collections, just focusing on execution fourth dimension and documents examined.

The aggregation pipeline runs 40 per centum faster with the Time Series collection. This should be more relevant the larger the collection is.

Conclusion

MongoDB five.0 is an interesting new version of the most pop document-based database, and new features similar Time Serial collections and resharding are amazing.

Anyhow, due to many core changes to WiredTiger and the core server introduced to facilitate new features, MongoDB 5.0.x is still unstable. Nosotros exercise not recommend using information technology for production environments.

Check the documentation of Percona Server for MongoDB v.0.iii-2 (Release Candidate).

Consummate the 2021 Percona Open Source Data Direction Software Survey

Take Your Say!

How To Store And Update Time Series Data In Mongodb,

Source: https://www.percona.com/blog/mongodb-5-0-time-series-collections/

Posted by: barnesoneven.blogspot.com

0 Response to "How To Store And Update Time Series Data In Mongodb"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel