Skip to content

Commit e31ea63

Browse files
author
Bob Grabar
committed
DOCS-206 new index faq page
1 parent 7d3801b commit e31ea63

File tree

2 files changed

+138
-89
lines changed

2 files changed

+138
-89
lines changed

source/applications/indexes.txt

Lines changed: 76 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ However, if you sometimes query on only one key but and at other times
5252
query on that key combined with a second key, then creating a
5353
:ref:`compound index <index-type-compound>` is more efficient. MongoDB
5454
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``.
5856

5957
.. code-block:: javascript
6058

6159
db.products.ensureIndex( { "category": 1, "item": 1 } )
6260

61+
This allows you both options. You can query on just ``category``, and
62+
you also can query on ``category`` combined with ``item``.
63+
6364
To query on multiple keys and sort the results, see :ref:`index-sort`.
6465

6566
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.
6869
.. _covered-queries:
6970
.. _indexes-covered-queries:
7071

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+
71116
Create Indexes that Support Covered Queries
72117
-------------------------------------------
73118

@@ -183,28 +228,19 @@ available but also must have RAM available for the rest of the
183228
:term:`working set`. Also remember:
184229

185230
- 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.
187233

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`.
190239

191240
.. seealso:: For additional :doc:`collection statistics
192241
</reference/collection-statistics>`, use :dbcommand:`collStats` or
193242
:method:`db.collection.stats()`.
194243

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-
208244
.. _indexing-right-handed:
209245

210246
Indexes that Hold Only Recent Values in RAM
@@ -218,17 +254,14 @@ RAM. This allows for efficient index use for read and write
218254
operations and minimize the amount of RAM required to support the
219255
index.
220256

221-
.. To determine the size of the index, see DOCS-224
222-
223257
.. _index-selectivity:
224258

225259
Create Queries that Ensure Selectivity
226260
--------------------------------------
227261

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.
232265

233266
To ensure selectivity:
234267

@@ -322,70 +355,27 @@ Consider Insert Throughput
322355
~~~~~~~~~~~~~~~~~~~~~~~~~~
323356

324357
.. TODO insert link to /source/core/write-operations when that page is complete.
358+
Do we want to link to write concern? -bg
325359

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:
377365

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.
381368

382-
.. code-block:: javascript
369+
.. TODO How do you determine if the above is the case?
383370

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.
385375

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
389377

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`.

source/faq/indexes.txt

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,68 @@ changes.
2525
While running :method:`ensureIndex() <db.collection.ensureIndex()>` is
2626
usually ok, if an index doesn't exist because of ongoing administrative
2727
work, a call to :method:`ensureIndex() <db.collection.ensureIndex()>`
28-
may disrupt database avalability. Runnning :method:`ensureIndex() <db.collection.ensureIndex()>`
29-
can render a replica set inaccessible as the index
30-
creation is happening. See :ref:`index-building-replica-sets`.
28+
may disrupt database avalability. Runnning :method:`ensureIndex()
29+
<db.collection.ensureIndex()>` can render a replica set inaccessible as
30+
the index creation is happening. See :ref:`index-building-replica-sets`.
31+
32+
How do you know what indexes exist in a collection?
33+
---------------------------------------------------
34+
35+
To list a collection's indexes, use the
36+
:method:`db.collection.getIndexes()` method or a similar
37+
:api:`method for your driver <>`.
38+
39+
How do you determine the size of an index?
40+
------------------------------------------
41+
42+
To check index size, use :method:`db.collection.totalIndexSize()`.
43+
44+
.. TODO FAQ How do I determine if an index fits into RAM?
45+
46+
What happens if an index does not fit into RAM?
47+
-----------------------------------------------
48+
49+
When an index is too large to fit into RAM, MongoDB must read the index
50+
from disk, which is a much slower operation than reading from RAM. Keep
51+
in mind an index fits into RAM when your server has RAM available for
52+
the index combined with the rest of the :term:`working set`.
53+
54+
In certain cases, an index does not need to fit *entirely* into RAM. For
55+
details, see :ref:`indexing-right-handed`.
56+
57+
.. TODO FAQ How does MongoDB determine what index to use?
58+
59+
How do you know what index a query used?
60+
----------------------------------------
61+
62+
To determine how a query is processed, use the :method:`explain()
63+
<cursor.explain()>` method.
64+
65+
How do you determine what fields to index?
66+
------------------------------------------
67+
68+
A number of factors determine what fields to index, including
69+
:ref:`selectivity <index-selectivity>`, fitting indexes into RAM,
70+
reusing indexes in multiple queries when possible, and creating indexes
71+
that can support all the fields in a given query. For detailed
72+
documentation on choosing which fields to index, see
73+
:doc:`/applications/indexes`.
74+
75+
.. TODO FAQ How do I guarantee a query uses an index?
76+
MongoDB's query optimizer always looks for the most advantageous
77+
index to use. You cannot guarantee use of a particular index, but you
78+
can write indexes with your queries in mind. For detailed
79+
documentation on creating optimal indexes, see
80+
:doc:`/applications/indexes`.
81+
82+
How do write operations affect indexes?
83+
---------------------------------------
84+
85+
While a read operation does not affect an index, every write operation
86+
does involve a write to the index. If your application is write-heavy,
87+
creating too many indexes might affect performance.
88+
89+
.. TODO More is needed on that last FAQ.
3190

3291
Will building a large index affect database performance?
3392
--------------------------------------------------------

0 commit comments

Comments
 (0)