Optimize Query Performance
Create Indexes to Support Queries
For commonly issued queries, create indexes. If aquery searches multiple fields, create a compound index. Scanning an index is much faster than scanning acollection. The indexes structures are smaller than the documentsreference, and store references in order.
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 theauthor_name
field:
- db.posts.createIndex( { author_name : 1 } )
Indexes also improve efficiency on queries that routinely sort on agiven field.
Example
If you regularly issue a query that sorts on thetimestamp
field, then you can optimize the query by creating anindex on the timestamp
field:
Creating this index:
- db.posts.createIndex( { timestamp : 1 } )
Optimizes this query:
- db.posts.find().sort( { timestamp : -1 } )
Because MongoDB can read indexes in both ascending and descendingorder, the direction of a single-key index does not matter.
Indexes support queries, update operations, and some phases of theaggregation pipeline.
Index keys that are of the BinData
type are more efficiently storedin the index if:
- the binary subtype value is in the range of 0-7 or 128-135, and
- the length of the byte array is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12,14, 16, 20, 24, or 32.
Limit the Number of Query Results to Reduce Network Demand
MongoDB cursors return results in groups of multipledocuments. If you know the number of results you want, you can reducethe demand on network resources by issuing the 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:
- db.posts.find().sort( { timestamp : -1 } ).limit(10)
For more information on limiting results, see limit()
Use Projections to Return Only Necessary Data
When you need only a subset of fields from documents, you can achieve betterperformance by returning only the fields you need:
For example, if in your query to the posts
collection, you need onlythe timestamp
, title
, author
, and abstract
fields, youwould issue the following command:
- db.posts.find( {}, { timestamp : 1 , title : 1 , author : 1 , abstract : 1} ).sort( { timestamp : -1 } )
For more information on using projections, seeProject Fields to Return from Query.
Use $hint to Select a Particular Index
In most cases the query optimizer selects the optimal index for aspecific operation; however, you can force MongoDB to use a specificindex using the hint()
method. Usehint()
to support performance testing, or onsome queries where you must select a field or field included inseveral indexes.
Use the Increment Operator to Perform Operations Server-Side
Use MongoDB’s $inc
operator to increment or decrementvalues in documents. The operator increments the value of the field onthe server side, as an alternative to selecting a document, makingsimple modifications in the client and then writing the entiredocument to the server. The $inc
operator can also helpavoid race conditions, which would result when two applicationinstances queried for a document, manually incremented a field, andsaved the entire document back at the same time.