Skip to content
Advertisement

Firestore is super slow to retrieve documents even though an index is created

I am having trouble with Firestore in production. There is a collection I am trying to query and it has about 200-300 documents. It’s a simple query being done through a Cloud Function. I’ve setup a timer to log the duration of each query.

This one takes about 2-4 seconds

   const missionQuery = await this._firestore
      .collection(this._collectionName)
      .where('active', '==', true)
      .where('archived_at', '==', null)
      .get();

This one takes another 2-3 seconds

let missions = missionQuery.docs.map(doc =>
      plainToClass(Mission, doc.data())
    );

There might be something I can do with the plainToClass, but there is nothing I know I can do with the Firestore query. I created a compose index for both fields, but it didn’t help. It is pretty faster when I query slower collections.

Anyone has any idea on how to improve the performance of this query?

Thanks much

Advertisement

Answer

There is nothing you can do to speed up (or slow down) a query.

Firestore query performance is impacted mostly by the amount of data you request, so reducing that would be your best option. The simplest way is to request fewer documents, but an alternative is to request less data per document by either creating a separate collection with only the fields of each document that you need, or by using server-side projection with the select method.

Advertisement