如何优化Firestore缓存和查询模型 [英] How to optimize Firestore Caching and Querymodeling

查看:53
本文介绍了如何优化Firestore缓存和查询模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Moneymanagement-App,用户可以在其中创建每天的转账.

I am making a Moneymanagement-App where the user can create Transfers for each day.

我目前正在主屏幕上列出所有数据.目前,这并不重要,因为没有太多数据,但可以想象一个使用该应用程序数年并跟踪其所有支出的用户.

I am currently listing all the data on the mainscreen. At the moment that doesn't matter because there isn't much data but imagine a user who uses the app several years and tracking all his spendings.

我的第一个想法是为该用户缓存所有可用数据,但这会导致过多不必要的读取,因为该用户很可能不需要5年前的数据.

My first thought was to cache all the available Data for that user but that would cause too many unnecessary reads because the user most likely won't need the data from lets say 5 years ago.

所以我认为解决方案是仅对该屏幕实施分页.

So I thought the solution would be to just implement pagination for that screen.

但是:

通过选择类别和时间段,用户可以在另一个屏幕上获得有关其消费历史的统计信息. 目前,我每次更改这些参数时都会对它们进行查询,但这显然也会导致很多不必要的读取.

The user can get statistics about his spendinghistory on another screen by selecting a category and a timeperiod. Currently i am running a query on those parameters each time they change but this will obviously also lead to a lot of unnecessary reads.

所以问题是,如果用户选择从5年前获取统计信息,则数据将不存在于缓存中,因此我仍然必须在该时间段内运行查询,然后以不完整的缓存结束那段时间是因为我仅基于查询获得了一些数据.

So the problem is, if the user chooses to get statistics from 5 years ago, that Data wouldn't exist in the cache so i would still have to run a query for this time period and then end up with a incomplete cache of that period because i only got some of the Data based on the Query.

很想听听您对此的想法.您将如何处理?

Would love to hear your thoughts on this. How would you handle it ?

推荐答案

通常:不要按需运行来自客户端的聚合查询.而是将聚合数据存储在数据库中,并在写入数据时对其进行更新.

In general: don't run aggregation queries from the client on demand. Instead store aggregated data in the database, and update it as data is written.

所以说,您保留一些年度总计,例如,它们在年初和年末的余额,其当年的总收入和支出,可能按类别细分.这就是您每年可以在文档中输入的所有信息.

So say that you keep some annual totals, such as their balance at the start and end of the year, their total income and spend for that year, probably broken down by categories. That is all information that you could put in a document for each year.

您将具有结构/users/$uid/totals/$year,然后在该文档的字段中具有总计.每次您编写新交易时,都会更新该用户在当年的总计文档.

You'd have a structure /users/$uid/totals/$year and you then have the totals in fields in that document. Every time you write a new transaction, you update the totals document for that user for the current year.

如果执行此操作,则只需要阅读总计文档即可显示总计,并且如果要显示单个交易,则只需要阅读单个交易即可.

If you do this, you'll only need to read the totals document to show totals, and you'll only need to read individual transactions if you want to show individual transactions.

另请参阅:是否可以在Firestore中运行聚合查询?

这篇关于如何优化Firestore缓存和查询模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆