Sum firebase data efficiently up

前端 未结 1 1075
臣服心动
臣服心动 2020-12-10 20:52

I have an app where I have up to a few thousand entries in a firebase table. Now someone is connecting to this table and I need to count and sum these items up.

         


        
相关标签:
1条回答
  • 2020-12-10 21:13

    The Firebase Database has no server-side aggregation operators, since they would inherently be at odds with the scalability and realtime nature of the database.

    That leaves you with two options to calculate aggregates:

    1. perform them client side as you already suggested

    2. keep a running aggregate that you update with every change

    Performing the aggregation client-side is a good option if the client already needs to display the data anyway. For example: if you're showing the list of users in a specific chat room, you can easily count the number of users client-side from the same data.

    But if you don't need the data client-side, then just downloading it to aggregate it is wasteful and will hurt scalability of your app. In such cases, an option is to keep an extra node that keeps the aggregate and update it with every relevant write operation. For example: if you want to show how many users have registered with your app, you could keep a global /user_count that you update whenever a user registers/unregisters. For this update, you'd typically use a transaction.

    0 讨论(0)
提交回复
热议问题