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)

これらの2行を削除すると:

@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)

私の質問は、これらの 2 行が 4 つの追加クエリを生成する理由と、それを回避する方法です。

4

1 に答える 1

-1

@messages はモンゴイド基準で あるため

つまり、クエリを起動せず、(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.

モンゴのドキュメントからまっすぐ出て、これも答えをチェックしてください

1クエリ

@messages.first.present?

2 クエリ

@messages.first.message_body

3 クエリ

@messages.first.present?

4 クエリ

@messages.first.video_id

OMG正確に言うと、私がしなければならないことは、心配する必要はありません

a)MongoDBは、読み取り操作のような速度を目的としています。

b)前述のように、クエリからインデックスを作成した場合、mongoドキュメントの場合、クエリ結果がメモリからフェッチされる可能性が非常に高くなります

注:モンゴイドのこのキャッシングにあることは知っていますが、最後に試したときは文書化された方法で機能していなかったため、それが何をするのかわかりません

于 2013-03-11T17:15:22.627 に答える