views:

309

answers:

1

I'm doing an ActiveRecord find operation (via the "will_paginate" library) like this:

  contacts = Contact.paginate(:all,
    :conditions => conditions_specified,
    :select => "DISTINCT contacts.*",
    :joins => joins,
    :include => [:addresses, :emails, :phone_numbers, {:addresses => :organization}],
    :page => page,
    :per_page => @@PAGE_SIZE,
    :order => 'last_name, first_name, middle_name')

In the console, I get the output shown below. By setting the include parameter, I expect this to result in only one query. But in the output I'm seeing many queries. For example, I'm surprised to see the line...

  Address Load (2.5ms)   SELECT `addresses`.* FROM `addresses` WHERE (`addresses`.contact_id IN (94,106,104,108,121,55,40,75,62,4,67,36,77,64,7,35,24,68,57,117,8,30,85))

in the output below. (I understand that the SHOW FIELDS FROM queries are cached in production so that's not a problem. It's all the extra SELECTS I'm trying to figure out.)

Processing ContactController#index (for 127.0.0.1 at 2009-01-03 21:21:18) [GET]
  Session ID: d453897c2f67c29c9a68d3fbc7b94f7b
  ContactSearch Columns (4.4ms)   SHOW FIELDS FROM `contact_searches`
  Contact Load (1.1ms)   SELECT DISTINCT contacts.* FROM `contacts` WHERE (contacts.last_name LIKE '%n%') ORDER BY last_name, first_name, middle_name LIMIT 0, 35
  Contact Columns (2.3ms)   SHOW FIELDS FROM `contacts`
  Address Load (2.5ms)   SELECT `addresses`.* FROM `addresses` WHERE (`addresses`.contact_id IN (94,106,104,108,121,55,40,75,62,4,67,36,77,64,7,35,24,68,57,117,8,30,85))
  Address Columns (2.7ms)   SHOW FIELDS FROM `addresses`
  Email Load (1.7ms)   SELECT `emails`.* FROM `emails` WHERE (`emails`.contact_id IN (94,106,104,108,121,55,40,75,62,4,67,36,77,64,7,35,24,68,57,117,8,30,85))
  Email Columns (1.8ms)   SHOW FIELDS FROM `emails`
  PhoneNumber Load (3.3ms)   SELECT `phone_numbers`.* FROM `phone_numbers` WHERE (`phone_numbers`.contact_id IN (94,106,104,108,121,55,40,75,62,4,67,36,77,64,7,35,24,68,57,117,8,30,85))
  PhoneNumber Columns (3.0ms)   SHOW FIELDS FROM `phone_numbers`
  Organization Columns (2.9ms)   SHOW FIELDS FROM `organizations`
  Organization Load (0.9ms)   SELECT * FROM `organizations` WHERE (`organizations`.`id` IN (99,116,44,6,23,78,107,91,79,119,14,120,71,26,59,94,27,100,62,1,51,29,30,2,65,76,5))
Rendering template within layouts/main
Rendering contact/index
Rendered contact/_field_function_specifier (0.9ms)
Rendered contact/_field_function_specifier (0.6ms)
Rendered contact/_field_function_specifier (0.6ms)
Rendered contact/_field_function_specifier (0.6ms)
Rendered contact/_field_function_specifier (0.6ms)
Rendered contact/_field_function_specifier (0.6ms)
Rendered contact/_field_function_specifier (0.6ms)
Rendered contact/_field_function_specifier (0.6ms)
Rendered contact/_field_function_specifier (0.6ms)
Rendered contact/_field_function_specifier (0.6ms)
Rendered contact/_search_form (14.1ms)
Rendered contact/_quick_view_contact_info (3.2ms)
Rendered contact/_quick_view_contact_info (2.1ms)
Rendered contact/_quick_view_contact_info (1.9ms)
Rendered contact/_quick_view_contact_info (1.9ms)
Rendered contact/_quick_view_contact_info (0.8ms)
Rendered contact/_quick_view_contact_info (2.0ms)
Rendered contact/_quick_view_contact_info (2.4ms)
Rendered contact/_quick_view_contact_info (1.7ms)
Rendered contact/_quick_view_contact_info (1.4ms)
Rendered contact/_quick_view_contact_info (1.7ms)
Rendered contact/_quick_view_contact_info (2.2ms)
Rendered contact/_quick_view_contact_info (1.5ms)
Rendered contact/_quick_view_contact_info (2.0ms)
Rendered contact/_quick_view_contact_info (1.7ms)
Rendered contact/_quick_view_contact_info (1.8ms)
Rendered contact/_quick_view_contact_info (3.6ms)
Rendered contact/_quick_view_contact_info (2.2ms)
Rendered contact/_quick_view_contact_info (2.2ms)
Rendered contact/_quick_view_contact_info (1.9ms)
Rendered contact/_quick_view_contact_info (1.7ms)
Rendered contact/_quick_view_contact_info (2.0ms)
Rendered contact/_quick_view_contact_info (2.1ms)
Rendered contact/_quick_view_contact_info (1.9ms)
Completed in 308ms (View: 104, DB: 27) | 200 OK [http://localhost/contact]
+4  A: 

I did a little more research and found the answer. I'm updating this for those who search later. The long answer is in this blog post.

It appears that when you do a find that has both joins and a limit, ActiveRecord has to first query the main table to get the IDs of the rows to return, then redo the query using just the IDs in order to apply the limit.

Since I was using will_paginate, that's what happened as will_paginate used limited queries to do the pagination.

Ethan