What's A Padding Factor?
- An update that doesn't change a document's underlying size can be done in-place, without moving the document.
- An update which does change a document's size forces MongoDB to move the document and update indexes, which is slower.
- MongoDB can add extra padding to new documents which can allow in-place updates even when the size changes.
- $inc an int doesn't usually increase a document's size since the field type usually remains the same.
- Using $push to add a new embedded document is good example of a document changing size.
- Analysis of past updates is used to determine what % of a document's size should be added as padding.
db.unicorns.insert({name: 'sporkles', vampires: 10})
db.unicorns.stats().paddingFactor
1
db.unicorns.update({name: 'sporkles'}, {$inc: {vampires: 10}})
db.unicorns.stats().paddingFactor
1
db.unicorns.update({name: 'sporkles'}, {$set: {zombies: 2}})
db.unicorns.stats().paddingFactor
1.59
db.unicorns.compact()
db.unicorns.stats().paddingFactor
1