Firestore: Why is querying for each individual document faster than fetching a whole collection?

Viewed 196

I was doing some benchmarking to figure out which what was the best to structure my Firestore data to have the fastest queries. While doing so, I came across this interesting behavior.

In the example below, there are 1,000 documents in the Firestore collection "profiles". When I query for the whole collection, it takes on average 4.93 seconds. When I query for each individual document and wrap them in a Promise.all, it takes 2.65 seconds. Could someone explain why the latter query is almost twice as fast?

async function getProfiles() {
  const startDate = Date.now();

  // Fetch 1000 profiles with one query
  const snapshot = await firestore.collection("profiles").get();
  const midDate = Date.now();

  const userIds = snapshot.docs.map((doc) => doc.id);

  const allFetches = userIds.map((userId) =>
    firestore.collection("profiles").doc(userId).get()
  );

  // Fetch 1000 profiles with 1000 queries
  await Promise.all(allFetches);
  const endDate = Date.now();

  // ~4.93s
  console.log((midDate - startDate) / 1000);

  // ~2.65s
  console.log((endDate - midDate) / 1000);
}

getProfiles();
1 Answers

This is probably related with your data or machine (connection, etc...). I recreated this in Cloud Shell like this:

  1. Created 1000 of documents like this:
for (let i = 0; i < 1000; i++) {
   colref.doc().set( {data: { test_value1: i, test_value2: "test"}})
}
  1. Used the your code in nodejs and average results for 10 tries are:
0.748  
1.532 

So completely opposite to your result. I think this must be something specific to your database structure or runtime environment.

Related