Skip to content

Commit 6f9661d

Browse files
committed
DOCS-254 incorporate comments from aaron and ed
1 parent ee6ace8 commit 6f9661d

File tree

4 files changed

+168
-67
lines changed

4 files changed

+168
-67
lines changed

source/applications/indexes.txt

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ understanding of:
2929

3030
The best overall strategy for designing indexes is to profile a variety
3131
of index configurations with data sets similar to the ones you'll be
32-
running in production and to see which configurations perform best.
32+
running in production to see which configurations perform best.
3333

3434
MongoDB can only use *one* index to support any given
3535
operation. However, each clause of an :operator:`$or` query can use
@@ -41,14 +41,14 @@ Create Indexes to Support Your Queries
4141
--------------------------------------
4242

4343
If you only ever query on a single key in a given collection, then you need
44-
create just one single-key index for that collection. For example, you
44+
to create just one single-key index for that collection. For example, you
4545
might create an index on ``category`` in the ``product`` collection:
4646

4747
.. code-block:: javascript
4848

4949
db.products.ensureIndex( { "category": 1 } )
5050

51-
However, if you sometimes query on only one key but and at other times
51+
However, if you sometimes query on only one key 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
@@ -122,9 +122,9 @@ part of an index. They are "covered queries" because an index "covers" the query
122122
MongoDB can fulfill the query by using *only* the index. MongoDB need
123123
not scan documents from the database.
124124

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

129129
Mongod automatically uses a covered query when possible. To ensure use
130130
of a covered query, create an index that includes all the fields listed
@@ -135,7 +135,7 @@ explicitly exclude the ``_id`` field from the result set, unless the
135135
index includes ``_id``.
136136

137137
MongoDB cannot use a covered query if any of the indexed fields in any
138-
of the documents in the collection include an array. If an indexed field
138+
of the documents in the collection includes an array. If an indexed field
139139
is an array, the index becomes a :ref:`multi-key index
140140
<index-type-multikey>` index and cannot support a covered query.
141141

@@ -197,10 +197,10 @@ are equality matches.
197197

198198
.. note::
199199

200-
When the :method:`sort() <cursor.sort()>` method performs an
201-
in-memory sort operation without the use of an index, the operation
202-
is significantly slower than for operations that use an index, and
203-
the operation aborts when it consumes 32 megabytes of memory.
200+
For in-memory sorts that do not use an index, the :method:`sort()
201+
<cursor.sort()>` operation is significantly slower. The
202+
:method:`~cursor.sort()` operation will abort when it uses 32
203+
megabytes of memory.
204204

205205
.. _indexes-ensure-indexes-fit-ram:
206206

@@ -226,7 +226,7 @@ available but also must have RAM available for the rest of the
226226

227227
If you have and use multiple collections, you must consider the size
228228
of all indexes on all collections. The indexes and the working set must be able to
229-
fit RAM at the same time.
229+
fit in RAM at the same time.
230230

231231
There are some limited cases where indexes do not need
232232
to fit in RAM. See :ref:`indexing-right-handed`.
@@ -337,8 +337,8 @@ Consider Performance when Creating Indexes for Write-heavy Applications
337337
If your application is write-heavy, then be careful when creating new
338338
indexes, since each additional index with impose a
339339
write-performance penalty. In general, don't be careless about adding
340-
indexes. Add indexes complement your queries. Always have
341-
a good reason for adding a new index, and make sure you've benchmarked
340+
indexes. Add indexes to complement your queries. Always have
341+
a good reason for adding a new index, and be sure to benchmark
342342
alternative strategies.
343343

344344
Consider Insert Throughput
@@ -347,17 +347,16 @@ Consider Insert Throughput
347347
.. todo:: insert link to /source/core/write-operations when that page is complete.
348348
Do we want to link to write concern? -bg
349349

350-
MongoDB must update *all* indexes associated with a collection after every
351-
insert, update, or delete operation. For update operations, if the updated document
352-
does not move to a new location, then only the modified, MongoDB only needs
353-
to update the updated fields in the index.
354-
Therefore, every index on a
355-
collection adds some amount of overhead to these write operations. In almost
356-
every case, the performance gains that indexes realize for read
350+
MongoDB must update *all* indexes associated with a collection after
351+
every insert, update, or delete operation. For update operations, if
352+
the updated document does not move to a new location, then MongoDB only
353+
modifies the updated fields in the index. Therefore, every index on a
354+
collection adds some amount of overhead to these write operations. In
355+
almost every case, the performance gains that indexes realize for read
357356
operations are worth the insertion penalty. However, in some cases:
358357

359358
- An index to support an infrequent query might incur more
360-
insert-related costs than saved read-time.
359+
insert-related costs than savings in read-time.
361360

362361
.. todo:: How do you determine if the above is the case?
363362
Empirically.
@@ -371,5 +370,5 @@ operations are worth the insertion penalty. However, in some cases:
371370

372371
- If your indexes and queries are not sufficiently :ref:`selective
373372
<index-selectivity>`, the speed improvements for query operations
374-
might not offset the costs of maintaining an index. For more
373+
may not offset the costs of maintaining an index. For more
375374
information see :ref:`index-selectivity`.

source/reference/explain-output.txt

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Explain Output
55
.. default-domain:: mongodb
66

77
This document explains the output of the :operator:`$explain` operator
8-
and the :program:`mongo` shell wrapper :method:`explain()
8+
and the :program:`mongo` shell method :method:`explain()
99
<cursor.explain()>`.
1010

1111
Core Explain Output
@@ -47,6 +47,36 @@ this information is displayed for each accessed shard.
4747
"server" : "<host:port>",
4848
}
4949

50+
$OR Queries
51+
-----------
52+
53+
Queries with :operator:`$or` operator execute each clause of the
54+
:operator:`$or` expression in parallel and can use separate indexes on
55+
the individual clauses. If the indexes on the clauses are used, the
56+
explain output contains the :ref:`explain-output-fields-core` for each
57+
clause as well as the cumulative information for the query:
58+
59+
.. code-block:: javascript
60+
61+
{
62+
"clauses" : [
63+
{
64+
<core explain output>
65+
},
66+
{
67+
<core explain output>
68+
},
69+
...
70+
],
71+
"n" : <num>,
72+
"nscannedObjects" : <num>,
73+
"nscanned" : <num>,
74+
"nscannedObjectsAllPlans" : <num>,
75+
"nscannedAllPlans" : <num>,
76+
"millis" : <num>,
77+
"server" : "<host:port>"
78+
}
79+
5080
Sharded Collections
5181
-------------------
5282

@@ -100,13 +130,15 @@ Core Explain Output
100130

101131
Specifies the type of cursor used in the query operation:
102132

103-
- ``BasicCursor`` indicates use of full table scan.
133+
- ``BasicCursor`` indicates use of full collection scan.
104134

105-
- ``BtreeCursor`` indicates use of an index. The cursor information
106-
includes the index name. With the use of an index, the
135+
- ``BtreeCursor`` indicates use of an B-tree index. The cursor
136+
information includes the index name. With the use of an index, the
107137
:method:`explain() <cursor.explain()>` output will include the
108138
:data:`indexBounds` details.
109139

140+
- ``GeoSearchCursor`` indicates use of a geospatial index.
141+
110142
.. data:: isMultiKey
111143

112144
Specifies whether a :ref:`multikey index on an array field
@@ -119,17 +151,18 @@ Core Explain Output
119151

120152
.. data:: nscannedObjects
121153

122-
Specifies the total number of documents scanned during the query. If
123-
the index is a covered index, the :data:`nscannedObjects` may be
124-
lower than :data:`nscanned`.
154+
Specifies the total number of documents scanned during the query.
155+
The :data:`nscannedObjects` may be lower than :data:`nscanned`, such
156+
as if the index is a covered index.
125157

126158
.. data:: nscanned
127159

128160
Specifies the total number of documents or index entries scanned
129161
during the database operation. You want :data:`n` and
130-
:data:`nscanned` to be close in value as possible. If the index is a
131-
covered index, :data:`nscanned` may be higher than
132-
:data:`nscannedObjects`.
162+
:data:`nscanned` to be close in value as possible. The
163+
:data:`nscanned` value may be higher than the
164+
:data:`nscannedObjects` value, such as if the index is a covered
165+
index.
133166

134167
.. data:: nscannedObjectsAllPlans
135168

@@ -149,8 +182,12 @@ Core Explain Output
149182

150183
.. versionadded:: 2.2
151184

152-
Specifies whether the index could not be used for sorting. If
153-
``true``, the index could *not* be used for sorting.
185+
Specifies whether the index could not be used for sorting.
186+
187+
If ``true``, the index could *not* be used for sorting. Instead, the
188+
documents are sorted after being retrieved either from the index if
189+
an index is used to return the documents, or from the full
190+
collection if a collection scan is performed.
154191

155192
.. data:: indexOnly
156193

@@ -211,6 +248,18 @@ Core Explain Output
211248

212249
Specifies the MongoDB server.
213250

251+
.. _explain-output-field-or-clauses:
252+
253+
$OR Query Output
254+
~~~~~~~~~~~~~~~~
255+
256+
.. data:: clauses
257+
258+
Contains the :ref:`explain-output-fields-core` information for each
259+
clause of the :operator:`$or` expression. Displays only if indexes
260+
are used for the clauses in the :operator:`$or` expression.
261+
262+
214263
.. _explain-output-fields-sharded-collection:
215264

216265
Sharded Collections Output

source/reference/method/cursor.explain.txt

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ cursor.explain()
44

55
.. default-domain:: mongodb
66

7+
.. EDITS to cursor.explain.txt must be carried over to the operator
8+
explain.txt and vice versa
9+
710
.. method:: cursor.explain()
811

912
The :method:`cursor.explain()` method provides information on the
10-
query plan.
13+
query plan. The query plan is the plan the server uses to find the
14+
matches for a query. This information may be useful when optimizing
15+
a query.
1116

1217
:param boolean verbose:
1318

@@ -17,7 +22,7 @@ cursor.explain()
1722
</reference/explain-output>`.
1823

1924
:returns: A :doc:`document </reference/explain-output>` that
20-
describes the process used to return the query.
25+
describes the process used to return the query results.
2126

2227
Retrieve the query plan by appending :method:`explain()
2328
<cursor.explain()>` to a :method:`find()` query, as in the following
@@ -27,14 +32,21 @@ cursor.explain()
2732

2833
db.products.find().explain()
2934

35+
For details on the output, see :doc:`/reference/explain-output`.
36+
3037
:method:`explain <cursor.explain()>` runs the actual query to
31-
determine the result. If the query is slow, the :method:`explain
32-
<cursor.explain()>` will also be slow. Additionally, when you call
33-
the :method:`explain <cursor.explain()>` on a query, the query
34-
system reevaluates the query plan, which may cause some queries to
35-
run slower. As a result, these operations provide an accurate
36-
account of *how* MongoDB would perform the query, but not
37-
necessarily *how long* the query would take.
38+
determine the result. Although there are some differences between
39+
running the query with :method:`explain <cursor.explain()>` and
40+
running without, generally, the performance will be similar between
41+
the two. So, if the query is slow, the :method:`explain
42+
<cursor.explain()>` will also be slow.
43+
44+
Additionally, when you call the :method:`explain <cursor.explain()>`
45+
method on a query, the query system reevaluates a set of candidate
46+
query plans, which may cause some queries to run slower. As a
47+
result, these operations generally provide an accurate account of
48+
*how* MongoDB would perform the query [#explain-cache-plan]_, but
49+
not necessarily *how long* the query would take.
3850

3951
To determine the performance of a particular index, you can use
4052
:method:`hint() <cursor.hint()>` and in conjunction with
@@ -46,9 +58,19 @@ cursor.explain()
4658

4759
When you run :method:`explain <cursor.explain()>` with
4860
:method:`hint() <cursor.hint()>`, the query optimizer does not
49-
reevaluate the query plan.
50-
51-
For details on the output, see :doc:`/reference/explain-output`.
61+
reevaluate the query plans.
62+
63+
.. [#explain-cache-plan] In some situations, the :method:`explain
64+
<cursor.explain()>` operation may differ from the actual query
65+
plan used to perform the query. The :method:`explain
66+
<cursor.explain()>` operation evaluates the set of query plans
67+
and reports on the winning plan for the query. This plan is
68+
cached to be used for the execution of the query and similar
69+
queries at a later time. When you run similar queries, this
70+
cached plan is selected even though the cached plan may not be
71+
the best plan for similar queries. So, if you run
72+
:method:`explain <cursor.explain()>` operation on similar
73+
queries, the winning plan may be different.
5274

5375
.. seealso::
5476

0 commit comments

Comments
 (0)