@@ -60,16 +60,14 @@ create an index on both ``category`` and ``item``.
60
60
61
61
This allows you both options. You can query on just ``category``, and
62
62
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`.)
65
64
66
65
With the exception of queries that use the :operator:`$or` operator, a
67
66
query cannot use multiple indexes. A query must use only one index.
68
67
69
68
.. _covered-queries:
70
69
.. _indexes-covered-queries:
71
70
72
-
73
71
Use Compound Indexes to Support Several Different Queries
74
72
---------------------------------------------------------
75
73
@@ -116,18 +114,20 @@ can support all the queries that search a "prefix" subset of those fields.
116
114
Create Indexes that Support Covered Queries
117
115
-------------------------------------------
118
116
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
120
118
given index. A covered query is considered to be "covered" by the index.
121
119
MongoDB can fulfill the query by using *only* the index. MongoDB need
122
120
not scan documents from the database.
123
121
124
122
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
126
124
typically stored in RAM or located sequentially on disk.
127
125
128
126
Mongod automatically uses a covered query when possible. To ensure use
129
127
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
131
131
explicitly exclude the ``_id`` field from the result set, unless the
132
132
index includes ``_id``.
133
133
@@ -148,17 +148,12 @@ Use Indexes to Sort Query Results
148
148
---------------------------------
149
149
150
150
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.
157
152
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 .
162
157
163
158
.. example::
164
159
@@ -194,16 +189,13 @@ MongoDB uses a compound index to return sorted results *if*:
194
189
195
190
db.collection.find().sort( { b:1 } )
196
191
db.collection.find( { b:5 } ).sort( { b:1 } )
197
- db.collection.find( { b:{ $gt:5 } } ).sort( { a:1, b:1 } )
198
192
199
193
.. note::
200
194
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.
207
199
208
200
.. _indexes-ensure-indexes-fit-ram:
209
201
@@ -227,15 +219,12 @@ this index fits in RAM, you must not only have more than that much RAM
227
219
available but also must have RAM available for the rest of the
228
220
:term:`working set`. Also remember:
229
221
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.
233
225
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`.
239
228
240
229
.. seealso:: For additional :doc:`collection statistics
241
230
</reference/collection-statistics>`, use :dbcommand:`collStats` or
@@ -263,14 +252,10 @@ Selectivity is the ability of a query to narrow results using the index.
263
252
Effective indexes are more selective and allow MongoDB to use the index
264
253
for a larger portion of the work associated with fulfilling the query.
265
254
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.
274
259
275
260
.. example::
276
261
@@ -345,7 +330,7 @@ Consider Performance when Creating Indexes for Write-heavy Applications
345
330
-----------------------------------------------------------------------
346
331
347
332
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
349
334
write-performance penalty. In general, don't be careless about adding
350
335
indexes. Indexes should be added to complement your queries. Always have
351
336
a good reason for adding a new index, and make sure you've benchmarked
@@ -357,8 +342,10 @@ Consider Insert Throughput
357
342
.. TODO insert link to /source/core/write-operations when that page is complete.
358
343
Do we want to link to write concern? -bg
359
344
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
362
349
collection adds some amount of overhead to these operations. In almost
363
350
every case, the performance gains that indexes realize for read
364
351
operations are worth the insertion penalty. However, in some cases:
0 commit comments