Skip to content

Commit 7be9c4d

Browse files
author
Bob Grabar
committed
DOCS-206 updated indexing strategies per review edits
1 parent 38bae5a commit 7be9c4d

File tree

1 file changed

+29
-42
lines changed

1 file changed

+29
-42
lines changed

source/applications/indexes.txt

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,14 @@ create an index on both ``category`` and ``item``.
6060

6161
This allows you both options. You can query on just ``category``, and
6262
you also can query on ``category`` combined with ``item``.
63-
64-
To query on multiple keys and sort the results, see :ref:`index-sort`.
63+
(To query on multiple keys and sort the results, see :ref:`index-sort`.)
6564

6665
With the exception of queries that use the :operator:`$or` operator, a
6766
query cannot use multiple indexes. A query must use only one index.
6867

6968
.. _covered-queries:
7069
.. _indexes-covered-queries:
7170

72-
7371
Use Compound Indexes to Support Several Different Queries
7472
---------------------------------------------------------
7573

@@ -116,18 +114,20 @@ can support all the queries that search a "prefix" subset of those fields.
116114
Create Indexes that Support Covered Queries
117115
-------------------------------------------
118116

119-
A covered query is a query in which all the search keys are found in a
117+
A covered index query is a query in which all the search keys are found in a
120118
given index. A covered query is considered to be "covered" by the index.
121119
MongoDB can fulfill the query by using *only* the index. MongoDB need
122120
not scan documents from the database.
123121

124122
Querying *only* the index is much faster than querying documents.
125-
Indexes are smaller than the documents they catalog, and indexes are
123+
Indexes keys are typically smaller than the documents they catalog, and indexes are
126124
typically stored in RAM or located sequentially on disk.
127125

128126
Mongod automatically uses a covered query when possible. To ensure use
129127
of a covered query, create an index that includes all the fields listed
130-
in the query result. This means that the :term:`projection` must
128+
in the query result. This means that the :term:`projection` document
129+
given to a query (to specify which fields MongoDB returns from
130+
the result set) must
131131
explicitly exclude the ``_id`` field from the result set, unless the
132132
index includes ``_id``.
133133

@@ -148,17 +148,12 @@ Use Indexes to Sort Query Results
148148
---------------------------------
149149

150150
For the fastest performance when sorting query results by a given field,
151-
create a sorted index on that field. To sort query results on multiple
152-
fields, create a :ref:`compound index <index-type-compound>`. For
153-
details on creating compound indexes with sort in mind, see
154-
:ref:`index-ascending-and-descending`.
155-
156-
MongoDB uses a compound index to return sorted results *if*:
151+
create a sorted index on that field.
157152

158-
- The first field in the index is the first sorted field.
159-
160-
- The last field in the index *before the first sorted field* is an
161-
equality match in the query.
153+
To sort query results on multiple fields, create a :ref:`compound index
154+
<index-type-compound>`. MongoDB sorts results based on the field order
155+
in the index. Ensure that any index constraints on index fields that are
156+
before the first sort field are equalities.
162157

163158
.. example::
164159

@@ -194,16 +189,13 @@ MongoDB uses a compound index to return sorted results *if*:
194189

195190
db.collection.find().sort( { b:1 } )
196191
db.collection.find( { b:5 } ).sort( { b:1 } )
197-
db.collection.find( { b:{ $gt:5 } } ).sort( { a:1, b:1 } )
198192

199193
.. note::
200194

201-
Sorting query results by an index is faster than sorting results by
202-
the :method:`sort() <cursor.sort()>` method. The method supports
203-
in-memory sort operations without the use of an index, but these
204-
operations are significantly slower than sort operations that use an
205-
index, and they abort when the sort operation consume 32 megabytes of
206-
memory.
195+
When the :method:`sort() <cursor.sort()>` method performs an
196+
in-memory sort operation without the use of an index, the operation
197+
is significantly slower than for operations that use an index, and
198+
the operation aborts when it consumes 32 megabytes of memory.
207199

208200
.. _indexes-ensure-indexes-fit-ram:
209201

@@ -227,15 +219,12 @@ this index fits in RAM, you must not only have more than that much RAM
227219
available but also must have RAM available for the rest of the
228220
:term:`working set`. Also remember:
229221

230-
- If you have and use multiple collections, you must consider the size
231-
of all indexes on all collections. The indexes and the working set must be able to
232-
fit RAM at the same time.
222+
If you have and use multiple collections, you must consider the size
223+
of all indexes on all collections. The indexes and the working set must be able to
224+
fit RAM at the same time.
233225

234-
- All of your indexes use less space than all of the documents in the
235-
collection. This may not be an issue if all your queries use
236-
:ref:`covered queries <covered-queries>` or if indexes do not need to
237-
fit into RAM. There are some limited cases where indexes do not need
238-
to fit in RAM. See :ref:`indexing-right-handed`.
226+
There are some limited cases where indexes do not need
227+
to fit in RAM. See :ref:`indexing-right-handed`.
239228

240229
.. seealso:: For additional :doc:`collection statistics
241230
</reference/collection-statistics>`, use :dbcommand:`collStats` or
@@ -263,14 +252,10 @@ Selectivity is the ability of a query to narrow results using the index.
263252
Effective indexes are more selective and allow MongoDB to use the index
264253
for a larger portion of the work associated with fulfilling the query.
265254

266-
To ensure selectivity:
267-
268-
- Only index keys that have a high high distribution of the values
269-
within the collection.
270-
271-
- Write queries that limit the number of possible documents with the
272-
indexed field. Write queries that are appropriately selective relative
273-
to your indexed data.
255+
To ensure selectivity,
256+
write queries that limit the number of possible documents with the
257+
indexed field. Write queries that are appropriately selective relative
258+
to your indexed data.
274259

275260
.. example::
276261

@@ -345,7 +330,7 @@ Consider Performance when Creating Indexes for Write-heavy Applications
345330
-----------------------------------------------------------------------
346331

347332
If your application is write-heavy, then be careful when creating new
348-
indexes, since each additional index with impose a small
333+
indexes, since each additional index with impose a
349334
write-performance penalty. In general, don't be careless about adding
350335
indexes. Indexes should be added to complement your queries. Always have
351336
a good reason for adding a new index, and make sure you've benchmarked
@@ -357,8 +342,10 @@ Consider Insert Throughput
357342
.. TODO insert link to /source/core/write-operations when that page is complete.
358343
Do we want to link to write concern? -bg
359344

360-
MongoDB must update all indexes associated with a collection after every
361-
insert, update, or delete operation. Therefore, every index on a
345+
MongoDB must update *all* indexes associated with a collection after every
346+
insert, update, or delete operation. (With updates, if the updated document
347+
does not move to a new location, then only the modified, indexed fields
348+
are updated in the index.) Therefore, every index on a
362349
collection adds some amount of overhead to these operations. In almost
363350
every case, the performance gains that indexes realize for read
364351
operations are worth the insertion penalty. However, in some cases:

0 commit comments

Comments
 (0)