|
| 1 | +.. _mongoid-optimize-queries-with-indexes: |
| 2 | + |
| 3 | +============================= |
| 4 | +Optimize Queries With Indexes |
| 5 | +============================= |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. facet:: |
| 10 | + :name: genre |
| 11 | + :values: reference |
| 12 | + |
| 13 | +.. meta:: |
| 14 | + :keywords: code example, odm, optimization, efficiency, Atlas search |
| 15 | + |
| 16 | +.. contents:: On this page |
| 17 | + :local: |
| 18 | + :backlinks: none |
| 19 | + :depth: 3 |
| 20 | + :class: singlecol |
| 21 | + |
| 22 | +Overview |
| 23 | +-------- |
| 24 | + |
| 25 | +In this guide, you can learn how to use **indexes** with {+odm+}. Indexes can |
| 26 | +improve the efficiency of queries by limiting the number of documents MongoDB |
| 27 | +needs to scan. If your application is repeatedly running queries |
| 28 | +on certain fields, you can create an index on those fields to improve query |
| 29 | +performance. |
| 30 | + |
| 31 | +The following sections in this guide describe how to declare and create different |
| 32 | +types of indexes using {+odm+}. The examples use the ``Restaurant`` model, which |
| 33 | +maps to the ``restaurants`` collection in the ``sample_restaurants`` database. |
| 34 | +To learn how to connect to this database |
| 35 | +and collection using {+odm+}, see the :ref:`mongoid-quick-start-rails` or |
| 36 | +:ref:`mongoid-quick-start-sinatra` guides. |
| 37 | + |
| 38 | +Declare and Create an Index |
| 39 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 40 | + |
| 41 | +When using {+odm+}, you can declare your index using the ``index`` macro and |
| 42 | +then create it using the ``create_indexes`` command. |
| 43 | + |
| 44 | +The following code example shows how to declare and create an ascending index |
| 45 | +named ``cuisine_index`` on the ``cuisine`` field in the ``Restaurant`` class: |
| 46 | + |
| 47 | +.. literalinclude:: /includes/data-modeling/indexes.rb |
| 48 | + :language: ruby |
| 49 | + :emphasize-lines: 8, 11 |
| 50 | + :start-after: start create index |
| 51 | + :end-before: end create index |
| 52 | + |
| 53 | +The ``index`` macro defines the index you want to create and the ``create_indexes`` |
| 54 | +command creates it in the ``restaurants`` collection. |
| 55 | + |
| 56 | +When defining an index, the first hash object contains the field you want to |
| 57 | +index and its direction. ``1`` represents an ascending index, and ``-1`` represents a |
| 58 | +descending index. The second hash object contains index options. To learn more |
| 59 | +about index options, see the :ref:`mongoid-indexes-api-documentation` section. |
| 60 | + |
| 61 | +Aliases and Declaring Indexes |
| 62 | ++++++++++++++++++++++++++++++ |
| 63 | + |
| 64 | +You can use aliased field names in index definitions. For example, the following |
| 65 | +code creates an index on the ``b`` field, which is an alias of the ``borough`` |
| 66 | +field: |
| 67 | + |
| 68 | +.. literalinclude:: /includes/data-modeling/indexes.rb |
| 69 | + :language: ruby |
| 70 | + :start-after: start create alias index |
| 71 | + :end-before: end create alias index |
| 72 | + |
| 73 | +Create an Index on Embedded Document Fields |
| 74 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 75 | + |
| 76 | +You can define an index on embedded document fields. The following code example |
| 77 | +shows how to declare an ascending index on the ``street`` field, which is embedded |
| 78 | +within the ``address`` field in the ``Restaurant`` model. |
| 79 | + |
| 80 | +.. literalinclude:: /includes/data-modeling/indexes.rb |
| 81 | + :language: ruby |
| 82 | + :start-after: start create embedded index |
| 83 | + :end-before: end create embedded index |
| 84 | + |
| 85 | +Create a Compound Index |
| 86 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 87 | + |
| 88 | +You can define a compound index on multiple fields. The following code example |
| 89 | +shows how to declare a compound index that is ascending on the ``borough`` |
| 90 | +field and descending on the ``name`` field. |
| 91 | + |
| 92 | +.. literalinclude:: /includes/data-modeling/indexes.rb |
| 93 | + :language: ruby |
| 94 | + :start-after: start create compound index |
| 95 | + :end-before: end create compound index |
| 96 | + |
| 97 | +Create a Geospatial Index |
| 98 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 99 | + |
| 100 | +You can define a 2dsphere index on fields that contain GeoJSON objects or |
| 101 | +coordinate pairs. |
| 102 | +The following example defines a 2dsphere index on a field that contains GeoJSON |
| 103 | +objects: |
| 104 | + |
| 105 | +.. literalinclude:: /includes/data-modeling/indexes.rb |
| 106 | + :language: ruby |
| 107 | + :start-after: start create 2dsphere index |
| 108 | + :end-before: end create 2dsphere index |
| 109 | + |
| 110 | +For more information on 2dsphere indexes, see the :manual:`2dsphere </core/indexes/index-types/geospatial/2dsphere/>` |
| 111 | +guide in the MongoDB {+server-manual+}. |
| 112 | + |
| 113 | +For more information on the GeoJSON type, see the :manual:`GeoJSON Objects </reference/geojson/>` |
| 114 | +guide in the MongoDB {+server-manual+}. |
| 115 | + |
| 116 | +Create a Sparse Index |
| 117 | +~~~~~~~~~~~~~~~~~~~~~ |
| 118 | + |
| 119 | +You can define a sparse index on fields that are not present in all documents. |
| 120 | +The following code example defines a sparse index on the ``borough`` field: |
| 121 | + |
| 122 | +.. literalinclude:: /includes/data-modeling/indexes.rb |
| 123 | + :language: ruby |
| 124 | + :start-after: start create sparse index |
| 125 | + :end-before: end create sparse index |
| 126 | + |
| 127 | +For more information on sparse indexes, see the :manual:`Sparse Indexes </core/index-sparse/>` |
| 128 | +guide in the MongoDB {+server-manual+}. |
| 129 | + |
| 130 | +Create Multiple Indexes |
| 131 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 132 | + |
| 133 | +You can define multiple indexes within your model and create them using a single |
| 134 | +``create_indexes`` call. The following example shows how to create multiple |
| 135 | +indexes at the same time: |
| 136 | + |
| 137 | +.. literalinclude:: /includes/data-modeling/indexes.rb |
| 138 | + :language: ruby |
| 139 | + :start-after: start create multiple indexes |
| 140 | + :end-before: end create multiple indexes |
| 141 | + |
| 142 | +Drop Indexes |
| 143 | +~~~~~~~~~~~~ |
| 144 | + |
| 145 | +You can drop all indexes in your collection. The following example drops all |
| 146 | +indexes in the ``Restaurant`` model: |
| 147 | + |
| 148 | +.. literalinclude:: /includes/data-modeling/indexes.rb |
| 149 | + :language: ruby |
| 150 | + :start-after: start drop indexes |
| 151 | + :end-before: end drop indexes |
| 152 | + |
| 153 | +.. note:: Default Index |
| 154 | + |
| 155 | + MongoDB creates a default index on the ``_id`` field during the |
| 156 | + creation of a collection. This index prevents clients from inserting |
| 157 | + two documents with the same values for the ``_id`` field. You cannot |
| 158 | + drop this index. |
| 159 | + |
| 160 | +Atlas Search Indexes |
| 161 | +~~~~~~~~~~~~~~~~~~~~ |
| 162 | + |
| 163 | +You can declare and manage Atlas Search indexes using {+odm+}. |
| 164 | + |
| 165 | +To declare a search index, use the ``search_index`` macro within your model. To |
| 166 | +create the search indexes declared within a model, use the ``create_search_indexes`` |
| 167 | +command. The following code example shows how to declare and create an Atlas |
| 168 | +Search index named ``my_search_index``. |
| 169 | +The index is on the ``name`` and ``cuisine`` fields and is dynamic. |
| 170 | + |
| 171 | +.. literalinclude:: /includes/data-modeling/indexes.rb |
| 172 | + :language: ruby |
| 173 | + :start-after: start create atlas search index |
| 174 | + :end-before: end create atlas search index |
| 175 | + |
| 176 | +To learn more about the syntax for creating an Atlas Search index, see |
| 177 | +the :atlas:`Create an Atlas Search Index </atlas-search/create-index/#std-label-ref-create-index>` |
| 178 | +guide in the MongoDB Atlas documentation. |
| 179 | + |
| 180 | +Remove an Atlas Search Index |
| 181 | +++++++++++++++++++++++++++++ |
| 182 | + |
| 183 | +To remove an Atlas Search index, use the ``remove_search_indexes`` command. The |
| 184 | +following code example shows how to remove an Atlas Search index from the |
| 185 | +``restaurants`` collection: |
| 186 | + |
| 187 | +.. literalinclude:: /includes/data-modeling/indexes.rb |
| 188 | + :language: ruby |
| 189 | + :start-after: start remove atlas search index |
| 190 | + :end-before: end remove atlas search index |
| 191 | + |
| 192 | +List Atlas Search Indexes |
| 193 | ++++++++++++++++++++++++++ |
| 194 | + |
| 195 | +You can enumerate through all Atlas Search indexes in your collection |
| 196 | +by using the ``search_indexes`` command. The following example enumerates through |
| 197 | +all Atlas Search indexes in the ``restaurants`` collection and prints out their |
| 198 | +information: |
| 199 | + |
| 200 | +.. literalinclude:: /includes/data-modeling/indexes.rb |
| 201 | + :language: ruby |
| 202 | + :start-after: start list atlas search index |
| 203 | + :end-before: end list atlas search index |
| 204 | + |
| 205 | +.. _mongoid-indexes-api-documentation: |
| 206 | + |
| 207 | +API Documentation |
| 208 | +----------------- |
| 209 | + |
| 210 | +To learn more about using indexes in {+odm+}, see the |
| 211 | +`Mongoid::Indexable::ClassMethods <{+api-root+}/Indexable/ClassMethods.html>`__ |
| 212 | +documentation. |
| 213 | + |
| 214 | +To learn more about index options, see the `Mongoid::Indexable::Validators::Options |
| 215 | +<{+api-root+}/Indexable/Validators/Options.html>`__ documentation. |
| 216 | + |
| 217 | +To learn more about using Atlas Search indexes in {+odm+}, see the |
| 218 | +`Mongoid::SearchIndexable::ClassMethods <{+api-root+}/SearchIndexable/ClassMethods.html>`__ |
| 219 | +documentation. |
0 commit comments