Rails database Query Solution for N + 1 queries

Source: Internet
Author: User

Schema.rb

Activerecord::schema.define (version:20150203032005) do create_table"addresses", Force:true do |t|T.integer"client_id"t.string"Street"t.string"Postcode"T.datetime"Created_at"T.datetime"Updated_at"End Create_table"Clients", Force:true do |t|t.string"name"t.string"Gender"T.datetime"Created_at"T.datetime"Updated_at"End Create_table"infos", Force:true do |t|T.integer"address_id"t.string" History"T.datetime"Created_at"T.datetime"Updated_at"EndEnd

Client.rb

class Client < activerecord::base  has_one:addressend

Address.rb

class Address < activerecord::base  belongs_to:client  has_many:infosend

Info.rb

class Address < activerecord::base  belongs_to:client  has_many:infosend

Client.all

  Client Load (0.4MS)  

Address.all

Info.all


  

Solution for N + 1 queries

Clients = Client.limit (2) Clients.each do |client|   Puts Client.address.postcodeend 

The resulting SQL statement is:

Select "Clients". * FROM "clients" = #<  

and

Clients = Client.includes (: address). Limit (2) Clients.each do |client|   Puts Client.address.postcodeend 

The resulting SQL statement is:

Client Load (0.5ms)  Select  "Clients". * FROM "clients"  LIMIT 2  Address Load (0.4ms)  Select " Addresses ". * from" addresses "  WHERE" addresses "." Client_id "in (1, 2  
Load multiple associations on demand
Clients =|client|     puts Client.address.infos.first.historyend

Generated SQL statement:

Address Load (0.3ms)  select  "Addresses". * from "addresses"  WHERE "addresses". " client_id "=? LIMIT 1  [["client_id", 1]]  Info Load (0.3ms)  select  "Infos". * from "infos"  WHERE "Infos". " address_id "=?  ORDER by "Infos". " ID "ASC LIMIT 1  [[" address_id ", 1]]1110  address Load (0.1ms)  select  " Addresses ". * FROM" addresses " C13/>where "addresses". " client_id "=? LIMIT 1  [["client_id", 2]]  Info Load (0.1ms)  select  "Infos". * from "infos"  WHERE "Infos". " address_id "=?  ORDER by "Infos". " ID "ASC LIMIT 1  

The client and address are has_one relationships, address and info are has_many relationships, and want to load resources once so that the client can get info via address using the following statement:

Clients = Client.includes (address:: infos)

Generated SQL statement:

Client Load (0.4ms)  Select "Clients". * FROM "clients"  Address Load (0.5ms)  Select "Addresses". * FROM " Addresses "  WHERE" addresses "." Client_id "in (1, 2)  Info Load (0.3ms)  select" Infos ". * FROM" infos "  

  

Clients.each do |c|     puts C.address.infos.first.historyend

Output:

1110
2110

Rails database Query Solution for N + 1 queries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.