Friday, September 10, 2010

[Rails] Re: when is it okay to resort to find_by_sql?

Marnen Laibow-Koser wrote:

>>> There are relatively few cases (not none, but few) where a subquery
>>> should not be replaced by a join. Are you sure you need that subquery?

Marnen: I owe your a debt of gratitude. Your note spurred me to
*really* grok SQL joins. After thirty six hours of study and
anti-social behavior, I've got it. I think I'll write a blog post
called "The Zen of SQL JOIN: a gentle guide for procedural programmers".
(Teaser: Use JOINs to generate cartesian products of tables. Use ON,
WHERE, and HAVING to filter the combinatorial explosion to only those
rows that you want.)

And you are absolutely right: subqueries can (always? often?) be
replaced by studlier joins. In my system, I've re-cast what was
previously a four-level (!!) nested query with a "flat" join across
seven tables. And it now runs like a bat out of hell.

I *STILL* don't know how to ask the ActiveRecord query system to alias a
table in a join, as in:
...
INNER JOIN vertices AS v1 ON v1.id = edge.v1_id
INNER JOIN vertices AS v2 ON v2.id = edge.v2_id
...
so I'm still using find_by_sql(). If someone can suggest a clean
ActiveRecord syntax for this kind of query, I'm all ears.

- ff
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate