Indexes overview
This page describes indexing for Firestore with MongoDB compatibility. Firestore with MongoDB compatibility does not create any indexes by default. To improve database performance, create indexes for your most commonly used queries.
Indexes have a large impact on the performance of a database. If an index exists for a query, the database can efficiently return results by reducing the amount of data that needs to be scanned and reducing the work needed to sort the results. However, index entries increase storage costs and the amount of work done during a write operation on indexed fields.
Index definition and structure
An index consists of the following:
- a collection ID
- a list of fiends in the given collection
- an order, either ascending or descending, for each field
An index can also enable the sparse and multikey options.
Index ordering
The order and sort direction of each field uniquely defines the index. For example, the following indexes are two distinct indexes and not interchangeable:
Collection | Fields |
---|---|
cities | country (ascending), population (descending) |
cities | population (descending), country (ascending), |
When creating an index to support a query, include the fields in the same order as your query.
Index density
By default, an index entries store data from all documents in a collection. This is known as a non-sparse index. An index entry will be added for a document regardless of whether the document contains any of the fields specified in the index. Non-existent fields are treated as having a NULL value when generating index entries. To change this behavior, you can define the index as a sparse index.
Sparse indexes
A sparse index indexes only the documents in the collection that contain a value (including null) for at least one of the indexed fields. A sparse index reduces storage costs and can improve performance.
Multikey indexes for array values
If you are creating an index on a field that contain array values, you must create a multikey index. A regular index cannot index array values. A multikey index supports up to one array field in the index definition and can be used for operations that traverse array values.
Only use multikey indexes if you know that you need to index array values. Regular indexes have advantages when processing a query. For example, regular indexes can filter value within a range more efficiently.
The following situations lead to errors when working with array values and multikey indexes:
- An operation attempts to add an array value to a field indexed by a regular index. To add the array value, you must delete existing regular indexes on that field, and recreate them as multikey indexes.
- You attempt to create a regular index on a field that contains an array value. You must either create a multikey index or delete the array values.
- An operation attempts to index multiple fields with array values. You cannot have more than one field with an array value in a multikey index. To proceed, modify your data model or your index definitions.
- You attempt to create a multikey index where two field paths share a common
prefix like
users.posts
andusers.zip
.
Troubleshoot index building errors
You might encounter index building errors when managing your indexes. An indexing operation can fail if the database encounters a problem with the data. Indexing operations can fail for the following reasons:
- You have reached an index limit. For example, the operation may have reached the maximum number of index entries per document. If index creation fails, you see an error message.If you have not reached an index limit, re-try the index operation.
- A multikey index is required. At least one of the indexed fields contains an array value. To proceed, you must either use a multikey index or delete the array values.
- An operation attempts to index multiple fields with array values. You cannot have more than one field with an array value in a multikey index. To proceed, modify your data model or your index definitions.
What's next
- Learn how to create and manage indexes