conradkoh
conradkoh14mo ago

Aggregating sums

Hi, I'm new here. Have a question regarding how aggregation is handled. Suppose I have a large number of transactions, and I would like to maintain the sum of all transactions (say expenses). Does convex handle this in a way that may run into the limits (e.g. each update may require scanning the entire history), or does it automatically recongize that for a new transaction, it can simply add to the last snapshot? Also, if I added the functionality for aggregation after there are already some transactions, will convex help to ensure that the data is correctly backfilled? Thanks!
3 Replies
erquhart
erquhart14mo ago
Welcome! Convex doesn't have direct solutions for aggregation, but it does enable a lot of aggregation use cases via transactionality. Since convex queries and mutations are transactions, you can update an aggregate field by adding to the existing value, no need to recalculate at any point. To backfill, you can paginate through, and maybe for each page total up all of the transactions and add that total to the aggregate field, wherever you're keeping it.
conradkoh
conradkohOP14mo ago
great! my bad, I saw the headings for "join" and "aggregation" in the doc and I assumed that it was supported directly. what you said makes sense! thank you.
erquhart
erquhart14mo ago
No problem!

Did you find this page helpful?