Skip to content

Commit 38f00eb

Browse files
committed
DOCS-2552 index intersection
1 parent f2b3b14 commit 38f00eb

14 files changed

+242
-40
lines changed

config/htaccess-next.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,14 @@ redirect-path: '/reference/command/planCacheListFilters'
847847
url-base: '/reference/command'
848848
type: 'redirect'
849849
code: 303
850+
outputs:
851+
- 'manual'
852+
- 'before-v2.4'
853+
---
854+
redirect-path: '/core/index-intersection'
855+
url-base: '/core/indexes'
856+
type: 'redirect'
857+
code: 303
850858
outputs:
851859
- 'manual'
852860
- 'before-v2.4'

source/core/index-compound.txt

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ Compound indexes can support queries that match on multiple fields.
3030
.. code-block:: javascript
3131

3232
{
33-
"_id": ObjectId(...)
34-
"item": "Banana"
35-
"category": ["food", "produce", "grocery"]
36-
"location": "4th Street Store"
37-
"stock": 4
38-
"type": cases
33+
"_id": ObjectId(...),
34+
"item": "Banana",
35+
"category": ["food", "produce", "grocery"],
36+
"location": "4th Street Store",
37+
"stock": 4,
38+
"type": "cases",
3939
"arrival": Date(...)
4040
}
4141

@@ -97,7 +97,7 @@ The following index can support both these sort operations:
9797

9898
db.events.ensureIndex( { "username" : 1, "date" : -1 } )
9999

100-
However, the above index cannot support sorting by ascending
100+
However, the above index **cannot** support sorting by ascending
101101
``username`` values and then by ascending ``date`` values, such as the
102102
following:
103103

@@ -122,26 +122,29 @@ constraints, then you can drop the ``{ a: 1 }`` index. MongoDB will be
122122
able to use the compound index in all of situations that it would have
123123
used the ``{ a: 1 }`` index.
124124

125-
.. example::
125+
For example, given the following index:
126+
127+
.. code-block:: javascript
126128

127-
Given the following index:
129+
{ "item": 1, "location": 1, "stock": 1 }
128130

129-
.. code-block:: javascript
131+
MongoDB **can** use this index to support queries that include:
130132

131-
{ "item": 1, "location": 1, "stock": 1 }
133+
- the ``item`` field,
134+
- the ``item`` field *and* the ``location`` field,
135+
- the ``item`` field *and* the ``location`` field *and* the
136+
``stock`` field, or
137+
- only the ``item`` *and* ``stock`` fields; however, this index
138+
would be less efficient than an index on only ``item`` and
139+
``stock``.
132140

133-
MongoDB **can** use this index to support queries that include:
141+
MongoDB **cannot** use this index to support queries that include:
134142

135-
- the ``item`` field,
136-
- the ``item`` field *and* the ``location`` field,
137-
- the ``item`` field *and* the ``location`` field *and* the
138-
``stock`` field, or
139-
- only the ``item`` *and* ``stock`` fields; however, this index
140-
would be less efficient than an index on only ``item`` and
141-
``stock``.
143+
- only the ``location`` field,
144+
- only the ``stock`` field, or
145+
- only the ``location`` *and* ``stock`` fields.
142146

143-
MongoDB **cannot** use this index to support queries that include:
147+
Index Intersection
148+
------------------
144149

145-
- only the ``location`` field,
146-
- only the ``stock`` field, or
147-
- only the ``location`` *and* ``stock`` fields.
150+
.. include:: /includes/fact-index-intersection-vs-compound-indexes.rst

source/core/index-intersection.txt

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
==================
2+
Index Intersection
3+
==================
4+
5+
.. default-domain:: mongodb
6+
7+
.. versionadded:: 2.5.5
8+
9+
MongoDB can use the intersection of multiple indexes to fulfill
10+
queries. [#previous-versions]_ In general, each index intersection
11+
involves two indexes; however, MongoDB can employ multiple/nested index
12+
intersections to resolve a query.
13+
14+
To illustrate index intersection, consider a collection ``orders`` that
15+
has the following indexes:
16+
17+
.. code-block:: javascript
18+
19+
{ qty: 1 }
20+
{ item: 1 }
21+
22+
Then, MongoDB can use the intersection of the two indexes to support
23+
the following query:
24+
25+
.. code-block:: javascript
26+
27+
db.orders.find( { item: "abc123", qty: { $gt: 15 } } )
28+
29+
For query plans that use index intersection, the
30+
:method:`~cursor.explain()` returns the value ``Complex Plan`` in the
31+
``cursor`` field.
32+
33+
.. [#previous-versions] In previous versions, MongoDB could use only a
34+
single index to fulfill most queries, or for queries with
35+
:query:`$or` clauses, previous versions of MongoDB could use a
36+
single index for each :query:`$or` clause.
37+
38+
Index Prefix Intersection
39+
~~~~~~~~~~~~~~~~~~~~~~~~~
40+
41+
With index intersection, MongoDB can use an intersection of either the
42+
entire index or the index prefix. An index prefix is a subset of a
43+
compound index, consisting of one or more keys starting from the
44+
beginning of the index.
45+
46+
Consider a collection ``orders`` with the following indexes:
47+
48+
.. code-block:: javascript
49+
50+
{ qty: 1 }
51+
{ status: 1, ord_date: -1 }
52+
53+
To fulfill the following query which specifies a condition on both the
54+
``qty`` field and the ``status`` field, MongoDB can use the
55+
intersection of the two indexes:
56+
57+
.. code-block:: javascript
58+
59+
db.orders.find( { qty: { $gt: 10 } , status: "A" } )
60+
61+
.. _index-intersection-compound-indexes:
62+
63+
Index Intersection and Compound Indexes
64+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65+
66+
Index intersection does not eliminate the need for creating
67+
:doc:`compound indexes </core/index-compound>`. However, because both
68+
the list order (i.e. the order in which the keys are listed in the
69+
index) and the sort order (i.e. ascending or descending), matter in
70+
:doc:`compound indexes </core/index-compound>`, a compound index may
71+
not support query condition that does not include the :ref:`index
72+
prefix keys <compound-index-prefix>` or that specifies a different sort
73+
order.
74+
75+
For example, if a collection ``orders`` has the following compound
76+
index, with the ``status`` field listed before the ``ord_date`` field:
77+
78+
.. code-block:: javascript
79+
80+
{ status: 1, ord_date: -1 }
81+
82+
The compound index can support the following queries:
83+
84+
.. code-block:: javascript
85+
86+
db.orders.find( { status: { $in: ["A", "P" ] } } )
87+
db.orders.find(
88+
{
89+
ord_date: { $gt: new Date("2014-02-01") },
90+
status: {$in:[ "P", "A" ] }
91+
}
92+
)
93+
94+
But not the following two queries:
95+
96+
.. code-block:: javascript
97+
98+
db.orders.find( { ord_date: { $gt: new Date("2014-02-01") } } )
99+
db.orders.find( { } ).sort( { ord_date: 1 } )
100+
101+
However, if the collection has two separate indexes:
102+
103+
.. code-block:: javascript
104+
105+
{ status: 1 }
106+
{ ord_date: -1 }
107+
108+
The two indexes can, either individually or through index intersection,
109+
support all four aforementioned queries.
110+
111+
The choice between creating compound indexes that support your queries
112+
or relying on index intersection depends on the specifics of your
113+
system.
114+
115+
.. seealso:: :doc:`compound indexes </core/index-compound>`,
116+
:ref:`compound-key-indexes`
117+
118+
Index Intersection and Sort
119+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
120+
121+
Index intersection does not apply when the :method:`~cursor.sort()`
122+
operation requires an index completely separate from the query
123+
predicate.
124+
125+
For example, the ``orders`` collection has the following indexes:
126+
127+
.. code-block:: javascript
128+
129+
{ qty: 1 }
130+
{ status: 1, ord_date: -1 }
131+
{ status: 1 }
132+
{ ord_date: -1 }
133+
134+
MongoDB cannot use index intersection for the following query with sort:
135+
136+
.. code-block:: javascript
137+
138+
db.orders.find( { qty: { $gt: 10 } } ).sort( { status: 1 } )
139+
140+
That is, MongoDB does not use the ``{ qty: 1 }`` index for the query,
141+
and the separate ``{ status: 1 }`` or the ``{ status: 1, ord_date: -1
142+
}`` index for the sort.
143+
144+
However, MongoDB can use index intersection for the following query
145+
with sort since the index ``{ status: 1, ord_date: -1 }`` can fulfill
146+
part of the query predicate.
147+
148+
.. code-block:: javascript
149+
150+
db.orders.find( { qty: { $gt: 10 } , status: "A" } ).sort( { ord_date: -1 } )

source/core/index-types.txt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ In the :program:`mongo` shell, you can create an index by calling the
2626
more detailed instructions about building indexes, see the
2727
:doc:`Indexing Tutorials </administration/indexes>` page.
2828

29-
Behavior of Index Types
30-
-----------------------
29+
Behavior of Indexes
30+
-------------------
3131

3232
All indexes in MongoDB are :term:`B-tree` indexes, which can
3333
efficiently support equality matches and range queries. The index
@@ -50,14 +50,18 @@ greater impact on the results.
5050
See :ref:`index-ascending-and-descending` for more information on the
5151
impact of index order on results in compound indexes.
5252

53-
Redundant Indexes
54-
~~~~~~~~~~~~~~~~~
53+
Index Intersection
54+
~~~~~~~~~~~~~~~~~~
5555

56-
A single query can only use *one* index, except for queries that use
57-
the :query:`$or` operator that can use a different index for each
58-
clause.
56+
MongoDB can use the intersection of indexes to fulfill queries with
57+
compound conditions. See :doc:`/core/index-intersection` for details.
5958

60-
.. seealso:: :ref:`Index Limitations <index-limitations>`.
59+
Limits
60+
~~~~~~
61+
62+
Certain restrictions apply to indexes, such as the length of the index
63+
keys or the number of indexes per collection. See :ref:`Index
64+
Limitations <index-limitations>` for details.
6165

6266
.. _index-type-list:
6367

source/core/indexes-introduction.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,19 @@ field. The index skips documents that *do not* have the indexed field.
174174
You can combine the sparse index option with the unique index option
175175
to reject documents that have duplicate values for a field but ignore
176176
documents that do not have the indexed key.
177+
178+
Index Intersection
179+
------------------
180+
181+
.. versionadded:: 2.5.5
182+
183+
MongoDB can use the :doc:`intersection of indexes
184+
</core/index-intersection>` to fulfill queries. For queries that
185+
specify compound query conditions, if one index can fulfill a part of a
186+
query condition, and another index can fulfill another part of the
187+
query condition, then MongoDB can use the intersection of the two
188+
indexes to fulfill the query. Whether the use of a compound index or
189+
the use of an index intersection is more efficient depends on the
190+
particular query and the system.
191+
192+
For details on index intersection, see :doc:`/core/index-intersection`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Starting in version 2.5.5, MongoDB can use :doc:`index
2+
intersection</core/index-intersection>` to fulfill queries. The choice
3+
between creating compound indexes that support your queries or relying
4+
on index intersection depends on the specifics of your system. See
5+
:ref:`index-intersection-compound-indexes` for more details.

source/includes/toc-indexes-concepts-landing.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ description: |
1010
file: /core/index-creation
1111
description: |
1212
The options available when creating indexes.
13+
---
14+
file: /core/index-intersection
15+
description: |
16+
The use of index intersection to fulfill a query.
1317
...
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
file: /core/index-intersection
2+
description: |
3+
The use of index intersection to fulfill a query.
4+
...

source/includes/toc-spec-indexes-concepts-landing.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ sources:
22
- toc-indexes-concepts-landing.yaml
33
- toc-indexes-concepts-types.yaml
44
- toc-indexes-concepts-properties.yaml
5+
- toc-indexes-concepts-mechanics.yaml
56
files:
67
- file: /core/index-types
78
level: 1
@@ -27,3 +28,5 @@ files:
2728
level: 2
2829
- file: /core/index-creation
2930
level: 1
31+
- file: /core/index-intersection
32+
level: 1

source/includes/toc-spec-indexes-landing.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
sources:
22
- toc-indexes-landing.yaml
33
- toc-indexes-concepts-landing.yaml
4+
- toc-indexes-concepts-mechanics.yaml
45
files:
56
- /core/indexes-introduction
67
- /core/indexes
@@ -10,5 +11,7 @@ files:
1011
level: 2
1112
- file: /core/index-creation
1213
level: 2
14+
- file: /core/index-intersection
15+
level: 2
1316
- /administration/indexes
1417
- /reference/indexes

source/reference/limits.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,6 @@ Operations
323323

324324
A bulk operation can have at most 1000 operations.
325325

326-
.. _limit-multiple-in:
327-
.. limit:: Combination Limit with Multiple $in Expressions
328-
329-
.. include:: /includes/fact-limits-multiple-in-expressions.rst
330-
331326
Naming Restrictions
332327
-------------------
333328

source/reference/method/cursor.explain.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ sharded*. For queries on sharded collections, see
235235
- ``GeoSearchCursor`` indicates that the query used a geospatial
236236
index.
237237

238+
- ``Complex Plan`` indicates that MongoDB used :doc:`index
239+
intersection </core/index-intersection>`.
240+
238241
For ``BtreeCursor`` cursors, MongoDB will append the name of the
239242
index to the cursor string. Additionally, depending on how the
240243
query uses an index, MongoDB may append one or both of the

source/release-notes/2.6.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,12 @@ exposure </core/security-network>`. To enable the interface, see
647647
Index Improvements
648648
~~~~~~~~~~~~~~~~~~
649649

650+
Index Intersection
651+
``````````````````
652+
653+
MongoDB can now use the intersection of indexes to fulfill queries. See
654+
:doc:`/core/index-intersection` for details.
655+
650656
Background Index Builds Replicate to Secondaries
651657
````````````````````````````````````````````````
652658

0 commit comments

Comments
 (0)