Multiple calculations for the same data set: ruby ​​or database?

I have a model Transactionfor which I need to display the results of many calculations in many fields for a subset transactions.

I have seen 2 ways to do this, but I'm not sure which is better. I am for the one that will have the least impact in terms of performance when increasing the data set and increasing the number of concurrent users.

data[:total_before] = Transaction.where(xxx).sum(:amount_before)
data[:total_after] = Transaction.where(xxx).sum(:amount_after)
...

or

transactions = Transaction.where(xxx)
data[:total_before]= transactions.inject(0) {|s, e| s + e.amount_before }
data[:total_after]= transactions.inject(0) {|s, e| s + e.amount_after }
...

edit: where clause is always the same.

Which one to choose? (or is there a third, better way?)

Thanks P.

+3
source share
2 answers

Do not cut, but what about

transactions = Transaction.where(xxx)
data[:total_before] = transactions.sum(:amount_before)
data[:total_after] = transactions.sum(:amount_before)

? 1 2:) sum.

PS , Rails Transaction.where(xxx), . , .

+4

, .

, , ?

  • DBM .
  • , .

, , . , DBM, - , .

, , , DBM , , , , . , , , .

, .

+4

Source: https://habr.com/ru/post/1782104/


All Articles