mongodbfragmentation

Can MongoDB have fragmentation?


I have heard that if the goal is to modify a data in SQL, deleting and re-saving it is not a good idea because it can lead to fragmentation issues. Does this also happen in MongoDB? If so, how should I edit the information inside a collection?

Basically, now I have a function that saves data in a collection and another one that deletes. To edit one of those data, I delete it and save it with modifications.


Solution

  • Problem Description: Releasing allocated storage space usually impact performance so to achieve better performance during peak load sometimes is better to keep allocated deleted blocks and release them later manually , that was the case in mongoDB with the previous default storage engine mmapv1 not supporting compression and keeping deleted blocks allocated , but since mongoDB version >=3.2 the default storage engine is wiredTiger where there is compression and this process is improved alot so it has no such an impact on pre-allocated space.

    Workaraund: After some time if your application do multiple deletes and inserts it is always good to optimize and re-index the data , in mongoDB production systems this is fearly easy usually done via init-sync for all replicaSet members sequentially.

    As a general rule: If you need to update small amount of data in a document better do update(in-place) ,but if your modification expect to allocate more space then the old document occupy better you delete and insert after , same apply if you need to update multiple documents.