Rails里面的includes

Rails里面includes是比较常用的 依旧先码出背景

class LineItem < ActiveRecord::Base
  belongs_to :order, -> { includes :customer }
end 
class Order < ActiveRecord::Base
  belongs_to :customer
  has_many :line_items
end 
class Customer < ActiveRecord::Base
  has_many :orders, -> { includes :line_items }
end

关联 includes方法指定使用关联时要按需加载的间接关联

belongs_to

@line_item.order.customer 
如经常需要从 line_item上取到需要的 Customer 则这个 belongs_to :order 关联就有必要加上 includes

has_many & has_one

@customer.orders.line_items 
如果经常需要从 customer 上取到 line_items 则最后这个 has_many :orders 关联就有必要加上includes
没有加入includes时候的sql
2.3.0 :009 > l = LineItem.first
  LineItem Load (0.5ms)  SELECT  "line_items".* FROM "line_items"  ORDER BY "line_items"."id" ASC LIMIT 1
# {
            :id => 1,
      :order_id => 1,
    :created_at => Fri, 22 Jan 2016 14:02:57 UTC +00:00,
    :updated_at => Fri, 22 Jan 2016 14:10:19 UTC +00:00
}
2.3.0 :010 > l.order
  Order Load (0.4ms)  SELECT  "orders".* FROM "orders" WHERE "orders"."id" = $1 LIMIT 1  [["id", 1]]
# {
             :id => 1,
    :customer_id => 1,
     :created_at => Fri, 22 Jan 2016 14:07:43 UTC +00:00,
     :updated_at => Fri, 22 Jan 2016 14:07:43 UTC +00:00
}
2.3.0 :011 > l.order.customer
  Customer Load (0.4ms)  SELECT  "customers".* FROM "customers" WHERE "customers"."id" = $1 LIMIT 1  [["id", 1]]
# {
            :id => 1,
    :created_at => Fri, 22 Jan 2016 14:04:22 UTC +00:00,
    :updated_at => Fri, 22 Jan 2016 14:04:22 UTC +00:00
}
加入includes之后的sql
l= LineItem.first
  LineItem Load (0.7ms)  SELECT  "line_items".* FROM "line_items"  ORDER BY "line_items"."id" ASC LIMIT 1
# {
            :id => 1,
      :order_id => 1,
    :created_at => Fri, 22 Jan 2016 14:02:57 UTC +00:00,
    :updated_at => Fri, 22 Jan 2016 14:10:19 UTC +00:00
}
2.3.0 :002 > l.order
  Order Load (0.3ms)  SELECT  "orders".* FROM "orders" WHERE "orders"."id" = $1 LIMIT 1  [["id", 1]]
  Customer Load (2.5ms)  SELECT "customers".* FROM "customers" WHERE "customers"."id" IN (1)
# {
             :id => 1,
    :customer_id => 1,
     :created_at => Fri, 22 Jan 2016 14:07:43 UTC +00:00,
     :updated_at => Fri, 22 Jan 2016 14:07:43 UTC +00:00
}
2.3.0 :003 > l.order.customer
# {
            :id => 1,
    :created_at => Fri, 22 Jan 2016 14:04:22 UTC +00:00,
    :updated_at => Fri, 22 Jan 2016 14:04:22 UTC +00:00
}

查询

N+1 问题
# IN (1,7) 是因为有关联的order的id是 1和7
line_items = LineItem.includes(:order).limit(10)
  LineItem Load (0.5ms)  SELECT  "line_items".* FROM "line_items" LIMIT 10
  Order Load (0.3ms)  SELECT "orders".* FROM "orders" WHERE "orders"."id" IN (1, 7)

line_items.each do |l| #现在这里不会有sql产生
  l.order
end
一次性加载多个关联 (可以看到belongs_to 和 has_many的区别)
Order.includes(:customer, :line_items)
  Order Load (0.3ms)  SELECT "orders".* FROM "orders"
  Customer Load (0.3ms)  SELECT "customers".* FROM "customers" WHERE "customers"."id" IN (1, 2)
  LineItem Load (0.3ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."order_id" IN (1, 2, 4, 5, 6, 7, 3)
嵌套关联这里先不多说

includes的过滤(这里会牵扯到join)

  • left outer join
Order.includes(:line_items).where("line_items.id" => 5)
  SQL (0.5ms)  SELECT "orders"."id" AS t0_r0, "orders"."customer_id" AS t0_r1, "orders"."created_at" AS t0_r2, "orders"."updated_at" AS t0_r3, "line_items"."id" AS t1_r0, "line_items"."order_id" AS t1_r1, "line_items"."created_at" AS t1_r2, "line_items"."updated_at" AS t1_r3 FROM "orders"
 LEFT OUTER JOIN "line_items" ON "line_items"."order_id" = "orders"."id"
 WHERE "line_items"."id" = 5
[
    [0] # {
                 :id => 7,
        :customer_id => nil,
         :created_at => Fri, 22 Jan 2016 14:28:36 UTC +00:00,
         :updated_at => Fri, 22 Jan 2016 14:28:36 UTC +00:00
    }
]
  • inner join
Order.joins(:line_items).where("line_items.id > ?", 4).group("orders.id")
  Order Load (0.6ms)  SELECT "orders".* FROM "orders"
 INNER JOIN "line_items" ON "line_items"."order_id" = "orders"."id"
 WHERE (line_items.id > 4) GROUP BY orders.id
[
    [0] # {
                 :id => 5,
        :customer_id => nil,
         :created_at => Fri, 22 Jan 2016 14:28:35 UTC +00:00,
         :updated_at => Fri, 22 Jan 2016 14:28:35 UTC +00:00
    },
    [1] # {
                 :id => 7,
        :customer_id => nil,
         :created_at => Fri, 22 Jan 2016 14:28:36 UTC +00:00,
         :updated_at => Fri, 22 Jan 2016 14:28:36 UTC +00:00
    }
]

你可能感兴趣的:(Rails里面的includes)