Let’s Talk about MongoDB’s Compact Command

Why Use the Compact Command

The Remove and Drop Commands and How They Compare with the Compact Command

  • db.collection.remove({}, {multi: true}): With this command, files are removed from B-tree one by one, and finally all files are removed, but the physical space of the files will not be reclaimed, as shown in the figure above.
  • db.collection.drop(): With this command, the physical files in the collection are dropped, and the space is immediately reclaimed.

How the Compact Command Affects Write Performance

How the Compact Command Works

  1. Of the 80% of the space in the front of the file, 20% of the space is free for writing 20% of the data in the back of the file;
  2. Of the 90% of the space in the front of the file, 10% of the space is free for writing 10% of the data in the back of the file.

How Much Space Is Reclaimed by the Compact Command

mymongo:PRIMARY> db.coll.stats().wiredTiger["block-manager"]["file bytes available for reuse"]
5033984

What to Do Before You Run the Compact Command

// compact somedb.somecollection
use somedb
db.runCommnd({compact: "somecollection"})
// compact oplog,execute the force option on the copy set primary
use local
db.runCommnd({compact: "somecollection", force: true})

Original Source

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Master Procrastinator

Recursion

Instance-per-Pod Webhook: IaaS-level isolation for Kubernetes Pods

Directus 9 CMS + Gridsome

Kubernetes metallb bare metal loadbalancer — AdminTome Blog

ELK stack on a Kubernetes cluster: A hard way

60 Practices For Quality Engineering : Architecture (Part 2)

Installing and switching between multiple versions of PHP (The best and easy way) on mac

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

The Craft of Coding and Code Metrics for Open Source

What exactly is Apache Maven?

How to update Appsettings.json based on deployment environment using yaml.

Understanding Docker Architecture

Understanding Docker Architecture