管理数据库中不断变化的数据

时间:2022-09-11 14:58:43

I need some advice on how to architect my data in monogoDB. I have this app, where users can view, add, edit and remove credit and debit transactions. Below is how the data looks. 管理数据库中不断变化的数据

我需要一些关于如何在monogoDB中构建数据的建议。我有这个应用,用户可以查看、添加、编辑和删除信用卡和借记卡交易。下面是数据的外观。

The balance column here is dynamic. For example if someone adds a transaction dates 10-09-2017, all the amount in the balance field thereafter needs to change in that moment to reflect the new transaction. Right now, I am not saving this balance field at all in the database and is calculating it every time when the user loads the page, reloads it, and also when editing, deleting, adding a transaction. Now it is fast, but I assume, in the future, when the user has a lot of transactions, they will become slow as these calculations needs to be done before the user is displayed the data table. Is there a more efficient way to do this?

这里的平衡列是动态的。例如,如果某人在10-09-2017年添加了一个事务,那么之后需要更改balance字段中的所有金额,以反映新的事务。现在,我并没有在数据库中保存这个balance字段,而是在每次用户加载页面、重新加载页面以及编辑、删除和添加事务时进行计算。现在它很快,但是我假设,将来,当用户有很多事务时,它们会变得很慢,因为这些计算需要在用户显示数据表之前完成。有更有效的方法吗?

Also I am doing the calculations on the client side, so the load is on the client's device and not on server. I think if it is on server side, and a lot of users start using it, the API requests will become much slower and not unusable at all after a while. Is this the right way?

我还在客户端进行计算,所以负载是在客户端设备上,而不是服务器上。我认为,如果它是在服务器端,并且很多用户开始使用它,那么API请求将会变得非常慢,并且在一段时间之后也不会失效。这条路对吗?

PS : Also it was hard making sure the reader understand my questions but I have tried my best. Please let me know if I should explain this in more details or if I should add any more details.

另外,要确保读者理解我的问题也很困难,但我已经尽力了。请让我知道我是否应该解释更多的细节,或者我是否应该增加更多的细节。

1 个解决方案

#1


2  

It is not a question about mongodb, it is a question about user interface. Will you really display the whole history of transactions at once? You should either utilize pagination (simplest) or reload on scroll to load your data.

这不是mongodb的问题,而是用户界面的问题。你真的会同时显示整个交易的历史吗?您应该要么使用分页(最简单),要么在滚动上重新加载数据。

Before you get problems because of the balance cell calculation, it is more likely that you experience problems because of:

在你因为平衡计算而出现问题之前,你更有可能因为:

  • Slow loading from network (almost certainly)
  • 网络加载缓慢(几乎可以肯定)
  • Slow page interaction because of DOM size (maybe)
  • 由于DOM大小(可能),页面交互速度慢

Show the first 100 to 500 transactions and provider the user with some way to load earlier entries.

显示前100到500个事务,并提供用户以某种方式加载早期条目。

Update - Regarding server-side balance calculation:

更新-关于服务器端平衡计算:

You could calculate balance on server-side and store it into a second collection which serves as a cache. If a transaction insertion happens in the past, you recalculate the cache. To speed this up, you can utilize snapshots: Within a third collection, you could store the current balance in certain intervals, e.g. with the following data structure:

您可以在服务器端计算平衡,并将其存储到作为缓存的第二个集合中。如果在过去发生事务插入,则重新计算缓存。为了加快速度,您可以使用快照:在第三个集合中,您可以以一定的间隔存储当前余额,例如,使用以下数据结构:

{ Balance: 150000, Date: 2017-02-03, LastTransactionId: 546 } 

When a transaction is inserted in the past, take the most recent snapshot before that past moment and recalculate the cache based on that. This way, you. can keep the number of recalculated transactions pretty small.

当在过去插入一个事务时,在过去的时刻之前获取最近的快照,并基于此重新计算缓存。这种方式,你。可以保持重新计算的事务的数量相当小。

#1


2  

It is not a question about mongodb, it is a question about user interface. Will you really display the whole history of transactions at once? You should either utilize pagination (simplest) or reload on scroll to load your data.

这不是mongodb的问题,而是用户界面的问题。你真的会同时显示整个交易的历史吗?您应该要么使用分页(最简单),要么在滚动上重新加载数据。

Before you get problems because of the balance cell calculation, it is more likely that you experience problems because of:

在你因为平衡计算而出现问题之前,你更有可能因为:

  • Slow loading from network (almost certainly)
  • 网络加载缓慢(几乎可以肯定)
  • Slow page interaction because of DOM size (maybe)
  • 由于DOM大小(可能),页面交互速度慢

Show the first 100 to 500 transactions and provider the user with some way to load earlier entries.

显示前100到500个事务,并提供用户以某种方式加载早期条目。

Update - Regarding server-side balance calculation:

更新-关于服务器端平衡计算:

You could calculate balance on server-side and store it into a second collection which serves as a cache. If a transaction insertion happens in the past, you recalculate the cache. To speed this up, you can utilize snapshots: Within a third collection, you could store the current balance in certain intervals, e.g. with the following data structure:

您可以在服务器端计算平衡,并将其存储到作为缓存的第二个集合中。如果在过去发生事务插入,则重新计算缓存。为了加快速度,您可以使用快照:在第三个集合中,您可以以一定的间隔存储当前余额,例如,使用以下数据结构:

{ Balance: 150000, Date: 2017-02-03, LastTransactionId: 546 } 

When a transaction is inserted in the past, take the most recent snapshot before that past moment and recalculate the cache based on that. This way, you. can keep the number of recalculated transactions pretty small.

当在过去插入一个事务时,在过去的时刻之前获取最近的快照,并基于此重新计算缓存。这种方式,你。可以保持重新计算的事务的数量相当小。