Index types in Cloud Firestore

Indexes are an important factor in the performance of a database. Much like the index of a book which maps topics in a book to page numbers, a database index maps the items in a database to their locations in the database. When you send a database a query, the database can use an index to quickly look up the locations of the items you requested.

This page describes the two types of indexes that Cloud Firestore uses, single-field indexes and composite indexes.

An index behind every query

If no index exists for a query, most databases crawl through their contents item by item, a slow process that slows down even more as the database grows. Cloud Firestore guarantees high query performance by using indexes for all queries. As a result, query performance depends on the size of the result set and not on the number of items in the database.

Less index management, more app development

Cloud Firestore includes features that reduce the amount of time you need to spend managing indexes. The indexes required for the most basic queries are automatically created for you. As you use and test your app, Cloud Firestore helps you identify and create additional indexes your app requires.

Index types

Cloud Firestore uses two types of indexes, single-field and composite. Both types of indexes are uniquely defined by the fields they index and the index mode on each field.

Index modes

When you define an index, you select an index mode for each indexed field. There are three options for index mode:

Index mode Description
Ascending arrow_upward Supports <, <=, ==, >=, and > query clauses on the field and supports sorting results in ascending order based on this field value.
Descending arrow_downward Supports <, <=, ==, >=, and > query clauses on the field and supports sorting results in descending order based on this field value.
Array‑contains Supports array_contains query clauses on the field.

So, for example, an index on field foo in ascending mode is distinct from an index on field foo in descending mode.

Single-field indexes

A single-field index stores a sorted mapping of all the documents in a collection that contain a specific field. Each entry in a single-field index records a document's value for a specific field and the location of the document in the database. Cloud Firestore automatically maintains single-field indexes for each field in a document.

Automatic indexing

By default, Cloud Firestore automatically maintains an index for each field in a document and each subfield in a map. Cloud Firestore uses the following settings for automatically created single-field indexes:

  • For each non-array and non-map field, Cloud Firestore defines two single-field indexes, one in ascending mode and one in descending mode.

  • For each map field, Cloud Firestore creates one ascending index and one descending index for each non-array and non-map subfield in the map.

  • For each array field in a document, Cloud Firestore creates and maintains an array-contains index.

Single-field index exemptions

You can exempt a field from automatic indexing by creating a single-field index exemption. An indexing exemption overrides the database-wide automatic index settings. For cases where exemptions can be useful, see the indexing best practices.

To create and manage single-field index exemptions, see Managing Indexes in Cloud Firestore.

Examples of queries supported by single-field indexes

By automatically creating these indexes for you, Cloud Firestore allows your application to quickly support the most basic database queries. Single- field indexes allow you to perform simple queries based on field values and the comparators <, <=, ==, >=, and >. For array fields, they allow you to perform array_contains queries.

To illustrate, examine the cities examples from the point of view of index creation. The following example creates a few city documents in a cities collection and sets name, state, country, capital, population, and tags fields for each document:

Web
var citiesRef = db.collection("cities");

citiesRef.doc("SF").set({
    name: "San Francisco", state: "CA", country: "USA",
    capital: false, population: 860000,
    regions: ["west_coast", "norcal"] });
citiesRef.doc("LA").set({
    name: "Los Angeles", state: "CA", country: "USA",
    capital: false, population: 3900000,
    regions: ["west_coast", "socal"] });
citiesRef.doc("DC").set({
    name: "Washington, D.C.", state: null, country: "USA",
    capital: true, population: 680000,
    regions: ["east_coast"] });
citiesRef.doc("TOK").set({
    name: "Tokyo", state: null, country: "Japan",
    capital: true, population: 9000000,
    regions: ["kanto", "honshu"] });
citiesRef.doc("BJ").set({
    name: "Beijing", state: null, country: "China",
    capital: true, population: 21500000,
    regions: ["jingjinji", "hebei"] });

For each set operation, Cloud Firestore updates one ascending single-field index per non-array field, one descending single-field index per non-array field, and one array-contains single-field index for the array field.

Cloud Firestore updates one array-contains single-index for the array field and two single-field indexes per non-array field. Each row in the following table represents an entry in a single-field index:

Collection Field indexed
cities arrow_upward name
cities arrow_upward state
cities arrow_upward country
cities arrow_upward capital
cities arrow_upward population
cities arrow_downward name
cities arrow_downward state
cities arrow_downward country
cities arrow_downward capital
cities arrow_downward population
cities array-contains regions

Using these automatically created indexes, you can run simple queries like the following:

Web
citiesRef.where("state", "==", "CA")
citiesRef.where("population", "<", 100000)
citiesRef.where("name", ">=", "San Francisco")

The array_contains index allows you to query the regions array field:

Web
citiesRef.where("regions", "array-contains", "west_coast")

You can also create compound queries based on equalities (==):

Web
citiesRef.where("state", "==", "CO").where("name", "==", "Denver")
citiesRef.where("country", "==", "USA").where("capital", "==", false).where("state", "==", "CA").where("population", "==", 860000)

If you need to run a compound query that uses a range comparison (<, <=, >, or >=) or if you need to sort by a different field, you must create a composite index for that query.

Composite indexes

A composite index stores a sorted mapping of all the documents in a collection that contain multiple specific fields instead of just one. A composite index also defines an index mode for each of its fields (ascending, descending, or array-contains), and the index is sorted based on the index modes.

Cloud Firestore uses composite indexes to support compound queries not already supported by single-field indexes. For example, you would need a composite index for the following queries:

Web
citiesRef.where("country", "==", "USA").orderBy("population", "asc")
citiesRef.where("country", "==", "USA").where("population", "<", 3800000)
citiesRef.where("country", "==", "USA").where("population", ">", 690000)

These queries require the composite index below. Note that since the query uses an equality for the country field, the field's index mode can be either descending or ascending. By default, inequality queries will apply an ascending sort order based on the field in the inequality clause.

Collection Fields indexed
cities arrow_upward (or arrow_downward) country, arrow_upward population

Cloud Firestore does not automatically create composite indexes like it does for single-field indexes because of the large number of possible field combinations. Instead, Cloud Firestore helps you identify and create required composite indexes as you build your app.

If you attempt the query above without first creating the required index, Cloud Firestore returns an error message containing a link you can follow to create the missing index. This happens any time you attempt a query not supported by an index. You can also define and manage composite indexes manually by using the console or by using the Firebase CLI. For more on creating and managing indexes, see Managing Indexes.

If you want to run the same queries but with a descending sort order, then you need an additional composite index in the descending direction for population:

Web
citiesRef.where("country", "==", "USA").orderBy("population", "desc")

citiesRef.where("country", "==", "USA")
         .where("population", "<", 3800000)
         .orderBy("population", "desc")

citiesRef.where("country", "==", "USA")
         .where("population", ">", 690000)
         .orderBy("population", "desc")
Collection Fields indexed
cities arrow_upward country, arrow_upward population
cities arrow_upward country, arrow_downward population

You'll also need to create a composite index if you want to combine an array_contains query with additional clauses.

Web
citiesRef.where("regions", "array_contains", "east_coast")
         .where("capital", "==", true)
Collection Fields indexed
cities array-contains tags, arrow_upward (or arrow_downward) capital

Indexes and pricing

Indexes contribute to the storage costs of your application. For more on how storage size for indexes is calculated, see Index entry size.

Taking advantage of index merging

Although Cloud Firestore uses an index for every query, it does not necessarily require one index per query. For queries with multiple equality (==) clauses and, optionally, an orderBy clause, Cloud Firestore can re-use existing indexes. Cloud Firestore can merge the indexes for simple equality filters to build the composite indexes needed for larger equality queries.

You can reduce indexing costs by identifying situations where you can take advantage of index merging. For example, imagine a restaurants collection for a restaurant rating app:

  • collections_bookmark restaurants

    • class burgerthyme

      name : "Burger Thyme"
      category : "burgers"
      city : "San Francisco"
      editors_pick : true
      star_rating : 4

Now, imagine this app uses queries like the ones below. Notice that the app uses combinations of equality clauses for category, city, and editors_pick while always sorting by ascending star_rating:

Web
db.collection("restaurants").where("category", "==", "burgers")
                            .orderBy("star_rating")

db.collection("restaurants").where("city", "==", "San Francisco")
                            .orderBy("star_rating")

db.collection("restaurants").where("category", "==", "burgers")
                            .where("city", "==", "San Francisco")
                            .orderBy("star_rating")

db.collection("restaurants").where("category", "==", "burgers")
                            .where("city", "==" "San Francisco")
                            .where("editors_pick", "==", true )
                            .orderBy("star_rating")

You could create an index for each query:

Collection Fields indexed
restaurants arrow_upward category, arrow_upward star_rating
restaurants arrow_upward city, arrow_upward star_rating
restaurants arrow_upward category, arrow_upward city, arrow_upward star_rating
restaurants arrow_upward category, arrow_upward city, arrow_upward editors_pick, arrow_upward star_rating

As a better solution, you can reduce the number of indexes by taking advantage of Cloud Firestore's ability to merge indexes for equality clauses:

Collection Fields indexed
restaurants arrow_upward category, arrow_upward star_rating
restaurants arrow_upward city, arrow_upward star_rating
restaurants arrow_upward editors_pick, arrow_upward star_rating

Not only is this set of indexes smaller, it also supports an additional query:

Web
db.collection("restaurants").where("editors_pick", "==", true)
                            .orderBy("star_rating")

Indexing limits

The following limits apply to indexes. For all quotas and limits, see Quotas and Limits.

Limit Details
Maximum number of composite indexes for a database 200
Maximum number of single-field index exemptions for a database 200

Maximum number of index entries for each document

40,000

The number of index entries is the sum of the following for a document:

  • The number of single-field index entries
  • The number of composite index entries
Maximum size of an index entry

7.5 KiB

To see how Cloud Firestore calculates index entry size, see index entry size.

Maximum sum of the sizes of a document's index entries

8 MiB

The total size is the sum of the following for a document:

  • The sum of the size of a document's single-field index entries
  • The sum of the size of a document's composite index entries
  • Maximum size of an indexed field value

    1500 bytes

    Field values over 1500 bytes are truncated. Queries involving truncated field values may return inconsistent results.

    Indexing best practices

    For most apps, you can rely on automatic indexing and the error message links to manage your indexes. However, you may want to add single-field exemptions in the following cases:

    Case Description
    Large string fields

    If you have a string field that often holds long string values that you don't use for querying, you can cut storage costs by exempting the field from indexing.

    High write rates to a collection containing documents with sequential values

    If you index a field that increases or decreases sequentially between documents in a collection, like a timestamp, then the maximum write rate to the collection is 500 writes per second. If you don't query based on the field with sequential values, you can exempt the field from indexing to bypass this limit.

    In an IoT use case with a high write rate, for example, a collection containing documents with a timestamp field might approach the 500 writes per second limit.

    Large array or map fields

    Large array or map fields can approach the limit of 20,000 index entries per document. If you are not querying based on a large array or map field, you should exempt it from indexing.

    Gửi phản hồi về...

    Bạn cần trợ giúp? Truy cập trang hỗ trợ của chúng tôi.