Skip to content

Commit d823f15

Browse files
author
Sam Kleinman
committed
merge: DOCS-685
2 parents f019e28 + e7053bf commit d823f15

File tree

3 files changed

+145
-1
lines changed

3 files changed

+145
-1
lines changed

source/core/write-operations.txt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,105 @@ write operation that affects multiple documents using the
341341
To isolate a sequence of write operations from other read and write
342342
operations, see :doc:`/tutorial/perform-two-phase-commits`.
343343

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+
344443
Architecture
345444
------------
346445

source/faq/developers.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,49 @@ information on indexes.
660660
.. Commenting out.. If your small documents are approximately the page
661661
cache unit size, there is no benefit for ram cache efficiency, although
662662
embedding will provide some benefit regarding random disk i/o.
663+
664+
.. _faq-developers-manual-padding:
665+
666+
Can I manually pad documents to prevent moves during updates?
667+
-------------------------------------------------------------
668+
669+
An update can cause a document to move on disk if the document grows in
670+
size. To *minimize* document movements, MongoDB uses
671+
:ref:`padding <write-operations-padding-factor>`.
672+
673+
You should not have to pad manually because MongoDB adds
674+
:ref:`padding automatically <write-operations-padding-factor>` and can
675+
adaptive adjust the amount of padding added to documents to prevent
676+
document relocations following updates.
677+
678+
You can change the default :stats:`paddingFactor` calculation by using
679+
the the :dbcommand:`collMod` command with the
680+
:collflag:`usePowerOf2Sizes` flag. The :collflag:`usePowerOf2Sizes`
681+
flag ensures that MongoDB allocates document space in sizes that are
682+
powers of 2, which helps ensure that MongoDB can efficiently reuse
683+
free pace created by document deletion or relocation.
684+
685+
However, in those exceptions where you must pad manually, you can use
686+
the strategy of first adding a temporary field to a document and then
687+
:operator:`$unsetting <$unset>` the field, as in the following example:
688+
689+
.. code-block:: javascript
690+
691+
var myTempPadding = [ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
692+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
693+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
694+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"];
695+
696+
db.myCollection.insert( { _id: 5, paddingField: myTempPadding } );
697+
698+
db.myCollection.update( { _id: 5 },
699+
{ $unset: { paddingField: "" } }
700+
)
701+
702+
db.myCollection.update( { _id: 5 },
703+
{ $set: { realField: "Some text that I might have needed padding for" } }
704+
)
705+
706+
.. seealso::
707+
708+
:ref:`write-operations-padding-factor`

themes/mongodb/static/mongodb-docs.css_t

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,6 @@ div.section > h2, div.section > h3,div.section > h4 {
727727

728728
div.admonition p {
729729
line-height:1.5em;
730-
padding: 1em 0;
731730
}
732731

733732
dd > div.admonition { margin-left: 0; }

0 commit comments

Comments
 (0)