Skip to content

Commit 8d9f70d

Browse files
author
Dave
authored
DOCS-11790 equality sort range v5.3 (#908) (#948)
1 parent 859af13 commit 8d9f70d

File tree

6 files changed

+212
-21
lines changed

6 files changed

+212
-21
lines changed

source/applications/indexes.txt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,34 @@ index, and in addition, MongoDB can use an :doc:`intersection
3737

3838
The following documents introduce indexing strategies:
3939

40-
.. class:: toc
40+
:ref:`Use the ESR (Equality, Sort, Range) Rule <esr-indexing-rule>`
41+
The ESR (Equality, Sort, Range) Rule is a guide to creating indexes
42+
that support your queries efficiently.
4143

42-
:doc:`/tutorial/create-indexes-to-support-queries`
43-
An index supports a query when the index contains all the fields
44-
scanned by the query. Creating indexes that supports queries
45-
results in greatly increased query performance.
44+
:ref:`create-indexes-to-support-queries`
45+
An index supports a query when the index contains all the fields
46+
scanned by the query. Creating indexes that support queries
47+
results in greatly increased query performance.
4648

47-
:doc:`/tutorial/sort-results-with-indexes`
48-
To support efficient queries, use the strategies here when you
49-
specify the sequential order and sort order of index fields.
49+
:ref:`sorting-with-indexes`
50+
To support efficient queries, use the strategies here when you
51+
specify the sequential order and sort order of index fields.
5052

51-
:doc:`/tutorial/ensure-indexes-fit-ram`
52-
When your index fits in RAM, the system can avoid reading the
53-
index from disk and you get the fastest processing.
53+
:ref:`indexes-ensure-indexes-fit-ram`
54+
When your index fits in RAM, the system can avoid reading the
55+
index from disk and you get the fastest processing.
5456

55-
:doc:`/tutorial/create-queries-that-ensure-selectivity`
56-
Selectivity is the ability of a query to narrow results using the
57-
index. Selectivity allows MongoDB to use the index for a larger
58-
portion of the work associated with fulfilling the query.
57+
:ref:`index-selectivity`
58+
Selectivity is the ability of a query to narrow results using the
59+
index. Selectivity allows MongoDB to use the index for a larger
60+
portion of the work associated with fulfilling the query.
5961

6062

6163
.. toctree::
6264
:titlesonly:
6365
:hidden:
6466

67+
/tutorial/equality-sort-range-rule
6568
/tutorial/create-indexes-to-support-queries
6669
/tutorial/sort-results-with-indexes
6770
/tutorial/ensure-indexes-fit-ram

source/core/index-compound.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ operation that resembles the following prototype:
3939

4040
.. include:: /includes/fact-index-specification-field-value.rst
4141

42-
.. important:: You may not create compound indexes that have
43-
``hashed`` index type. You will receive an error if you attempt to
44-
create a compound index that includes :doc:`a hashed index
45-
field </core/index-hashed>`.
42+
.. important::
43+
44+
In MongoDB 4.2 or earlier:
45+
46+
- Compound indexes may **not** contain a :doc:`hashed index field
47+
</core/index-hashed>`.
48+
- You will receive an error if you attempt to create a compound index
49+
that contains a :doc:`hashed index field </core/index-hashed>`.
4650

4751
Consider a collection named ``products`` that holds documents that
4852
resemble the following document:
@@ -86,6 +90,11 @@ For details, see :ref:`compound-index-prefix`.
8690
.. index:: index; sort order
8791
.. _index-ascending-and-descending:
8892

93+
The order of the indexed fields has a strong impact on the
94+
effectiveness of a particular index for a given query. For most
95+
compound indexes, following the :ref:`ESR (Equality, Sort, Range) rule
96+
<esr-indexing-rule>` helps to create efficient indexes.
97+
8998
Sort Order
9099
----------
91100

source/indexes.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,11 @@ index can support a sort operation. See
141141
:ref:`index-ascending-and-descending` for more information on the
142142
impact of index order on results in compound indexes.
143143

144-
See :doc:`/core/index-compound` and :ref:`sort-on-multiple-fields` for
145-
more information on compound indexes.
144+
See also:
145+
146+
- :ref:`index-type-compound`,
147+
- :ref:`sort-on-multiple-fields`, and
148+
- :ref:`esr-indexing-rule`
146149

147150
Multikey Index
148151
~~~~~~~~~~~~~~

source/reference/method/cursor.hint.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _cursor-hint:
2+
13
=============
24
cursor.hint()
35
=============
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
.. _esr-indexing-rule:
2+
3+
====================================
4+
The ESR (Equality, Sort, Range) Rule
5+
====================================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
16+
An index that references multiple fields is a :ref:`compound index
17+
<index-type-compound>`. Compound indexes can dramatically improve query
18+
response times.
19+
20+
Index keys correspond to document fields. In most cases, applying the
21+
ESR (Equality, Sort, Range) Rule to arrange the index keys helps to
22+
create a more efficient :ref:`compound index <index-type-compound>`.
23+
24+
This page introduces the ESR Rule. For more information on tuning
25+
queries, see :dbcommand:`explain` and
26+
:ref:`query-plans-query-optimization`.
27+
28+
.. tip::
29+
30+
To force MongoDB to use a particular index, use :ref:`cursor-hint`
31+
when testing indexes.
32+
33+
.. _esr-rule-equality:
34+
35+
Equality
36+
--------
37+
38+
"Equality" refers to an exact match on a single value. The following
39+
exact match queries scan the ``cars`` collection for documents whose
40+
``model`` field exactly matches ``Cordoba``.
41+
42+
.. code-block:: javascript
43+
44+
db.cars.find( { model: "Cordoba" } )
45+
db.cars.find( { model: { $eq: "Cordoba" } } )
46+
47+
Index searches make efficient use of exact matches to limit the number
48+
of documents that need to be examined to satisfy a query. Place fields
49+
that require exact matches first in your index.
50+
51+
An index may have multiple keys for queries with exact matches. The
52+
index keys for equality matches can appear in any order. However, to
53+
satisfy an equality match with the index, all of the index keys for
54+
exact matches must come before any other index fields. MongoDB's search
55+
algorithm eliminates any need to arrange the exact match fields in a
56+
particular order.
57+
58+
Exact matches should be selective. To reduce the number of index keys
59+
scanned, ensure equality tests eliminate at least 90% of possible
60+
document matches.
61+
62+
.. _esr-rule-sort:
63+
64+
Sort
65+
----
66+
67+
"Sort" determines the order for results. Sort follows equality matches
68+
because the equality matches reduce the number of documents that need
69+
to be sorted. Sorting after the equality matches also allows MongoDB to
70+
do a non-blocking sort.
71+
72+
An index can support sort operations when the query fields are a subset
73+
of the index keys. Sort operations on a subset of the index keys are
74+
only supported if the query includes equality conditions for all of the
75+
prefix keys that precede the sort keys. For more information see:
76+
:ref:`Sort and Non-prefix Subset of an Index
77+
<sort-index-nonprefix-subset>`.
78+
79+
The following example queries the ``cars`` collection. The output is
80+
sorted by ``model``:
81+
82+
.. code-block:: javascript
83+
84+
db.cars.find( { manufacturer: "GM" } ).sort( { model: 1 } )
85+
86+
To improve query performance, create an index on the ``manufacturer``
87+
and ``model`` fields:
88+
89+
.. code-block:: javascript
90+
91+
db.cars.createIndex( { manufacturer: 1, model: 1 } )
92+
93+
- ``manufacturer`` is the first key because it is an equality match.
94+
- ``model`` is indexed in the same order ( ``1`` ) as the query.
95+
96+
.. _esr-rule-range:
97+
98+
Range
99+
-----
100+
101+
"Range" filters scan fields. The scan doesn't require an exact match,
102+
which means range filters are loosely bound to index keys. To improve
103+
query efficiency, make the range bounds as tight as possible and use
104+
equality matches to limit the number of documents that must be scanned.
105+
106+
Range filters resemble the following:
107+
108+
.. code-block:: javascript
109+
110+
db.cars.find( { price: { $gte: 15000} } )
111+
db.cars.find( { age: { $lt: 10 } } )
112+
db.cars.find( { priorAccidents: { $ne: null } } )
113+
114+
MongoDB cannot do an index sort on the results of a range filter.
115+
Place the range filter after the sort predicate so MongoDB can use a
116+
non-blocking index sort. For more information on blocking sorts, see
117+
:method:`cursor.allowDiskUse()`.
118+
119+
Additional Considerations
120+
-------------------------
121+
122+
Inequality operators such as :query:`$ne` or :query:`$nin` are range
123+
operators, not equality operators.
124+
125+
:query:`$regex` is a range operator.
126+
127+
:query:`$in` can be an equality operator or a range operator.
128+
When :query:`$in` is used alone, it is an equality operator that
129+
does a series of equality matches. :query:`$in` acts like a range
130+
operator when it is used with ``.sort()``.
131+
132+
Example
133+
-------
134+
135+
The following query searches the ``cars`` collection for vehicles
136+
manufactured by Ford that cost more than $15,000 dollars. The results
137+
are sorted by model:
138+
139+
.. code-block:: javascript
140+
141+
db.cars.find(
142+
{
143+
manufacturer: 'Ford',
144+
cost: { $gt: 10000 }
145+
} ).sort( { model: 1 } )
146+
147+
148+
The query contains all the elements of the ESR Rule:
149+
150+
- ``manufacturer: 'Ford'`` is an equality based match
151+
- ``cost: { $gt: 10000 }`` is a range based match, and
152+
- ``model`` is used for sorting
153+
154+
Following the ESR rule, the optimal index for the example query is:
155+
156+
.. code-block:: javascript
157+
158+
{ manufacturer: 1, model: 1, cost: 1 }
159+
160+
Further Discussion
161+
------------------
162+
163+
A number of MongoDB conference presentations discuss the ESR rule in
164+
depth.
165+
166+
- `Tips and Tricks for Effective Indexing
167+
<https://www.slideshare.net/mongodb/mongodb-local-toronto-2019-tips-and-tricks-for-effective-indexing>`__
168+
- `The Sights (and Smells) of a Bad Query
169+
<https://www.slideshare.net/mongodb/mongodb-world-2019-the-sights-and-smells-of-a-bad-query>`__
170+
- `Tips and Tricks++ for Querying and Indexing MongoDB
171+
<https://www.slideshare.net/mongodb/mongodb-world-2019-tips-and-tricks-for-querying-and-indexing-mongodb>`__
172+

source/tutorial/sort-results-with-indexes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ In such cases, MongoDB can use the index to retrieve the documents in
144144
order specified by the sort. As the example shows, the index prefix in
145145
the query predicate can be different from the prefix in the sort.
146146

147+
.. _sort-index-nonprefix-subset:
148+
147149
Sort and Non-prefix Subset of an Index
148150
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
149151

0 commit comments

Comments
 (0)