Search code examples
firebaseindexinglimitgoogle-cloud-firestorecomposite

Firebase Firestore composite index limit of 200 - is it reached if used in nested structure with lots of users?


I am confused about what Firestore counts toward its composite index limit of 200.

For example, let's say I have thousands of users, and each user has thousands of characters. I want each user to be able to sort through his characters, based on different parameters about those characters. I have the following structure, where {} indicates a wildcard:

users:
   {user_ID}
      user_name: {user_name}
      user_email: {user_email}
      characters:
         {characterUID}
            name:  {name}
            strength:  {strength}
            speed:  {speed}
            stamina: {stamina}
            date_created:  {timestamp}

So, in android, I would then do this:

Query query = fsDB.collection("users").document("user_ID").collection("characters")
.orderBy("strength").orderBy("name");  

Or this:

Query query = fsDB.collection("users").document("user_ID").collection("characters")
.orderBy("speed").orderBy("date_created");

Then, I would create the composite index in Firebase Console as:

Collection Group:   characters

Fields Indexed:  strength , name

and

Collection Group:  characters

Fields Indexed:  speed, date_created. 

So, is this just 2 composite indexes according to Firebase? Or is this multiplied by the number of Users I have?

If it is multiplied by the number of users, how should I restructure my data so that I do not run into this problem?

Thanks -

Jeff


Solution

  • After discussing with Sam Stern at Firebase,

    The answer is that the indexes are not multiplied by the number of users.

    From Sam ----

    Indexes actually on depend on the collection name so all of the "characters" subcollections of your "users" documents can share the same indexes.

    You'd only approach the 200 index limit if you had 200+ different field combinations you wanted to index. Hope that makes sense!

    • Sam