0

Im getting stuck with archiving a huge amount of data in MongoDB 3.6

I want to delete 56 crore records in a collection. I tried to remove using bulk.remove(), but that is also slow.(50 records are removing per second).

But somewhere I read, TTL index and do scan interval every 1hour. So it'll remove in faster way.

But if I create this index in foreground, it'll lock the collection. So im thinking to do with the rolling index creation method.

If do like that, lets say on a 3 node replica set, detach the node3 and then create index. Once its created it'll start automatically remove the data. Then once I add the node back to the Replica set, maybe the primary will do the delete after I create the index, that time it'll try to replicate, in the worst case, the data is already removed on that node, then it'll break the replication?

0

Can I create TTL index with rolling method

Yes, this is a supported approach for building indexes on replica sets. However, if your goal is to efficiently remove a large quantity of existing documents there are some caveats to be aware of as noted below.

I tried to remove using bulk.remove(), but that is also slow.(50 records are removing per second).

A TTL index will not speed up removal of documents if you already have an index that supports finding expired documents: the TTL thread still needs to find & remove matching documents so will be doing similar work to a bulk remove.

I would investigate why your current bulk remove operations are slow. For example, make sure you have an optimal index in place to find documents to remove and monitor your system resources (memory, I/O, network, ...) to ensure there aren't any obvious bottlenecks.

If you have a large number of documents that are ready to be removed when the TTL index is created, this could have a significant performance impact. Bulk remove queries with a supporting index would allow more control over the impact since you can add query criteria to restrict the range of documents matching each bulk deletion.

But somewhere I read, TTL index and do scan interval every 1hour. So it'll remove in faster way.

That timing is incorrect: the TTL deletion task runs every 60 seconds. Based on an indexed date field the TTL monitor can either expire documents after a specified number of seconds has passed or expire documents at a specific clock time.

Assuming your documents have a range of expiry dates, once the initial removal of expired documents is complete a TTL index will be able to delete documents in smaller batches which will be less impactful than an infrequent bulk delete.

But if I create this index in foreground, it'll lock the collection. So im thinking to do with the rolling index creation method.

Prior to MongoDB 4.2, a foreground index build on a populated collection will block all other operations on the database that holds that collection. For a populated collection in a production environment you will definitely want to use either a rolling index build or a background index build. The rolling index build ensures that only one of your replica set members is building an index and allows a foreground index build to complete faster, however this approach does include some risk of that member becoming stale while running in standalone mode.

MongoDB 4.2+ uses an optimised index build process that limits the lock scope to the affected collection and only holds an exclusive lock at the beginning and end of the index build. You can still use the rolling index build approach but there is no longer a foreground vs background index build distinction.

If do like that, lets say on a 3 node replica set, detach the node3 and then create index. Once its created it'll start automatically remove the data.

The TTL index thread on replica set members only deletes documents when a member is in the primary state. Document deletes are replicated via the oplog so secondaries always have a consistent point of time with the current primary.

If you restart a replica set member in standalone mode, the TTL collection monitor will not be started (again, to keep the secondary state consistent).

  • Thanks a lot for your detailed answer. Its a 3 shard infra. Each shard is having 3 node replica set. If I want to create the TTL index in rolling update way on all the 9 nodes, will it affect anything in sharding process? – Bhuvanesh Oct 10 at 7:22
  • Do you have arbiters in your replica sets? Also, what replica set protocol version are you using (pv0 or pv1)? Without arbiters your deployment will be fine as long as you keep a majority of your voting replica set members online. If you are using arbiters and pv1, having a secondary offline will create cache pressure on the primary and you will want to disable Read Concern Majority. I strongly recommend not using arbiters. – Stennie Oct 10 at 10:11
  • I don't have any arbiters – Bhuvanesh Oct 10 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.