0

我一直在分析我网站上的速度,我发现它有问题。这是我的控制器:

  before_filter :authorize
  before_filter :load_stuff

  def load_stuff
    @messages = current_user.messages.desc(:created_at).limit(5)
    @accounts = current_user.accounts.limit(5)
    @searches = current_user.searches.desc(:updated_at).where(complete: true).limit(5)
    @last_message = @messages.first.message_body if @messages.first.present?
    @last_video = @messages.first.video_id if @messages.first.present?
  end

  def index
    @message = Message.new url: @last_video, message_body: @last_message
  end

这是它生成的查询列表:

Processing by PromoteController#index as HTML
  MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} (0.7179ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=users selector={"_id"=>"5130abdfc9f2672059000005"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.3302ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=messages selector={"$query"=>{"user_id"=>"5130abdfc9f2672059000005"}, "$orderby"=>{"created_at"=>-1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.5000ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=messages selector={"$query"=>{"user_id"=>"5130abdfc9f2672059000005"}, "$orderby"=>{"created_at"=>-1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.3879ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=messages selector={"$query"=>{"user_id"=>"5130abdfc9f2672059000005"}, "$orderby"=>{"created_at"=>-1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.3750ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=messages selector={"$query"=>{"user_id"=>"5130abdfc9f2672059000005"}, "$orderby"=>{"created_at"=>-1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.3281ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=accounts selector={"user_id"=>"5130abdfc9f2672059000005"} flags=[:slave_ok] limit=5 skip=0 batch_size=nil fields=nil (0.3731ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=searches selector={"$query"=>{"user_id"=>"5130abdfc9f2672059000005", "complete"=>true}, "$orderby"=>{"updated_at"=>-1}} flags=[:slave_ok] limit=5 skip=0 batch_size=nil fields=nil (3.7348ms)
  MOPED: 127.0.0.1:27017 COMMAND      database=tp3_development command={:count=>"messages", :query=>{"user_id"=>"5130abdfc9f2672059000005"}} (0.3629ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=messages selector={"$query"=>{"user_id"=>"5130abdfc9f2672059000005"}, "$orderby"=>{"created_at"=>-1}} flags=[:slave_ok] limit=5 skip=0 batch_size=nil fields=nil (0.3257ms)
  Rendered promote/index.html.erb within layouts/application (7.4ms)
Completed 200 OK in 17ms (Views: 11.3ms)

现在如果我删除这两行:

@last_message = @messages.first.message_body if @messages.first.present?
@last_video = @messages.first.video_id if @messages.first.present?

比查询数下降到这个:

Processing by PromoteController#index as HTML
  MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} (0.5271ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=users selector={"_id"=>"5130abdfc9f2672059000005"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.2854ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=accounts selector={"user_id"=>"5130abdfc9f2672059000005"} flags=[:slave_ok] limit=5 skip=0 batch_size=nil fields=nil (0.5162ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=searches selector={"$query"=>{"user_id"=>"5130abdfc9f2672059000005", "complete"=>true}, "$orderby"=>{"updated_at"=>-1}} flags=[:slave_ok] limit=5 skip=0 batch_size=nil fields=nil (6.1519ms)
  MOPED: 127.0.0.1:27017 COMMAND      database=tp3_development command={:count=>"messages", :query=>{"user_id"=>"5130abdfc9f2672059000005"}} (0.3920ms)
  MOPED: 127.0.0.1:27017 QUERY        database=tp3_development collection=messages selector={"$query"=>{"user_id"=>"5130abdfc9f2672059000005"}, "$orderby"=>{"created_at"=>-1}} flags=[:slave_ok] limit=5 skip=0 batch_size=nil fields=nil (0.2959ms)
  Rendered promote/index.html.erb within layouts/application (11.2ms)
Completed 200 OK in 99ms (Views: 15.4ms)

所以我的问题是,为什么这两行会产生 4 个额外的查询,以及如何避免它?

4

1 回答 1

-1

因为 @messages是您的 mongoid 标准

这意味着它不会触发查询,它会为您构建查询(如 arel)并且不像

缓存查询结果的关系数据库 mongodb 不会缓存您的查询

所以例如假设上面

Does MongoDB handle caching?
Yes. MongoDB keeps all of the most recently used data in RAM. If you have created indexes for your queries and your working data set fits in RAM, MongoDB serves all queries from memory.

MongoDB does not implement a query cache: MongoDB serves all queries directly from the indexes and/or data files.

直接从 Mongo 文档中检查问题的答案

1个查询

@messages.first.present?

2 查询

@messages.first.message_body

3 查询

@messages.first.present?

4 查询

@messages.first.video_id

天哪,我必须做好的事情是准确的,你不必担心它

a) MongoDB 旨在提高读取操作的速度。

b) 如前所述,如果 mongo 文档如果您从查询创建索引,则很可能会更改查询结果将从内存中获取

注意:我知道在 mongoid 的这个缓存中我不知道它做了什么,因为我上次尝试它的工作方式不是它记录的方式

于 2013-03-11T17:15:22.627 回答