-
Notifications
You must be signed in to change notification settings - Fork 1.7k
DOCS-665 new optimization page #451
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
============ | ||
Optimization | ||
============ | ||
|
||
.. default-domain:: mongodb | ||
|
||
This section describes techniques for optimizing database performance. | ||
|
||
.. seealso:: :ref:`aggregation-optimize-performance` | ||
|
||
Use Indexes | ||
----------- | ||
|
||
For commonly issued queries, create :doc:`indexes </indexes>`. If a | ||
query searches multiple fields, create a :ref:`compound index | ||
<index-type-compound>`. Scanning an index is much faster than scanning a | ||
collection. Items in an index are ordered and smaller than the documents | ||
they summarize. | ||
|
||
.. example:: If you have a ``posts`` collection containing blog posts, | ||
and if you regularly issue a query that sorts on the ``author_name`` | ||
field, then you can optimize the query by creating an index on the | ||
``author_name`` field: | ||
|
||
.. code-block:: javascript | ||
|
||
db.posts.ensureIndex( { author_name : 1 } ) | ||
|
||
Indexes also improve efficiency on queries that routinely sort on a | ||
given field. | ||
|
||
.. example:: If you regularly issue a query that sorts on the | ||
``timestamp`` field, then you can optimize the query by creating an | ||
index on the ``timestamp`` field: | ||
|
||
Creating this index: | ||
|
||
.. code-block:: javascript | ||
|
||
db.posts.ensureIndex( { timestamp : 1 } ) | ||
|
||
Optimizes this query: | ||
|
||
.. code-block:: javascript | ||
|
||
db.posts.find().sort( { timestamp : -1 } ) | ||
|
||
Direction on a single-key index does not matter. You can store the index | ||
in either direction. | ||
|
||
In certain cases, indexes speed performance for fields used by | ||
aggregation operators. See | ||
:ref:`aggregation-pipeline-operators-and-performance` for more | ||
information. | ||
|
||
Limit Results | ||
------------- | ||
|
||
MongoDB :term:`cursors <cursor>` return results in groups of multiple | ||
documents. If you know the number of results you want, you can reduce | ||
the demand on network resources by issuing the :method:`cursor.limit()` | ||
method. | ||
|
||
This is typically used in conjunction with sort operations. For example, | ||
if you need only 10 results from your query to the ``posts`` | ||
collection, you would issue the following command: | ||
|
||
.. code-block:: javascript | ||
|
||
db.posts.find().sort( { timestamp : -1 } ).limit(10) | ||
|
||
For more information on limiting results, see :method:`cursor.limit()` | ||
|
||
Use Projections to Return Only Necessary Data | ||
--------------------------------------------- | ||
|
||
When you need only a subset of fields from documents, you can achieve better | ||
performance by returning only the fields you need: | ||
|
||
For example, if in your query to the ``posts`` collection, you need only | ||
the ``timestamp``, ``title``, ``author``, and ``abstract`` fields, you | ||
would issue the following command: | ||
|
||
.. code-block:: javascript | ||
|
||
db.posts.find( {}, { timestamp : 1 , title : 1 , author : 1 , abstract : 1} ).sort( { timestamp : -1 } ) | ||
|
||
For more information on using projections, see | ||
:ref:`read-operations-projection`. | ||
|
||
Use the Database Profiler to Evaluate Performance | ||
------------------------------------------------- | ||
|
||
.. todo Add link below: :doc:`database profiler </tutorial/manage-the-database-profiler>` | ||
|
||
MongoDB provides a database profiler that shows performance | ||
characteristics of each operation against the database. Use the profiler | ||
to locate any queries or write operations that are running slow. You can | ||
use this information, for example, to determine what indexes to create. | ||
|
||
.. todo Add below: , see :doc:`/tutorial/manage-the-database-profiler` and ... | ||
|
||
For more information, see :ref:`database-profiling`. | ||
|
||
Use db.currentOp() to Evaluate Performance | ||
------------------------------------------ | ||
|
||
The :method:`db.currentOp()` method reports on current operations | ||
running on a :program:`mongod` instance. For more information, see | ||
:doc:`/reference/current-op`. | ||
|
||
Use $explain to Evaluate Performance | ||
------------------------------------ | ||
|
||
Use :method:`explain <cursor.explain()>` to return statistics on the | ||
query, including what index MongoDB selected to fulfill the query. | ||
|
||
.. todo Link to Kay's new explain doc | ||
|
||
Use $hint to Select a Particular Index | ||
-------------------------------------- | ||
|
||
In most cases the :ref:`query optimizer | ||
<read-operations-query-optimization>` selects the best index. But | ||
:method:`hint <cursor.hint()>` might help when you query on multiple | ||
fields that are indexed in separate indexes. You can :method:`hint | ||
<cursor.hint()>` to specify the index to use, potentially improving | ||
performance. | ||
|
||
Use the Increment Operator to Perform Operations Server-Side | ||
------------------------------------------------------------ | ||
|
||
Use MongoDB's :operator:`$inc` operator to increment fields. The | ||
operator increments fields on the server side, which can be much faster | ||
than updating a document on the client side. Specifically, using | ||
:operator:`$inc` is much faster than selecting a document, incrementing | ||
a field in your application, and then writing the entire document back | ||
to the server. | ||
|
||
The :operator:`$inc` operator also avoids race conditions, as would | ||
occur if two threads simultaneously queried for a document, manually | ||
incremented a field, and saved the entire document back. | ||
|
||
Perform Server-Side Code Execution | ||
---------------------------------- | ||
|
||
Occasionally, for maximum performance, you might want to perform an | ||
operation on the database server to eliminate client/server network | ||
turnarounds. For example, if you want to remove a field from all | ||
documents in a collection, performing the operation directly on the | ||
server is more efficient than transmitting the collection to your client | ||
and back again. | ||
|
||
For more information, see :wiki:`Server-side Code Execution <Server-side+Code+Execution>`. | ||
|
||
Use Capped Collections | ||
---------------------- | ||
|
||
:doc:`/core/capped-collections` are circular, fixed-size collections that | ||
keep documents well-ordered, even without the use of an index. This | ||
means that capped collections can receive very high-speed writes and | ||
sequential reads. | ||
|
||
These collections are particularly useful for keeping log files but are | ||
not limited to that purpose. Use capped collections where appropriate. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for keeping log-file type data? |
||
|
||
Use Natural Order | ||
----------------- | ||
|
||
To return documents in the order they exist on disk, use the | ||
:operator:`$natural` operator. :term:`Natural order <natural order>` | ||
does not use indexes but can be fast for operations where the first or | ||
last items on disk are required, particularly to operations on capped | ||
collections. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sort of? cursors are iterables/generators? so this is more a driver tunable, as you set up.
the optimization that you want to talk about is reducing network traffic when you know you only want a certain number of results. Typically. used in conjunction with sort operations as in the following?