|
| 1 | +============== |
| 2 | +Index Overview |
| 3 | +============== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +Synopsis |
| 8 | +-------- |
| 9 | + |
| 10 | +Indexes are an internal representation of the documents in your |
| 11 | +database organized so that MongoDB can use them to quickly locate |
| 12 | +documents and fulfill queries very efficiently. Fundamentally, indexes |
| 13 | +in MongoDB are similar to indexes, MongoDB supports indexes on any |
| 14 | +field or sub-field contained in documents within a MongoDB |
| 15 | +collection. Consider the following core features of indexes: |
| 16 | + |
| 17 | +- MongoDB defines indexes on a per-collection level. |
| 18 | + |
| 19 | +- Every query (including update operations,) can use one and only one |
| 20 | + index. The query optimizer determines, empirically, the best query |
| 21 | + plan and indexes to use on a specific query, but can be overridden |
| 22 | + using the :func:`cursor.hint()` method. However, :ref:`compound |
| 23 | + indexes <index-types-compound>` make it possible to include multiple |
| 24 | + fields in a single index. |
| 25 | + |
| 26 | +- Indexes often dramatically increase the performance of queries; |
| 27 | + however, each index creates a slight overhead for every write |
| 28 | + operation. |
| 29 | + |
| 30 | +- Queries that are "covered" by the index are return much more quickly |
| 31 | + than documents that have to scan many individual documents. Using |
| 32 | + queries with good index coverage it possible for MongoDB to only |
| 33 | + store the index itself and the most often used documents in memory, |
| 34 | + which can maximize database capacity, performance and throughput. |
| 35 | + |
| 36 | +Continue reading for a complete overview of indexes in MongoDB, |
| 37 | +including the :ref:`types of indexes <index-types>`, basic |
| 38 | +:ref:`operations with indexes <index-operations>`, and other MongoDB |
| 39 | +:ref:`features <index-features>` implemented using indexes. |
| 40 | + |
| 41 | +.. TODO add index section seealso |
| 42 | + |
| 43 | +.. index:: index types |
| 44 | +.. _index-types: |
| 45 | + |
| 46 | +Index Types |
| 47 | +----------- |
| 48 | + |
| 49 | +All indexes in MongoDB are "B-Tree" indexes. In the :program:`mongo` |
| 50 | +shell, the helper :func:`ensureIndex() <db.collection.ensureIndex()>` |
| 51 | +provides a method for creating indexes. This section provides an |
| 52 | +overview of the types of indexes available in MongoDB as well as an |
| 53 | +introduction to their use. |
| 54 | + |
| 55 | +.. index:: _id index |
| 56 | +.. index:: _id |
| 57 | +.. index:: index; _id |
| 58 | +.. index:: index types; primary |
| 59 | + |
| 60 | +_id |
| 61 | +~~~ |
| 62 | + |
| 63 | +The ``_id`` index is a :ref:`unique index <index-type-unique>` on the |
| 64 | +``_id`` field, and MongoDB creates this index by default on all |
| 65 | +collections. [#capped-collections]_ You cannot delete the index on |
| 66 | +``_id``. |
| 67 | + |
| 68 | +The ``_id`` field is the :term:`primary key` for the |
| 69 | +collection, and every document *must* have a unique ``_id`` field. If |
| 70 | +the application or client does not insert a value into the ``_id`` |
| 71 | +field, typically an :term:`ObjectId`, the server will insert an |
| 72 | +ObjectId in to the ``_id`` field. |
| 73 | + |
| 74 | +While ObjectIds are 12-byte, unique identifiers, that make suitable |
| 75 | +``_id`` values, the only requirement of this field is that it be |
| 76 | +unique within a collection. You may store any unique value in the |
| 77 | +``_id`` field. |
| 78 | + |
| 79 | +.. note:: |
| 80 | + |
| 81 | + In :term:`shard clusters <shard cluster>`, if the you do *not* use |
| 82 | + the ``_id`` field as the :term:`shard key`, then your application |
| 83 | + **must** ensure the uniqueness of the values in the ``_id`` field |
| 84 | + to prevent errors. |
| 85 | + |
| 86 | +.. [#capped-collections] Capped collections do not have an ``_id`` |
| 87 | + index. |
| 88 | + |
| 89 | +.. _index-types-secondary: |
| 90 | + |
| 91 | +Secondary Indexes |
| 92 | +~~~~~~~~~~~~~~~~~ |
| 93 | + |
| 94 | +All indexes other than the index on the ``_id`` field are |
| 95 | +:term:`secondary indexes <secondary index>`. You can create indexes on |
| 96 | +any field within any document or sub-document. Additionally, you can |
| 97 | +create compound indexes with multiple fields, so that a single query |
| 98 | +can match multiple components using the index without needing to scan |
| 99 | +(as many) actual documents. |
| 100 | + |
| 101 | +In general, you should have secondary indexes that support all of your |
| 102 | +primary common and user-facing queries, and require MongoDB to scan |
| 103 | +the fewest number of documents possible. |
| 104 | + |
| 105 | +The specifications an index using the :func:`ensureIndex() |
| 106 | +<db.collection.ensureIndex()>` operation will resemble the following: |
| 107 | + |
| 108 | +.. code-block:: javascript |
| 109 | + |
| 110 | + { "field": 1 } |
| 111 | + { "field0.field1": 1 } |
| 112 | + { "field0": 1, "field1": 1 } |
| 113 | + |
| 114 | +For each field in the index you will specify either ``1`` or ``-1``, |
| 115 | +which represents the order of the keys in the index. For indexes with |
| 116 | +more than one key (i.e. "compound indexes,") the sequence of fields is |
| 117 | +important. |
| 118 | + |
| 119 | +Embedded Fields |
| 120 | +``````````````` |
| 121 | + |
| 122 | +You can create indexes on fields that exist in sub-documents within |
| 123 | +your collection. Consider the collection ``people`` that holds |
| 124 | +documents that resemble the following an example document: |
| 125 | + |
| 126 | +.. code-block:: javascript |
| 127 | + |
| 128 | + { |
| 129 | + "_id": ObjectId(...) |
| 130 | + "name": "John Doe" |
| 131 | + "address": { |
| 132 | + "street": "Main" |
| 133 | + "zipcode": 53511 |
| 134 | + "state": "WI" |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | +You could create an index on the ``address.zipcode`` field, using the |
| 139 | +following specification: |
| 140 | + |
| 141 | +.. code-block:: javascript |
| 142 | + |
| 143 | + db.people.ensureIndex( { "address.zipcode": 1 } ) |
| 144 | + |
| 145 | +Introspecting sub-documents in this way is commonly called "dot |
| 146 | +notation." |
| 147 | + |
| 148 | +.. _index-types-compound: |
| 149 | + |
| 150 | +Compound Indexes |
| 151 | +```````````````` |
| 152 | + |
| 153 | +MongoDB supports "compound indexes," where a single index structure |
| 154 | +holds references to multiple fields within a collection's |
| 155 | +documents. Consider the collection ``products`` that holds documents |
| 156 | +that resemble the following an example document: |
| 157 | + |
| 158 | +.. code-block:: javascript |
| 159 | + |
| 160 | + { |
| 161 | + "_id": ObjectId(...) |
| 162 | + "item": "Banana" |
| 163 | + "category": ["food", "produce", "grocery"] |
| 164 | + "stock": 4 |
| 165 | + "type": cases |
| 166 | + "arrival": Date(...) |
| 167 | + } |
| 168 | + |
| 169 | +Most queries probably select on the ``item`` field, but a significant |
| 170 | +number of queries will also need to check the ``stock`` field. You can |
| 171 | +specify a single compound index to support all of these queries: |
| 172 | + |
| 173 | +.. code-block:: javascript |
| 174 | + |
| 175 | + db.products.ensureIndex( { "item": 1, "stock": 1 } ) |
| 176 | + |
| 177 | +MongoDB will be able to use this index to support all queries that |
| 178 | +select on the ``item`` field as well as those queries that select on |
| 179 | +the ``item`` field **and** the ``stock`` field. These indexes will not |
| 180 | +support queries that select *only* on the ``stock`` field. |
| 181 | + |
| 182 | +Ascending and Descending |
| 183 | +```````````````````````` |
| 184 | + |
| 185 | +Indexes store references to fields in either ascending or descending |
| 186 | +order. Because MongoDB can transverse the index in either direction, |
| 187 | +the order of keys often doesn't matter. However, in compound indexes, |
| 188 | +for some kinds of sort operations, it's useful to have the fields |
| 189 | +running in opposite order. |
| 190 | + |
| 191 | +To specify an index with an ascending order, use the following form: |
| 192 | + |
| 193 | +.. code-block:: javascript |
| 194 | + |
| 195 | + db.products.ensureIndex( { "field": -1 } ) |
| 196 | + |
| 197 | +More typically in the context of a :ref:`compound index |
| 198 | +<index-type-compound>`, the specification would resemble the |
| 199 | +following: |
| 200 | + |
| 201 | +.. code-block:: javascript |
| 202 | + |
| 203 | + db.products.ensureIndex( { "field0": 1, "field1": -1 } ) |
| 204 | + |
| 205 | +.. TODO understand the sort operations better. |
| 206 | + |
| 207 | +.. _index-types-multikey: |
| 208 | + |
| 209 | +Multikey |
| 210 | +```````` |
| 211 | + |
| 212 | +Sparse Index |
| 213 | +~~~~~~~~~~~~ |
| 214 | + |
| 215 | +.. _index-type-unique: |
| 216 | + |
| 217 | +Unique Index |
| 218 | +~~~~~~~~~~~~ |
| 219 | + |
| 220 | +.. discuss compound unique indexes enforce uniqueness over the |
| 221 | + combination of the fields, not for any specific key. |
| 222 | + |
| 223 | + |
| 224 | +.. _index-creation-operations: |
| 225 | + |
| 226 | +Index Creation Options |
| 227 | +---------------------- |
| 228 | + |
| 229 | +Background Construction |
| 230 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 231 | + |
| 232 | +Duplicate Dropping |
| 233 | +~~~~~~~~~~~~~~~~~~ |
| 234 | + |
| 235 | +.. _index-features: |
| 236 | + |
| 237 | +Index Features |
| 238 | +-------------- |
| 239 | + |
| 240 | +TTL Indexes |
| 241 | +~~~~~~~~~~~ |
| 242 | + |
| 243 | +TTL indexes are special indexes that MongoDB can use to automatically |
| 244 | +remove documents from a collection after a certain amount of |
| 245 | +time. This is ideal for some types of information like machine |
| 246 | +generated event data, logs, and session information that only need to |
| 247 | +persist in a database for a limited period of time. |
| 248 | + |
| 249 | +These indexes have the following limitations: |
| 250 | + |
| 251 | +- The index can only contain one field. Compound indexes are *not* |
| 252 | + supported. |
| 253 | + |
| 254 | +- The indexed field **must** contain a value with a date type. |
| 255 | + |
| 256 | +- If the field holds an array, and there are multiple date-typed data |
| 257 | + in the index, the document will expire when the *lowest* |
| 258 | + (i.e. earliest) matches the expiration threshold. |
| 259 | + |
| 260 | +In all other respects, TTL indexes are normal :ref:`secondary indexes |
| 261 | +<index-types-secondary>`, and if appropriate, MongoDB can uses these |
| 262 | +indexes to fulfill arbitrary queries. |
| 263 | + |
| 264 | +.. see:: :doc:`/tutorial/expire-data` |
| 265 | + |
| 266 | +Geospatial Indexes |
| 267 | +~~~~~~~~~~~~~~~~~~ |
| 268 | + |
| 269 | +Index Limitations |
| 270 | +----------------- |
| 271 | + |
| 272 | +- Number of indexes. |
| 273 | + |
| 274 | +- Length of index keys. |
0 commit comments