Home > OS >  Flutter Firestore Composite Index with variable item not realistic?
Flutter Firestore Composite Index with variable item not realistic?

Time:09-01

I just created a StreamBuilder that has this stream:

      stream: db
          .collection('GearLockerItems')
          .where('inPack.$packID', isEqualTo: true)
          .orderBy('itemName')
          .snapshots(),

When I ran the code, it didn't work at first, and when I checked the debug console. I saw a link I had to click and then it went and build a composite index and it worked!

But.... the composite index if for that specific variable $packID which means everytime I click on a new "pack" in my app to get this list, it wont work until I manually click to create the index....

This doesn't seem intuitive fro an app that lots of user could use, I can't manually index each item. Is there anyway around this or do I need to rething my entire db?

CodePudding user response:

One way to solve this is to flatten the 'inPack.$packID' to be part of the document fields rather than inside an object.

Another is to list all possible options and create indexes for them, takes some time yes but gets the job done. I would not follow this approach as it will be prune to future errors if new packs are created and an index is not created for those. unless if you create a cloud function to automate the indexes creation. But again too many indexes will increase the size and will be counterproductive to "write operations" performance.

Also, take a look at this question as it is relevant to your case.

  • Related