简体   繁体   English

如何提高 Firebase 实时数据库的性能

[英]How to improve performance in Firebase real time database

We are developing an app in dart wherein we need to fetch around more than 50K rows at once (doing it when app loads) and then data will be used in other sections of app for further calculations.我们正在 dart 中开发一个应用程序,其中我们需要一次获取超过 50K 行(在应用程序加载时执行),然后数据将用于应用程序的其他部分以进行进一步计算。 We are using Firebase Realtime database and we are facing some serious performance issues.我们正在使用 Firebase 实时数据库,我们正面临一些严重的性能问题。

Its currently taking somewhere around 40 seconds to load 50K rows(currently using free database version, not sure if that would the reason), but we have also observed that when multiple users uses the app, it starts to take around 1 minute 20 sec to load 50K rows and Peak goes to 100%.目前加载 50K 行大约需要 40 秒(当前使用免费数据库版本,不确定是否是这个原因),但我们也观察到,当多个用户使用该应用程序时,开始需要大约 1 分 20 秒加载 50K 行,峰值达到 100%。

Can you please suggest how can we improve performance in firebase realtime database?您能否建议我们如何提高 firebase 实时数据库的性能?

If I break the data in two collection but keep it in same JSON file, would that help?如果我打破两个集合中的数据但将其保存在同一个 JSON 文件中,这会有帮助吗?

Can it be because we are using currently free database version for testing?可能是因为我们正在使用当前免费的数据库版本进行测试吗?

We have tried creating indexes in "Rules" section on 1 Key field but that did not help much.我们尝试在 1 Key 字段的“规则”部分创建索引,但这并没有太大帮助。 Is there any way we can improve this?我们有什么办法可以改善这一点吗?

Can it be because we are using currently free database version for testing? 可能是因为我们正在使用当前免费的数据库版本进行测试吗?

All Firebase Realtime Database instances run on the same infrastructure. 所有Firebase实时数据库实例均在同一基础结构上运行。 There is no difference based on the plan your project is on. 根据您的项目所依据的计划,没有任何区别。

Can you please suggest how can we improve performance in firebase realtime database? 您能否建议我们如何提高Firebase实时数据库的性能?

The best way to improve performance is to only load data that you're going to show to the user right away. 改善性能的最好方法是仅加载要立即显示给用户的数据。 In any client-side application it's unlikely that the user will look at 50K items, let alone look at them straight when the application starts. 在任何客户端应用程序中,用户不太可能查看5万个项目,更不用说在应用程序启动时直接看它们了。

If you need 50K items to show the initial data to the user, that typically means that you're aggregating that data in some way, and showing that aggregate to the user. 如果您需要5万个项目来向用户显示初始数据,那通常意味着您正在以某种方式聚合该数据,并向用户显示该聚合。 Consider doing that aggregation when you write the data to the database, and store the aggregation result in the database. 将数据写入数据库时,请考虑进行聚合,并将聚合结果存储在数据库中。 Then you can load just that result in each client, instead of having each client do its own aggregation. 然后,您可以仅在每个客户端中加载该结果,而不是让每个客户端进行自己的聚合。

For more data modeling tips, read NoSQL data modeling and watch Firebase for SQL developers . 有关更多数据建模技巧,请阅读NoSQL数据建模,并观看SQL开发人员的Firebase I'd also recommend watching Getting to know Cloud Firestore , which is for Cloud Firestore, but contains many great tips that apply to all NoSQL databases. 我还建议您观看“ 了解Cloud Firestore” (适用于Cloud Firestore),但其中包含许多适用于所有NoSQL数据库的重要技巧。

Add the field that you are using for the query to the Realtime Database rules.将您用于查询的field添加到实时数据库规则。

For example例如

{
  "rules": {
    "v1": {
            "history": {
                ".indexOn": "refresh_at"
            }   
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM