3

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

I've seen 2 ways to do it, but am not sure which is the best. I'm after the one that will have the least impact in terms of performance when data set grows and number of concurrent users increases.

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: the where clause is always the same.

Which one should I choose? (or is there a 3rd, better way?)

Thanks, P.

1
  • 2
    Side note: array.inject(0) {|s, e| s + e.amount_before } -> array.sum(&:amount_before). But that's for normal enumerables, for AR is sum(:amount_before) as Nikita says.
    – tokland
    Dec 25, 2010 at 18:44

2 Answers 2

4

Not to nag, but what about

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

? This looks like the union of strengths of methods 1 and 2 :) You reuse search results and employ more clean rails-specific sum aggregator.

PS If you were asking whether it's possible to rely on Rails in caching results of Transaction.where(xxx) query, that I don't know. And when I don't know, I prefer to play safe.

1
  • Just so you know, yes you can rely on rails query caching. It is a part of well-document public API. However, you should not do so as to not repeat yourself twice.
    – glebm
    Dec 26, 2010 at 9:54
4

Really you're talking about scalability.

If you're talking about millions of rows and needing to do calculations on them, then which do you think would be faster?

  1. Asking the DBM to summarize millions of rows and return you two numbers.
  2. Returning millions of query results across the network which you iterate over twice.

In the first scenario you can scale up your DB host with faster CPUs, more RAM, faster drives or pre-compute your values at regular intervals. The calculations you want done in the DBM are exactly the sort of things it's written to do.

In the second scenario you have to scale up your computing host, and maybe the switch connecting the DBM and computing host, plus maybe the database host because it will have to retrieve and push the data. Imagine the impact on the network as it's handling the data, and the impact on the computing host's CPU as it's doing everything.

I'd do the first one as it seems a lot more scalable to me.

4
  • I think you misunderstand the first example. It doesn't delegate any processing to database, it's just a shorthand for inject version: api.rubyonrails.org/classes/Enumerable.html#method-i-sum Rails isn't so smart as you think :) Dec 25, 2010 at 22:17
  • Or may be I misunderstand where? Dec 25, 2010 at 22:27
  • The first example will require at least one database hit. If the undefined where clause is identical, which we don't know, then the sum could be written to grab the summed value for several columns. Either way, it is a lot more efficient than asking the database to return all matching records and processing it in the calling code, which is why I said it's a scalability issue, and determining that pretty much answers the OPs question. Dec 25, 2010 at 23:58
  • the where clause is indeed always the same (I've edited the question). And I'm not talking millions of rows either... merely a few tens of thousands.
    – Pierre
    Dec 26, 2010 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.