@@ -341,6 +341,105 @@ write operation that affects multiple documents using the
341
341
To isolate a sequence of write operations from other read and write
342
342
operations, see :doc:`/tutorial/perform-two-phase-commits`.
343
343
344
+ .. _write-operations-padding-factor:
345
+
346
+ Padding Factor
347
+ --------------
348
+
349
+
350
+ If an update operation does not cause the document to increase in
351
+ size, MongoDB can apply the update in-place in-place. Some updates
352
+ change the size of the document, for example using the
353
+ :operator:`$push` operator to append a sub-document to an array can
354
+ cause the top level document to grow beyond its allocated space.
355
+
356
+ When documents grow, MongoDB relocates the document on disk with
357
+ enough contiguous space to hold the document. These relocations
358
+ take longer than in-place updates, particularly if the collection has
359
+ indexes that MongoDB must update all index entries. If
360
+ collection has many indexes, the move will impact write throughput.
361
+
362
+ To minimize document movements, MongoDB employs padding. MongoDB
363
+ adaptively learns if documents in a collection tend to grow, and if
364
+ they do, adds a :stats:`paddingFactor` so that the documents have room
365
+ to grow on subsequent writes. The :stats:`paddingFactor` indicates the
366
+ padding for new inserts and moves.
367
+
368
+ .. versionadded:: 2.2
369
+ You can use the :dbcommand:`collMod` command
370
+ with the :collflag:`usePowerOf2Sizes` flag so that MongoDB
371
+ allocates document space in sizes that are powers of 2. This helps
372
+ ensure that MongoDB can efficiently reuse the space freed as a
373
+ result of deletions or document relocations. As with all padding,
374
+ using document space allocations with power of 2 sizes minimizes,
375
+ but does not eliminate, document movements.
376
+
377
+ To check the current :stats:`paddingFactor` on a collection, you can
378
+ run the :dbcommand:`db.collection.stats()` command in the
379
+ :program:`mongo` shell, as in the following example:
380
+
381
+ .. code-block:: javascript
382
+
383
+ db.myCollection.stats()
384
+
385
+ Since MongoDB writes each document at a different point in time, the
386
+ padding for each document will not be the same. You can calculate the
387
+ padding size by subtracting 1 from the :stats:`paddingFactor`, for
388
+ example:
389
+
390
+ .. code-block:: none
391
+
392
+ padding size = (paddingFactor - 1) * <document size>.
393
+
394
+ For example, a ``paddingFactor`` of ``1.0`` specifies no padding
395
+ whereas a paddingFactor of ``1.5`` specifies a padding size of ``0.5`` or 50
396
+ percent (50%) of the document size.
397
+
398
+ Because the :stats:`paddingFactor` is relative to the size of each
399
+ document, you cannot calculate the exact amount of padding for a
400
+ collection based on the average document size and padding factor.
401
+
402
+ If an update operation causes the document to *decrease* in size, for
403
+ instance if you perform an :operator:`$unset` or a :operator:`$pop`
404
+ update, the document remains in place and effectively has more
405
+ padding. If the document remains this size, the space is not reclaimed
406
+ until you perform a :dbcommand:`compact` or a
407
+ :dbcommand:`repairDatabase` operation.
408
+
409
+ .. note::
410
+
411
+ The following operations remove padding:
412
+
413
+ - :dbcommand:`compact`,
414
+ - :dbcommand:`repairDatabase`, and
415
+ - initial replica sync operations.
416
+
417
+ However, with the :dbcommand:`compact` command, you can run the
418
+ command with a ``paddingFactor`` or a ``paddingBytes`` parameter.
419
+
420
+ Padding is also removed if you use :program:`mongoexport` from a
421
+ collection. If you use :program:`mongoimport` into a new
422
+ collection, :program:`mongoimport` will not add padding. If you
423
+ use :program:`mongoimport` with an existing collection with
424
+ padding, :program:`mongoimport` will not affect the existing
425
+ padding.
426
+
427
+ When a database operation removes padding, subsequent update that
428
+ require changes in record sizes will have reduced throughput until
429
+ the collection's padding factor grows. Padding does not affect
430
+ in-place, and after :dbcommand:`compact`,
431
+ :dbcommand:`repairDatabase`, and replica set initial sync
432
+ the collection will require less storage.
433
+
434
+ .. COMMENT -- not sure if we really need this manual padding info or if
435
+ it belongs here, but ...
436
+
437
+ .. seealso::
438
+
439
+ - :ref:`faq-developers-manual-padding`
440
+
441
+ - `Fast Updates with MongoDB with in-place Updates <http://blog.mongodb.org/post/248614779/fast-updates-with-mongodb-update-in-place>`_ (blog post)
442
+
344
443
Architecture
345
444
------------
346
445
0 commit comments