Skip to content

DOCS-664 migrate 'optimize storage small object` #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 1, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions source/faq/developers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,56 @@ flag to modify the file preallocation strategy.

.. seealso:: This wiki page that address :wiki:`MongoDB disk use <Excessive+Disk+Space>`.

How do I optimize storage for small documents
---------------------------------------------

Each MongoDB document contains a certain amount of overhead. This
overhead is normally insignificant but becomes significant if your
documents are just a few bytes, as might be the case for documents with
just one or two fields. Below are some suggestions on how to optimize
storage efficiently in such situations.

- Use the ``_id`` field explicitly.

MongoDB clients automatically add an :term:`ObjectId` to each document and set it to
a unique value. Additionally this field in indexed. For tiny documents
this takes up significant space.

To optimize, users can provide a value for the ``_id`` field
explicitly. The goal is to leverage the ``_id`` field to store a value
that would have occupied space in another portion of the document. The
only requirement for the ``_id`` field value is that it must serve as
a primary key for documents in the collection by uniquely identifying
them. If the field's value is not unique, then it cannot serve as a
primary key as there would be collisions in the namespace.

- Use shorter field names. This is recommended only in certain situations.

For example, the strings ``last_name`` and ``best_score`` in this
record:

.. code-block:: javascript

{ last_name : "Smith", best_score: 3.9 }

could be shortened to ``lname`` and ``score``, which would save 9
bytes per document:

.. code-block:: javascript

{ lname : "Smith", score : 3.9 }

Shortening field names reduces expressiveness and is not recommended
unless you have a collection where document overhead is of significant
concern. Also, shortening field names does not reduce index size, as
indexes have a predefined structure. In general it is not necessary to
use short field names.

- Embed documents.

In some cases you may be able to embed documents in other documents and save
on the per document overhead.

How does MongoDB address SQL or Query injection?
------------------------------------------------

Expand Down