我一直在分析我網站的速度,並發現它有問題。這裏是我的控制器:Rails 3 - 爲什麼在Mongoid遊標中調用.first開始新查詢
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)
所以我q問題是,爲什麼這兩行會生成4個額外的查詢,以及如何避免它?
請不要寫他們的短信給你最好的朋友(你,OMG,你等等)的答案。) – AD7six 2013-03-11 17:16:48
沒問題,所以只要我建立索引,不管有多少查詢都沒關係,因爲它們是基於RAM的。將看看緩存。另外,-1不是來自我。 – 2013-03-12 05:47:07
@JanNetherdrake對不起,延遲迴復不在你是你是正確的,只要你建立索引它將存儲在最近的結果內存關於你的其他問題'多少'我不完全確定如何回答這個問題,因爲mongodb內存映射它文件到操作系統,然後所有的操作系統來緩存它,並驅逐,如果它的內存不足,我推測以下[這](http://docs.mongodb.org/manual/faq/fundamentals/)和[這] (http://docs.mongodb.org/manual/faq/diagnostics/)會幫助你,如果你想深入瞭解Mongo將如何使用內存和其他東西 – Viren 2013-03-15 16:31:24