views:

298

answers:

5

Our company loves reports that calculate obscure metrics--metrics that cannot be calculated with ActiveRecord's finders (except find_by_sql) and where ruport's ruby-based capabilities are just too slow.

Is there a plugin or gem or db adapter out there that will do large calculations in the database layer? What's your solution to creating intricate reports?

+1  A: 

Thoughtbot's Squirrel plugin adds a lot of Ruby-ish functionality to ActiveRecord's find method, with multi-layered conditionals, ranges, and nested model associations:

www.thoughtbot.com/projects/squirrel/

Arrel
Excellent idea! We've looked at searchlogic and squirrel for generating 'where' statements more effectively, but the problem we're encountering is with calculating values between multiple fields in one query... I'm not sure if these will help in this particular use case. But let me know if they do!
btelles
+1  A: 

Is there anything inherent about your reports that prevents the use of an SQL view or stored procedure?

In one particular project, a technique I often find useful is to create your SQL query (that may be quite complex) as a named view in the database, and then use

 YourModel.connection.select_all(query)

to pull back the data. It's not an optimal approach; I'm keen to explore improvements to it.

Unfortunately, as you suggested, the support for doing computing complex database-based reports within rails seems fairly limited.

Lee
Yes, our shop has a SQL guru who would really like that option. And it seems like a pragmatic approach... I have no problem with that, save one...it's a little difficult to maintain db agnosticism when I create migrations that include stored procedures that are specific to a database type. Views are definitely up for grabs though! Thanks for the input, and sorry for the late response!
btelles
+1  A: 

Although not database agnostic, our solution is plpgsql functions where it becomes really slow to use Ruby and ActiveRecord.

Omar Qureshi
Yeah, we're really trying to stick with something that's db agnostic.Thanks for the input though!
btelles
+1  A: 

You might want to look at using DataMapper or Sequel for your ORM if you're finding that ActiveRecord lacks the expressiveness you need for complex queries. Switching away from ActiveRecord wouldn't be a decision to take likely, but it might be worth investigating at least.

Pete Hodgson
Although right now it's a little too late in our project to do this, I believe this will be our choice moving forward. I haven't looked much into DataMapper, but have heard many great stories about it. Thanks Pete!
btelles
+1  A: 

It sounds as if your tables could be normalized. At one place I worked, the amount of normalization we did was impacting our reporting needs, so we created some shadow tables that contained a bunch of the aggregate data, and did reporting against that.

I agree with Neil N's comment that the question is a little vague, but perhaps this gets you moving in the right direction?

Tony Collen
Yes, the question is definitely vague. But data normalization would solve a slightly different problem, and further normalizing our data would actually probably slow down queries a little more.Thanks for contributing though!
btelles
What I meant was, consider de-normalizing for reporting purposes.
Tony Collen