Skip to content

Commit 2492c2c

Browse files
author
Dave
authored
DOCS-11790 equality sort range v5.3 (#908) (#949)
* DOCS-11790 ESR Rule * DOCS-11790 ESR Discussion * Review feedback * Review feedback * Review feedback * Review feedback * Review feedback
1 parent 87fb241 commit 2492c2c

File tree

6 files changed

+195
-13
lines changed

6 files changed

+195
-13
lines changed

source/applications/indexes.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,24 @@ index, and in addition, MongoDB can use an :doc:`intersection
3737

3838
The following documents introduce indexing strategies:
3939

40-
:doc:`/tutorial/create-indexes-to-support-queries`
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.
43+
44+
:ref:`create-indexes-to-support-queries`
4145
An index supports a query when the index contains all the fields
4246
scanned by the query. Creating indexes that support queries
4347
results in greatly increased query performance.
4448

45-
:doc:`/tutorial/sort-results-with-indexes`
49+
:ref:`sorting-with-indexes`
4650
To support efficient queries, use the strategies here when you
4751
specify the sequential order and sort order of index fields.
4852

49-
:doc:`/tutorial/ensure-indexes-fit-ram`
53+
:ref:`indexes-ensure-indexes-fit-ram`
5054
When your index fits in RAM, the system can avoid reading the
5155
index from disk and you get the fastest processing.
5256

53-
:doc:`/tutorial/create-queries-that-ensure-selectivity`
57+
:ref:`index-selectivity`
5458
Selectivity is the ability of a query to narrow results using the
5559
index. Selectivity allows MongoDB to use the index for a larger
5660
portion of the work associated with fulfilling the query.
@@ -60,6 +64,7 @@ The following documents introduce indexing strategies:
6064
:titlesonly:
6165
:hidden:
6266

67+
/tutorial/equality-sort-range-rule
6368
/tutorial/create-indexes-to-support-queries
6469
/tutorial/sort-results-with-indexes
6570
/tutorial/ensure-indexes-fit-ram

source/core/index-compound.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ operation that resembles the following prototype:
3939

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

42+
The order of the indexed fields has a strong impact on the
43+
effectiveness of a particular index for a given query. For most
44+
compound indexes, following the :ref:`ESR (Equality, Sort, Range) rule
45+
<esr-indexing-rule>` helps to create efficient indexes.
46+
4247
.. important::
4348

4449
Starting in MongoDB 4.4:

source/indexes.txt

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

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

148151
Multikey Index
149152
~~~~~~~~~~~~~~
@@ -279,13 +282,6 @@ Indexes and Collation
279282

280283
.. include:: /includes/extracts/collation-versionadded.rst
281284

282-
----------
283-
284-
|arrow| Use the **Select your language** drop-down menu in the
285-
upper-right to set the language of the examples on this page.
286-
287-
----------
288-
289285
.. include:: /includes/driver-examples/driver-example-indexes-2.rst
290286

291287
.. include:: /includes/extracts/collation-index-use.rst

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
@@ -159,6 +159,8 @@ In such cases, MongoDB can use the index to retrieve the documents in
159159
order specified by the sort. As the example shows, the index prefix in
160160
the query predicate can be different from the prefix in the sort.
161161

162+
.. _sort-index-nonprefix-subset:
163+
162164
Sort and Non-prefix Subset of an Index
163165
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
164166

0 commit comments

Comments
 (0)