1

我试图了解includesidentity_map 关闭时的急切加载()行为。

class Account
  has_and_belongs_to_many :subscriptions
end

a = Account.includes(:subscriptions).first # makes first two calls to the database
a.subscriptions # returns nothing
a.subscriptions.map(&:name) # makes the 3rd call to the database.

根据文档includes选择器应该仅在启用 identity_map 时才起作用。但在我的情况下,即使 identity_map 被禁用,Mongoid 库也会尝试预先加载包含的关联。当稍后访问关联时,这会导致对数据库的冗余调用。

MOPED: yyyy:10019 QUERY database=xxxx collection=accounts 
  selector={"$query"=>{}, "$orderby"=>{:_id=>1}} 
  flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (148.0079ms)

MOPED: yyyy:10019 QUERY database=xxxx collection=subscriptions 
  selector={"_id"=>{"$in"=>["50eaad6b6f758a601a000017", "50eaad7e6f758a601a000036"]}} 
  flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (573.0329ms)

MOPED: yyyy:10019 QUERY database=xxxx collection=subscriptions 
  selector={"$and"=>[{"_id"=>{"$in"=>["50eaad6b6f758a601a000017", 
  "50eaad7e6f758a601a000036"]}}]} 
  flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (586.0329ms)
4

0 回答 0