Rails 使用 Mongoid 时对 Criteria 的每一次引用都会触发一次数据库查询?

rbprocareer · 2015年07月20日 · 2194 次阅读
drawings = Drawing.published.includes(:user).limit(10)
drawing_ids = drawings.map(&:id)
drawing_user_ids = drawings.map { |drawing| drawing.user.id }

logs:

Started GET "/api/hot" for 127.0.0.1 at 2015-07-20 10:55:18 +0800
  MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 0.7450ms
  MOPED: 127.0.0.1:27017 QUERY        database=ki_backend_development collection=drawings selector={"published"=>true} flags=[] limit=10 skip=0 batch_size=nil fields=nil runtime: 1.4050ms
  MOPED: 127.0.0.1:27017 KILL_CURSORS cursor_ids=[40708006394] runtime: 0.1960ms
  MOPED: 127.0.0.1:27017 QUERY        database=ki_backend_development collection=users selector={"_id"=>{"$in"=>[3, 7, 10, 9, 4, 16, 11]}} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 1.5960ms
QUERY CACHE  database=ki_backend_development collection=drawings selector={"published"=>true}
  MOPED: 127.0.0.1:27017 QUERY        database=ki_backend_development collection=users selector={"$query"=>{"_id"=>3}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.5010ms
  MOPED: 127.0.0.1:27017 QUERY        database=ki_backend_development collection=users selector={"$query"=>{"_id"=>7}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 1.1690ms
  MOPED: 127.0.0.1:27017 QUERY        database=ki_backend_development collection=users selector={"$query"=>{"_id"=>10}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.5130ms
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>7}, "$orderby"=>{:_id=>1}}
  MOPED: 127.0.0.1:27017 QUERY        database=ki_backend_development collection=users selector={"$query"=>{"_id"=>9}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.8260ms
  MOPED: 127.0.0.1:27017 QUERY        database=ki_backend_development collection=users selector={"$query"=>{"_id"=>4}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 1.6400ms
  MOPED: 127.0.0.1:27017 QUERY        database=ki_backend_development collection=users selector={"$query"=>{"_id"=>16}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.3710ms
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>4}, "$orderby"=>{:_id=>1}}
  MOPED: 127.0.0.1:27017 QUERY        database=ki_backend_development collection=users selector={"$query"=>{"_id"=>11}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.3980ms
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>16}, "$orderby"=>{:_id=>1}}
QUERY CACHE  database=ki_backend_development collection=drawings selector={"published"=>true}
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>3}, "$orderby"=>{:_id=>1}}
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>7}, "$orderby"=>{:_id=>1}}
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>10}, "$orderby"=>{:_id=>1}}
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>7}, "$orderby"=>{:_id=>1}}
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>9}, "$orderby"=>{:_id=>1}}
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>4}, "$orderby"=>{:_id=>1}}
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>16}, "$orderby"=>{:_id=>1}}
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>4}, "$orderby"=>{:_id=>1}}
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>11}, "$orderby"=>{:_id=>1}}
QUERY CACHE  database=ki_backend_development collection=users selector={"$query"=>{"_id"=>16}, "$orderby"=>{:_id=>1}}

Mongoid 版本为 4.0.2

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号