For those who have a Rail application with lots of complex connected models, what techniques would you employ to lessen database queries?

Actually, I'll extend that question just a little further and request, what you think "a lot ofInch queries for just about any page?

I've got a page which i expect will finish up striking the database about 20 occasions each page load. That concerns be try not to know whether or not this should concern me, or things i can perform to lessen the burden?

Take a look at: bullet

Its a terrific way to identify n+1 queries also it offers tips to minimize it.

It will decelerate development mode, so make sure to disable it when you're not performance tuning.

When we're in internet marketing, also checkout: rails_indexes

An easy method to identify which indexes your application might be missing.

Happy tuning.

One common practice is cautious utilisation of the include => :attribute option.

For example on the controller you may do:

def show
    @items = Item.find(:all) 

...and also the show view would make a move like:

<% @items.each |item| %>
    <%= item.some_attribute %>
<% end %>

This can produce a query for each call with a_attribute. But when you declare the attribute incorporated the following, you receive eagerly-loaded characteristics in a single query:

def show
    @items = Item.find(:all, :include => :some_attribute)

Of course, look at your console for query occasions and the like.

It really is hard to estimate a restriction for queries. This really is related in the concept/style of the application.

Without having to reload the entire page, It is best to consider javascript (or rjs) to be able to update just the data you'll need. This ought to be also an UI improvement, your customers will like it!

Look into the SQL produced out of your ActiveRecord queries. Make sure that things are like expected.

Envisage to denormalize your db to be able to improve performance. (be carefully)

This is exactly what I see in the "code side".

I'm useing :joins and :choose options if you want simply to display data. I discovered very helpful named_scope to define all possible :joins and something :choose_posts named_scope. Example

    class Activity < ActiveRecord::Base
      belongs_to :event
      belongs_to :html_template
      has_many :participants

      named_scope :join_event, :joins => :event
      named_scope :join_owner, :joins => {:event => :owner}
      named_scope :left_join_html_template, 
      :joins => "LEFT JOIN html_templates ON = activities.html_template_id"
      named_scope :select_columns, lambda { |columns| {:select => columns}}
      named_scope :order, lambda{ |order| {:order => order}}

So you can now easly build queries such as this:

columns = "activities.*,events.title,events.owner_id,owners.full_name as owner_name"
@activities = Activity.join_event.join_owner.order("date_from ASC").select_columns(columns)

I consider this isn't the very best and most secure way, however in my situation it truly minify query count that executes per request and you will find no errors rised about some wrong produced queries yet.