2013-03-09 20 views
0

我一直在分析我網站的速度,並發現它有問題。這裏是我的控制器: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個額外的查詢,以及如何避免它?

回答

-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. 

直爲答案以及出蒙戈文檔檢查this

1查詢

@messages.first.present?

2查詢

@messages.first.message_body

3查詢

@messages.first.present?

4查詢

@messages.first.video_id

OMG我有什麼做不好要精確您不必擔心生根粉它

一)MongoDB是意味着速度像讀取操作。

二)如上面所提到前面,如果蒙戈文件,如果你創建了從查詢,那麼極有可能改變的索引,查詢結果會從內存中獲取

注:我知道有在mongoid this緩存的事情我不知道它是什麼,因爲上次我嘗試它沒有按照它被記錄的方式工作

+1

請不要寫他們的短信給你最好的朋友(你,OMG,你等等)的答案。) – AD7six 2013-03-11 17:16:48

+0

沒問題,所以只要我建立索引,不管有多少查詢都沒關係,因爲它們是基於RAM的。將看看緩存。另外,-1不是來自我。 – 2013-03-12 05:47:07

+0

@JanNetherdrake對不起,延遲迴復不在你是你是正確的,只要你建立索引它將存儲在最近的結果內存關於你的其他問題'多少'我不完全確定如何回答這個問題,因爲mongodb內存映射它文件到操作系統,然後所有的操作系​​統來緩存它,並驅逐,如果它的內存不足,我推測以下[這](http://docs.mongodb.org/manual/faq/fundamentals/)和[這] (http://docs.mongodb.org/manual/faq/diagnostics/)會幫助你,如果你想深入瞭解Mongo將如何使用內存和其他東西 – Viren 2013-03-15 16:31:24

相關問題