Rails 4查看渲染非常慢

时间:2014-05-16 13:58:26

标签: ruby-on-rails ruby ruby-on-rails-4 rails-activerecord

我一直在开发我的第一个Rails应用程序,它突然变得很慢。我不确定是什么造成的。我已经添加了paperclip来管理图像,并且在它开始变慢之前就切换到使用Dropbox(而不是本地)存储,但我也做了一些不相关的视图更改。

现在,我对Rails和MVC框架一般都比较陌生。所以我怀疑由于无知和缺乏经验而我做错了。只是因为同样的无知和缺乏经验使得诊断问题变得困难。

因此,这是使用" rails server"进行开发时的输出。 (尽管Heroku的制作速度要快得多)。

    Started GET "/games/index" for 217.156.133.130 at 2014-05-16 09:31:10 -0400
    Started GET "/games/index" for 217.156.133.130 at 2014-05-16 09:31:10 -0400
    Processing by GamesController#index as HTML
    Processing by GamesController#index as HTML
      Game Load (1.3ms)  SELECT "games".* FROM "games" ORDER BY name ASC
      Game Load (1.3ms)  SELECT "games".* FROM "games" ORDER BY name ASC
   (0.8ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 9]]
   (0.8ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 9]]
  User Load (0.4ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 9]]
  User Load (0.4ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 9]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 5]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 5]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 5]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 5]]
   (1.0ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 7]]
   (1.0ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 7]]
  User Load (0.6ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 7]]
  User Load (0.6ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 7]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 12]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 12]]
  User Load (0.5ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 12]]
  User Load (0.5ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 12]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 2]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 2]]
  User Load (0.4ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 2]]
  User Load (0.4ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 2]]
   (0.7ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 10]]
   (0.7ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 10]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 10]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 10]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 23]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 23]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 23]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 23]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 18]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 18]]
  User Load (0.5ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 18]]
  User Load (0.5ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 18]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 3]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 3]]
  User Load (0.7ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 3]]
  User Load (0.7ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 3]]
   (0.8ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 13]]
   (0.8ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 13]]
  User Load (0.5ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 13]]
  User Load (0.5ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 13]]
   (0.7ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 24]]
   (0.7ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 24]]
  User Load (0.4ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 24]]
  User Load (0.4ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 24]]
   (1.2ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 17]]
   (1.2ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 17]]
  User Load (0.8ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 17]]
  User Load (0.8ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 17]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 4]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 4]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 4]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 4]]
   (1.0ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 8]]
   (1.0ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 8]]
  User Load (0.6ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 8]]
  User Load (0.6ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 8]]
   (0.8ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 19]]
   (0.8ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 19]]
  User Load (0.4ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 19]]
  User Load (0.4ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 19]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 15]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 15]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 15]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 15]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 16]]
   (0.6ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 16]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 16]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 16]]
   (0.7ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 20]]
   (0.7ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 20]]
  User Load (0.4ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 20]]
  User Load (0.4ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 20]]
   (1.0ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 21]]
   (1.0ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 21]]
  User Load (0.7ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 21]]
  User Load (0.7ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 21]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 22]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 22]]
  User Load (0.6ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 22]]
  User Load (0.6ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 22]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 1]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 1]]
  User Load (0.5ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 1]]
  User Load (0.5ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 1]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 6]]
   (0.9ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 6]]
  User Load (0.7ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 6]]
  User Load (0.7ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 6]]
   (0.7ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 11]]
   (0.7ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 11]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 11]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 11]]
   (0.7ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 14]]
   (0.7ms)  SELECT COUNT(*) FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 14]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 14]]
  User Load (0.3ms)  SELECT "users".* FROM "users" INNER JOIN "user_games" ON "users"."id" = "user_games"."user_id" WHERE "user_games"."game_id" = $1  [["game_id", 14]]
  Rendered games/index.html.erb within layouts/application (77221.9ms)
  Rendered games/index.html.erb within layouts/application (77221.9ms)
Completed 200 OK in 77227ms (Views: 77194.1ms | ActiveRecord: 31.4ms)
Completed 200 OK in 77227ms (Views: 77194.1ms | ActiveRecord: 31.4ms)

正如您所看到的那样,View需要花费近一分钟和20秒的时间进行渲染,而数据库访问并不是一个真正的问题。以下是渲染的视图。

    <h1>All Games</h1>

<%= link_to "Add new", games_new_path %>

<% @games.each do |game| %>
  <div class="game">
    <div class="game_image">
      <% if game.image.exists? %>
        <%= image_tag game.image.url(:thumb) %>
      <% end %>
    </div>
    <div class="game_info">
      <h2><%= link_to game.name, game %></h2><%= link_to "Edit", edit_game_path(game) %><br />
      <% if game.free %>
        This game is free.<br />
        <% if game.notes != "" %>
          N.B. <%= game.notes %>
        <% end %>
      <% end %>
      <%= game.users.size %> Players<br />
      <div class="user_list">
      <% game.users.each do |user| %>
        <div class="user_thumb">
          <% if user.avatar.exists? %>
            <%= link_to image_tag(user.avatar.url(:pinky)), user %>
          <% else %>
            <div class="alt_link">
              <%= link_to user.name, user %>
            </div>
          <% end %>
        </div>
      <% end %>
    </div>
    <div class="clear_div"></div>
    </div>
  </div>
<% end %>

我从一般编程中获得的知识向我表明,最昂贵的部分应该是这样一个事实,即每个游戏我循环遍历所有用户(用户和游戏之间存在多对多的关系)。但是,数据库中的每个表只有几十个记录......几乎没有任何数据需要处理,所以我不相信它应该真的非常这个慢。

作为一个Rails新手,任何人都可以向我推荐一些我可以采取的步骤来尝试查看导致这种情况的原因吗?我已经google了,我看到很多人都有这个问题,但显然有很多原因可以发生,因为我找到的任何东西都没有解决我的特殊问题。

编辑:添加Eager Loading后,Active Record会更快但View渲染仍然非常慢。

Started GET "/games/index" for 217.156.133.130 at 2014-05-16 10:07:07 -0400
Started GET "/games/index" for 217.156.133.130 at 2014-05-16 10:07:07 -0400
Processing by GamesController#index as HTML
Processing by GamesController#index as HTML
  Game Load (1.1ms)  SELECT "games".* FROM "games" ORDER BY name ASC
  Game Load (1.1ms)  SELECT "games".* FROM "games" ORDER BY name ASC
  UserGame Load (0.8ms)  SELECT "user_games".* FROM "user_games" WHERE "user_games"."game_id" IN (9, 5, 7, 12, 2, 10, 23, 18, 3, 13, 24, 17, 4, 8, 19, 15, 16, 20, 21, 22, 1, 6, 11, 14)
  UserGame Load (0.8ms)  SELECT "user_games".* FROM "user_games" WHERE "user_games"."game_id" IN (9, 5, 7, 12, 2, 10, 23, 18, 3, 13, 24, 17, 4, 8, 19, 15, 16, 20, 21, 22, 1, 6, 11, 14)
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" IN (5, 6, 4, 1, 9, 8, 11)
  User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" IN (5, 6, 4, 1, 9, 8, 11)
  Rendered games/index.html.erb within layouts/application (79191.5ms)
  Rendered games/index.html.erb within layouts/application (79191.5ms)
Completed 200 OK in 79196ms (Views: 79191.6ms | ActiveRecord: 2.5ms)
Completed 200 OK in 79196ms (Views: 79191.6ms | ActiveRecord: 2.5ms)

2 个答案:

答案 0 :(得分:4)

在Paperclip中调用exists?非常慢,因为它将检查文件系统,或者如果您使用的是Amazon S3,它将在每次调用时发出http请求。您可以使用exists?game.image?,而不是user.avatar?。这只会检查文件名是否存储在数据库中,而不是检查文件是否确实存在。如果您不担心这些图像会在Rails应用程序的上下文之外被删除,那么它会更快。

答案 1 :(得分:1)

我首先会在您的ERB文件中注释掉任何ruby代码,然后尝试确切地指出导致渲染缓慢爬行的行。我从未使用过Paperclip,但@ abstractcoder的回复肯定是个原因。

另一种选择是使用片段缓存,如here所述。它将允许您缓存部分视图,以便它们从磁盘呈现,而不是在每个请求上处理。它可以缓存代码的某些部分,但它仍然足够聪明,可以在模型更新时破坏缓存。

但在你做任何过早优化之前,一定要先寻找根本原因。