Описание тега mongodb-indexes
An index is a data structure that allows you to quickly locate documents based on the values stored in certain specified fields. Fundamentally, indexes in MongoDB are similar to indexes in other database systems. MongoDB supports indexes on one or more fields or sub-fields contained in documents within a MongoDB collection.
Supported index types include:
- single field indexes
- compound indexes
- multikey (array) indexes
- geospatial indexes
- text indexes
- hashed indexes
Depending on the index type, additional properties such as sparse or unique may also be supported.
Core Features
MongoDB indexes have the following core features:
Indexes are defined on a per-collection level.
Indexes can enhance query performance, often dramatically. However, each index also incurs some overhead for every write operation. Consider the queries, the frequency of these queries, the size of your working set, the insert load, and your application’s requirements as you create indexes in your MongoDB environment.
All MongoDB indexes use a B-tree data structure. MongoDB can use this representation of the data to optimize query responses.
When using indexes with
$or
queries, MongoDB can use a separate index for each clause in the query.MongoDB 2.6 added support for intersection of multiple indexes to be used to fulfill queries. In general, each index intersection involves two indexes; however, MongoDB can employ multiple/nested index intersections to resolve a query.
The query optimizer empirically selects the plan for a given query shape by occasionally running candidate query plans and caching the "winning" plan with the best response time. You can override the query optimizer using a
hint()
or index filter (MongoDB 2.6) to force a specific index to be used, however these should be used sparingly (typically only for testing)Using queries with good index coverage reduces the number of full documents that MongoDB needs to store in memory, thus maximizing database performance and throughput.