|
| 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 | + |
0 commit comments