|
| 1 | +.. _php-index-mgmt: |
| 2 | + |
| 3 | +=================================== |
| 4 | +Index Considerations and Management |
| 5 | +=================================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: query, optimization, efficiency |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn about using **indexes** to improve the |
| 24 | +efficiency of your queries and add functionality to querying and |
| 25 | +storing documents. |
| 26 | + |
| 27 | +Without a relevant index, MongoDB scans every document in a collection |
| 28 | +to find the documents that match a query. These collection scans are |
| 29 | +slow and can negatively affect the performance of your application. |
| 30 | +However, if the appropriate index exists, MongoDB can use the index to |
| 31 | +reduce the number of documents to inspect. |
| 32 | + |
| 33 | +Operational Considerations |
| 34 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 35 | + |
| 36 | +To improve query performance, build indexes on fields that appear often in |
| 37 | +your application's queries or operations that return sorted results. Each |
| 38 | +index that you add consumes disk space and memory, so we recommend |
| 39 | +that you track memory and disk usage when doing capacity planning. In addition, |
| 40 | +when a write operation updates an indexed field, MongoDB updates the related |
| 41 | +index, which can negatively impact performance for write operations. |
| 42 | + |
| 43 | +You can use :manual:`wildcard indexes </core/index-wildcard/>` in your |
| 44 | +MongoDB application to query against fields whose names are not known in |
| 45 | +advance or are arbitrary. Wildcard indexes are *not* designed to replace |
| 46 | +workload-based index planning. |
| 47 | + |
| 48 | +To learn more about designing your data model and choosing |
| 49 | +indexes, see the :manual:`Indexes |
| 50 | +</core/data-model-operations/#indexes>` section of the Operational |
| 51 | +Factors and Data Models guide in the {+mdb-server+} manual. |
| 52 | + |
| 53 | +Sample Data |
| 54 | +~~~~~~~~~~~ |
| 55 | + |
| 56 | +The examples in this guide use the ``movies`` collection in the |
| 57 | +``sample_mflix`` database from the :atlas:`Atlas sample datasets |
| 58 | +</sample-data>`. To learn how to create a free MongoDB Atlas cluster and |
| 59 | +load the sample datasets, see the :atlas:`Get Started with Atlas |
| 60 | +</getting-started>` guide. |
| 61 | + |
| 62 | +Create an Index |
| 63 | +--------------- |
| 64 | + |
| 65 | +MongoDB supports several index types to help query your data. |
| 66 | +The following pages describe different index types and provide sample |
| 67 | +code to programmatically create each type of index. |
| 68 | + |
| 69 | +- :ref:`php-single-field-index` |
| 70 | + |
| 71 | +.. - :ref:`php-compound-index` |
| 72 | +.. - :ref:`php-atlas-search-index` |
| 73 | + |
| 74 | +List Indexes |
| 75 | +------------ |
| 76 | + |
| 77 | +You can retrieve a list of indexes on a collection by calling the |
| 78 | +``MongoDB\Collection::listIndexes()`` method: |
| 79 | + |
| 80 | +.. literalinclude:: /includes/indexes/indexes.php |
| 81 | + :language: php |
| 82 | + :start-after: start-list-indexes |
| 83 | + :end-before: end-list-indexes |
| 84 | + :dedent: |
| 85 | + |
| 86 | +Remove an Index |
| 87 | +--------------- |
| 88 | + |
| 89 | +You can remove any unused index except the default unique index on the |
| 90 | +``_id`` field. |
| 91 | + |
| 92 | +The following sections provide examples that show how to remove one or |
| 93 | +more indexes from a collection. |
| 94 | + |
| 95 | +Delete a Single Index |
| 96 | +~~~~~~~~~~~~~~~~~~~~~ |
| 97 | + |
| 98 | +Pass the name of an index to the ``MongoDB\Collection::dropIndex()`` |
| 99 | +method to remove an index from a collection. |
| 100 | + |
| 101 | +The following example removes the ``'_title_'`` index from the |
| 102 | +``movies`` collection: |
| 103 | + |
| 104 | +.. literalinclude:: /includes/indexes/indexes.php |
| 105 | + :language: php |
| 106 | + :start-after: start-remove-index |
| 107 | + :end-before: end-remove-index |
| 108 | + :dedent: |
| 109 | + |
| 110 | +.. note:: |
| 111 | + |
| 112 | + You cannot remove a single field from a compound index. You must |
| 113 | + drop the entire index and create a new one to update the indexed |
| 114 | + fields. |
| 115 | + |
| 116 | +Delete All Indexes |
| 117 | +~~~~~~~~~~~~~~~~~~ |
| 118 | + |
| 119 | +You can delete all indexes by calling the |
| 120 | +``MongoDB\Collection::dropIndexes()`` method on a collection: |
| 121 | + |
| 122 | +.. literalinclude:: /includes/indexes/indexes.php |
| 123 | + :language: php |
| 124 | + :start-after: start-remove-all-indexes |
| 125 | + :end-before: end-remove-all-indexes |
| 126 | + :dedent: |
| 127 | + |
| 128 | +The ``dropIndexes()`` method returns information about the number of |
| 129 | +indexes removed and a success message. |
| 130 | + |
| 131 | +API Documentation |
| 132 | +~~~~~~~~~~~~~~~~~ |
| 133 | + |
| 134 | +To learn more about any of the methods or types discussed in this |
| 135 | +guide, see the following API documentation: |
| 136 | + |
| 137 | +- `MongoDB\\Collection::listIndexes() <{+api+}/method/MongoDBCollection-listIndexes/>`__ |
| 138 | +- `MongoDB\\Collection::dropIndex() <{+api+}/method/MongoDBCollection-dropIndex/>`__ |
| 139 | +- `MongoDB\\Collection::dropIndexes() <{+api+}/method/MongoDBCollection-dropIndexes/>`__ |
0 commit comments