我一直在分析我网站上的速度,我发现它有问题。这是我的控制器:
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 个额外的查询,以及如何避免它?