Skip to content

DOCS-489 aggregation operators and indexes #227

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 3 commits into from
Sep 13, 2012
Merged
Show file tree
Hide file tree
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
31 changes: 28 additions & 3 deletions source/applications/aggregation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,45 @@ Document size <limit-bson-document-size>` limit, which is currently 16 megabytes
Optimizing Performance
----------------------

Early Filtering
~~~~~~~~~~~~~~~

Because you will always call :method:`aggregate` on a
:term:`collection` object, which logically inserts the *entire* collection into
the aggregation pipeline, you may want to optimize the operation
by avoiding scanning the entire collection whenever possible.

Pipeline Operators and Indexes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Depending on the order in which they appear in the pipeline,
aggregation operators can take advantage of indexes.

The following pipeline operators take advantage of an index when they
occur at the beginning of the pipeline:

- :agg:pipeline:`$match`
- :agg:pipeline:`$sort`
- :agg:pipeline:`$limit`
- :agg:pipeline:`$skip`.

The above operators can also use an index when placed **before** the
following aggregation operators:

- :agg:pipeline:`$project`
- :agg:pipeline:`$unwind`
- :agg:pipeline:`$group`.

Early Filtering
~~~~~~~~~~~~~~~

If your aggregation operation requires only a subset of the data in a
collection, use the :agg:pipeline:`$match` operator to restrict which items go
in to the top of the pipeline, as in a query. When placed early in a
pipeline, these :agg:pipeline:`$match` operations use suitable indexes
to scan only the matching documents in a collection.

Placing :agg:pipeline:`$match` then :agg:pipeline:`$sort` at the very
start of the pipeline would be logically equivalent to a single query
and a sort, and be able to use an index.

.. OMMITED: this feature is pending SERVER-4506. Other optimizations
.. are pending SERVER-4507 SERVER-4644 SERVER-4656 SERVER-4816
..
Expand Down
10 changes: 10 additions & 0 deletions source/reference/aggregation/sort.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ $sort

.. TODO mention the importance of order preserving objects

- :agg:pipeline:`$skip`

:pipeline:`$sort` operator can take advantage of an index when
placed at the **beginning** of the pipleline or placed **before**
the following aggregation operators:

- :agg:pipeline:`$project`
- :agg:pipeline:`$unwind`
- :agg:pipeline:`$group`.

.. warning:: Unless the :pipeline:`$sort` operator can use an index,
in the current release, the sort must fit within memory. This
may cause problems when sorting large numbers of documents.
Expand Down