In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to index the killer of out-of-date data by MONGODB TTL". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to index the killer of out-of-date data by MONGODB TTL.
The problem of cleaning up expired data has always been a problem in the database community, and there are many ways to deal with it, most of which are handled through stored procedures or using third-party tools on a regular basis. In the aspect of MONGODB dealing with out-of-date data, you can use a concept similar to REDIS expired key to create a TTL index to deal with out-of-date data in a time-based manner.
The benefits of the way it is handled
1 simple and convenient
(2) to deal with it in the way of the database system itself, efficient and secure
3 the database will automatically judge whether to clean up the data according to its own performance and database state, rather than with stored procedures or other ways, to deal with it when it comes to time, regardless of the current situation of the database.
The TTL index itself is a special single-field index, and you can create a field with the expiredAfterSecond option in a normal way to create an index. The value of the field needs to be a date type, or an array with a date type.
Well, let's first understand what the date data is and how it is reflected in MONGODB.
1 because mongodb itself is a distributed database, at the beginning of the design, the concept of time is the concept of UTC, so the isodate of the time type of mongodb, with the 0 time zone of the world as the unified expression of time, does not have the concept of time zone, so the time you see should increase or decrease the corresponding time in your own time zone.
The function that can return time in 2 MONGODB (with the latest version 4.2) has two Date (), new Date (), the former one returns the value of the character type, and the latter returns the time of UTC. For more information on how to adjust the time value returned by new Date (), please refer to the documentation.
Here is the automatically generated test data
In the test data, only creationDate is of type ISODATE, and only this field can be used as the only standard field to determine whether the data is out of date. Adding an index requires creating an TTL index on a field that holds a value of the BSON date type or an array of objects, and specifying a non-negative, non-zero value in expireAfterSeconds. When the number of seconds in a field exceeds the time specified by its index, the document expires and cleanup begins.
From the test, the speed of deleting documents is faster.
Here are a few things TTL indexes need to know
1 TTL index does not guarantee that expired data will be deleted immediately after the index is generated
2 federated indexes are not supported
3 the scheduling task for deleting documents runs once in 60 seconds
(4) for overloaded systems, task scheduling will be skipped and priority will be given to the system to provide normal services.
5. Non-master members of replica set members will not delete data automatically and only accept delete instructions from the master database.
6 the TTL index itself supports query usage
7 after the establishment of the index, can not change the value of expireAfterSeconds, need to delete the index, in re-establishment, so add values, must be good.
8 create fields of TTL, which cannot be fields of other existing indexes, otherwise you cannot add expired function.
You can view the number of deleted documents through db.serverStatus ()
Of course, you can also use logs to monitor the operation of deleting logs.
The default monitoring mode is turned off, and you need to use the following command to turn it on
Db.setLogLevel (1, "index")
Or you don't need monitoring, you can turn it off by using the following command
Db.adminCommand ({setParameter:1, ttlMonitorEnabled:false})
At this point, I believe you have a deeper understanding of "MONGODB TTL how to index the killer of out-of-date data". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.