@@ -52,14 +52,15 @@ However, if you sometimes query on only one key but and at other times
52
52
query on that key combined with a second key, then creating a
53
53
:ref:`compound index <index-type-compound>` is more efficient. MongoDB
54
54
will use the compound index for both queries. For example, you might
55
- create an index on both ``category`` and ``item``, allowing you both
56
- options: to query only on ``category`` and also to query on ``category``
57
- combined with ``item``:
55
+ create an index on both ``category`` and ``item``.
58
56
59
57
.. code-block:: javascript
60
58
61
59
db.products.ensureIndex( { "category": 1, "item": 1 } )
62
60
61
+ This allows you both options. You can query on just ``category``, and
62
+ you also can query on ``category`` combined with ``item``.
63
+
63
64
To query on multiple keys and sort the results, see :ref:`index-sort`.
64
65
65
66
With the exception of queries that use the :operator:`$or` operator, a
@@ -68,6 +69,50 @@ query cannot use multiple indexes. A query must use only one index.
68
69
.. _covered-queries:
69
70
.. _indexes-covered-queries:
70
71
72
+
73
+ Use Compound Indexes to Support Several Different Queries
74
+ ---------------------------------------------------------
75
+
76
+ A single :ref:`compound index <index-type-compound>` on multiple fields
77
+ can support all the queries that search a "prefix" subset of those fields.
78
+
79
+ .. example::
80
+
81
+ The following index on a collection:
82
+
83
+ .. code-block:: javascript
84
+
85
+ { x: 1, y: 1, z: 1 }
86
+
87
+ Can support queries that the following indexes support:
88
+
89
+ .. code-block:: javascript
90
+
91
+ { x: 1 }
92
+ { x: 1, y: 1 }
93
+
94
+ There are some situations where the prefix indexes may offer better
95
+ query performance: for example if ``z`` is a large array.
96
+
97
+ The ``{ x: 1, y: 1, z: 1 }`` index can also support many of the same
98
+ queries as the following index:
99
+
100
+ .. code-block:: javascript
101
+
102
+ { x: 1, z: 1 }
103
+
104
+ Also, ``{ x: 1, z: 1 }`` has an additional use. Given the following
105
+ query:
106
+
107
+ .. code-block:: javascript
108
+
109
+ db.collection.find( { x: 5 } ).sort( { z: 1} )
110
+
111
+ The ``{ x: 1, z: 1 }`` index supports both the query and the sort
112
+ operation, while the ``{ x: 1, y: 1, z: 1 }`` index only supports
113
+ the query. For more information on sorting, see
114
+ :ref:`sorting-with-indexes`.
115
+
71
116
Create Indexes that Support Covered Queries
72
117
-------------------------------------------
73
118
@@ -183,28 +228,19 @@ available but also must have RAM available for the rest of the
183
228
:term:`working set`. Also remember:
184
229
185
230
- If you have and use multiple collections, you must consider the size
186
- of all indexes on all collections.
231
+ of all indexes on all collections. The indexes and the working set must be able to
232
+ fit RAM at the same time.
187
233
188
- - There are some limited cases where indexes do not need to fit in RAM.
189
- See :ref:`indexing-right-handed`.
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`.
190
239
191
240
.. seealso:: For additional :doc:`collection statistics
192
241
</reference/collection-statistics>`, use :dbcommand:`collStats` or
193
242
:method:`db.collection.stats()`.
194
243
195
- Indexes require space, both on disk and in RAM. Indexes require less
196
- space in RAM than the full documents in the collection. In theory, if
197
- your queries only match a subset of the documents and can use the index
198
- to locate those documents, MongoDB can maintain a much smaller
199
- :term:`working set`. Ensure that:
200
-
201
- - The indexes and the working set can fit RAM at the same time.
202
-
203
- - All of your indexes use less space than all of the documents in the
204
- collection. This may not be an issue all of your queries use
205
- :ref:`covered queries <covered-queries>` or indexes do not need to fit
206
- into ram, as in the following situation:
207
-
208
244
.. _indexing-right-handed:
209
245
210
246
Indexes that Hold Only Recent Values in RAM
@@ -218,17 +254,14 @@ RAM. This allows for efficient index use for read and write
218
254
operations and minimize the amount of RAM required to support the
219
255
index.
220
256
221
- .. To determine the size of the index, see DOCS-224
222
-
223
257
.. _index-selectivity:
224
258
225
259
Create Queries that Ensure Selectivity
226
260
--------------------------------------
227
261
228
- Selectivity is the ability of a query to narrow results
229
- using the index. Effective indexes are more selective and allow
230
- MongoDB to use the index for a larger portion of the work associated
231
- with fulfilling the query.
262
+ Selectivity is the ability of a query to narrow results using the index.
263
+ Effective indexes are more selective and allow MongoDB to use the index
264
+ for a larger portion of the work associated with fulfilling the query.
232
265
233
266
To ensure selectivity:
234
267
@@ -322,70 +355,27 @@ Consider Insert Throughput
322
355
~~~~~~~~~~~~~~~~~~~~~~~~~~
323
356
324
357
.. TODO insert link to /source/core/write-operations when that page is complete.
358
+ Do we want to link to write concern? -bg
325
359
326
- MongoDB must update all indexes associated with a collection after
327
- every insert, update, or delete operation.
328
-
329
- Every index on a collection adds some amount of overhead to these
330
- operations. In almost every case, the performance gains that indexes
331
- realize for read operations are worth the insertion penalty; however:
332
-
333
- - In some cases, an index to support an infrequent query may incur
334
- more insert-related costs than saved read-time.
335
-
336
- - In some situations, if you have many indexes on a collection with a
337
- high insert throughput and a number of very similar indexes, you may
338
- find better overall results by using a slightly less effective index
339
- on some queries if it means consolidating the total number of
340
- indexes.
341
-
342
- - If your indexes and queries are not very selective, the speed
343
- improvements for query operations may not offset the costs of
344
- maintaining an index. See the section on :ref:`index selectivity
345
- <index-selectivity>` for more information.
346
-
347
- - In some cases a single compound on two or more fields index may
348
- support all of the queries that index on a single field index, or a
349
- smaller :ref:`compound index <index-type-compound>`. In general,
350
- MongoDB can use compound index
351
- to support the same queries as any of its prefixes. Consider the
352
- following example:
353
-
354
- .. example::
355
-
356
- Given the following index on a collection:
357
-
358
- .. code-block:: javascript
359
-
360
- { x: 1, y: 1, z: 1 }
361
-
362
- Can support a number of queries as well as most of the queries
363
- that the following indexes support:
364
-
365
- .. code-block:: javascript
366
-
367
- { x: 1 }
368
- { x: 1, y: 1 }
369
-
370
- There are some situations where the prefix indexes may offer
371
- better query performance as is the case if ``z`` is a large
372
- array. Also, consider the following index on the same collection:
373
-
374
- .. code-block:: javascript
375
-
376
- { x: 1, z: 1 }
360
+ MongoDB must update all indexes associated with a collection after every
361
+ insert, update, or delete operation. Therefore, every index on a
362
+ collection adds some amount of overhead to these operations. In almost
363
+ every case, the performance gains that indexes realize for read
364
+ operations are worth the insertion penalty. However, in some cases:
377
365
378
- The ``{ x: 1, y: 1, z: 1 }`` index can support many of the same
379
- queries as the above index; however, ``{ x: 1, z: 1 }`` has
380
- additional use: Given the following query:
366
+ - An index to support an infrequent query might incur more
367
+ insert-related costs than saved read-time.
381
368
382
- .. code-block:: javascript
369
+ .. TODO How do you determine if the above is the case?
383
370
384
- db.collection.find( { x: 5 } ).sort( { z: 1} )
371
+ - If you have many indexes on a collection with a high insert throughput
372
+ and a number of very similar indexes, you may find better overall
373
+ results by using a slightly less effective index on some queries if it
374
+ means consolidating the total number of indexes.
385
375
386
- The ``{ x: 1, z: 1 }`` will support both the query and the sort
387
- operation, while the ``{ x: 1, y: 1, z: 1 }`` index can only
388
- support the query.
376
+ .. TODO The above is unclear. -bg
389
377
390
- See the :ref:`sorting-with-indexes` section for more
391
- information.
378
+ - If your indexes and queries are not very :ref:`selective
379
+ <index-selectivity>`, the speed improvements for query operations
380
+ might not offset the costs of maintaining an index. For more
381
+ information see :ref:`index-selectivity`.
0 commit comments