-1

I have a system that lets users entering financial transactions including dollar amounts. Once transaction is added\updated or removed from the system the account total has to be re-calculated and displayed to the user instantly. Users have access to certain accounts in the system but not all.

Below is a link to the screenshot of the tables that hold account and transaction data.

http://i.imgur.com/EqV7w.png

Currently in order to get the account total I use the following query:

select sum(t.Amount) 
from [Transaction] t
join [Account] a on a.AccountId=t.AccountId
where a.AccountId=@AccountId

I have non-unique non-clustered index on the Transaction table on AccountId and Amount columns which allows the query to quickly find transactions by AccountId.

It does what I want but my concern is that Transaction table is growing pretty fast, currently I have around 1 million records there and over time I expect it to reach tens of millions. Also the number of users that can actively edit transactions is growing as well, I have around 5500 of them in the system.

I am looking into a solution to limit the use of Transaction table when getting the total $ for the account so the system can scale out. I have 2 solutions for that:

  1. Calculate account total on demand by asking users to click on a certain button when they need that information. I might go that route but I want to still explore my options with the real time update of the account total.
  2. Store a running total some place else and use it to calculate account total. There is a limitation that is associated with it as everything has to be done through a single interface that would know about all these nuances. Editing transactions in the database would be tricky since you have to make sure the running total is updated as well.

I am looking for other alternatives to calculating account total in real time without worrying about database performance.

4

2 回答 2

0

我的建议是仅当您遇到性能问题时才使用最后一种解决方案。
处理大量数据是数据库的工作,因此,如果您的数据结构正确,即使有数千行,SQL 查询也能很好地执行。
所以,首先尝试继续这样。如果遇到性能问题,请尝试寻找解决方案。

如果您决定使用保留计算的 Total 字段的解决方案,以规避更新数据而不更新 Total 字段的风险,您应该使用触发器:每次添加、删除或更新一行时,您应该添加或减去总值。

这里有一些关于如何创建触发器的帮助。

于 2011-11-23T17:22:46.170 回答
0

即使表很大,accountId 上的索引也会以有效的方式将查询中涉及的行数减少到相对较少的行数。

如果您有很多这种类型的查询并且可以忍受摘要不如实际事务新鲜,您可以考虑设置一个单独的数据库来报告您可以在其中非规范化、构建摘要等。然后您可以创建作业以定期更新这些表(这是“数据仓库”背后的基本思想——你可能不需要完整的版本,但同样的想法也适用)。

于 2011-11-24T04:56:54.457 回答