Common performance issues

Hi,

When writing a Rails app, what common tasks can likely result in
performance issues?

The one I’m aware of is something like:
Model.find(:all).each do |elem|

do stuff

end

Especially when there’s lots of elements in the Model. What other
ones are there?

Also, does Rails do caching of queries? If I’m in a view and I do:
<% if user.admin? %> …

and then later on in that same view, I have the same code again, does
that execute two queries?

Joe

[email protected] wrote:

Especially when there’s lots of elements in the Model. What other
ones are there?

Also, does Rails do caching of queries? If I’m in a view and I do:
<% if user.admin? %> …

and then later on in that same view, I have the same code again, does
that execute two queries?

Joe

Another one:
What’s a faster way to write:

class User < AR
has_many :comments
def number_of_comments
comments.size
end
end

(if I don’t want to do caching). I assume that calling
number_of_comments creates AR objects for each comment.

Joe-

You may find it useful to use “tail” to monitor the file
development.logwhile you are developing - each SQL query that Rails
executes will show up
there. I believe (though you should double check this) that calling
comments.size when comments has not been loaded will do a single “select
count(*)…” query without instantiating a bunch of Comment objects.

-Steve H.

Steve H. wrote:

Joe-

You may find it useful to use “tail” to monitor the file
development.logwhile you are developing - each SQL query that Rails
executes will show up
there. I believe (though you should double check this) that calling
comments.size when comments has not been loaded will do a single “select
count(*)…” query without instantiating a bunch of Comment objects.

I thought that, when in development mode, Rails generates different SQL
(i.e. more SQL queries) than when in production mode.

Is this not the case?

Joe

On Tuesday 19 September 2006 21:34, [email protected] wrote:

I thought that, when in development mode, Rails generates different
SQL (i.e. more SQL queries) than when in production mode.

Is this not the case?

Definitely not.

Regarding your original question, have a look at the :counter_cache
option for associations.

Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/

[email protected] wrote:

Hi,

When writing a Rails app, what common tasks can likely result in
performance issues?

I found this article: A Look at Common Performance Problems in Rails

May be useful to others out there.

In my applications, the logs seem to indicate that the “rendering”
part, and not the “database” part, is the bottleneck. Is ActiveRecord
work included in the database figure?

Another one: I’ve noticed that, if I have a couple thousand <%= … %>
on a page, it slows down considerably. Would it be worth a try to
build up strings of html using <% str += “more” %> and then <%= str %>?

Joe

if rendering is your bottleneck, you should check this out:
http://www.railsexpress.de/blog/articles/2006/08/15/rails-template-optimizer-beta-test

ed

On 9/19/06, [email protected] [email protected] wrote:

May be useful to others out there.


Ed Hickey
Developer
Litmus Media
816-533-0409
[email protected]
A Member of Think Partnership, Inc
www.ThinkPartnership.com
Amex ticker symbol: THK

Hi

the ruby Net::HTTP library allows calls like:

Net::HTTP.get(url.path)

end similar looking post calls,
Since the RESTful support uses
DELETE and PUT, i was wondering how to make ruby calls
like the above Net::HTTP of the kind DELETE and PUT?

Thanks in advance!
Gustav

joevandyk wrote:

When writing a Rails app, what common tasks can likely result in
performance issues?

I noticed in the Log folder that many SELECT statements had LIMIT 1 on
the end.

We all know that’s a no-no when you are architecting a database. It’s
mostly harmless when using it…

…however, I would have relied on assertions demanding that all select
statements that only need 1 row return will only get 1 row. Without
LIMIT 1. That would imply that the database is sufficiently normal, and
the SELECT statements are sufficiently constrained.

So, in general, the ideal might be to transfer limit-checking to the
tests, so the code can run faster without it, and with proper
constraints.

(The spectre of SQL data normalization and query optimization also
raises it’s ugly head. We might presume that the slowest DB operation
is still faster than the fastest Ruby page rendering cycle, no? :wink: