1

includesidentity_map がオフになっているときのeager-loading()の動作を理解しようとしています。

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 が有効な場合にのみ機能するはずです。しかし、私の場合、Mongoid ライブラリは、identity_map が無効になっている場合でも、含まれているアソシエーションを熱心にロードしようとします。これにより、関連付けが後でアクセスされるときに、データベースへの冗長な呼び出しが発生します。

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