Rails模板渲染速度慢?

时间:2009-12-29 12:11:48

标签: ruby-on-rails

我目前正在使用运行带有ruby1.8的rails2.3.5的 mongrel 服务器,并且得到了这种奇怪的行为:

Rendered common/_header (3.1ms)
Rendered common/_categories (1.2ms)
Rendered common/_footer (1.2ms)
Completed in 6934ms (View: 5897, DB: 174)   ## 5897ms... to rendering a local page??

为什么?

更新

那里只有一个用户..我正在我的笔记本上工作,问题在于渲染视图,我想。但我无法弄清楚为什么,因为在局部布局和模板中没有任何花哨的东西。

服务器日志

Processing PostController#show (for 127.0.0.1 at 2009-12-29 22:09:35) [GET]
  Parameters: {"action"=>"show", "id"=>"1", "controller"=>"post"}
  Post Columns (4.0ms)   SHOW FIELDS FROM `posts`
  Post Load (3.0ms)   SELECT * FROM `posts` WHERE (`posts`.`id` = 1) 
  User Columns (4.5ms)   SHOW FIELDS FROM `users`
  User Load (3.1ms)   SELECT * FROM `users` WHERE (`users`.`persistence_token` = 'XXXXXXXXXX') LIMIT 1
  Answer Columns (3.5ms)   SHOW FIELDS FROM `answers`
  Reading Columns (2.5ms)   SHOW FIELDS FROM `readings`
  Reading Load (1.2ms)   SELECT * FROM `readings` WHERE (`readings`.`user_id` = 9) AND (`readings`.post_id = 1) LIMIT 1
Rendering template within layouts/base
Rendering post/show
  Vote Load (1.5ms)   SELECT * FROM `votes` WHERE (`votes`.post_id = 1) 
  Vote Columns (3.0ms)   SHOW FIELDS FROM `votes`
  SQL (1.0ms)   SELECT count(*) AS count_all FROM `readings` WHERE (`readings`.post_id = 1) 
  User Load (3.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  posts_tags Columns (3.3ms)   SHOW FIELDS FROM `posts_tags`
  Tag Load (1.8ms)   SELECT * FROM `tags` INNER JOIN `posts_tags` ON `tags`.id = `posts_tags`.tag_id WHERE (`posts_tags`.post_id = 1 ) 
  Tag Columns (5.4ms)   SHOW FIELDS FROM `tags`
  SQL (1.1ms)   SELECT count(*) AS count_all FROM `answers` WHERE (`answers`.post_id = 1) 
  Answer Load (23.4ms)   SELECT * FROM `answers` WHERE (`answers`.post_id = 1) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  Digg Load (2.2ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 9) 
  Digg Columns (3.3ms)   SHOW FIELDS FROM `diggs`
  Reply Load (2.8ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 9) 
  Reply Columns (3.4ms)   SHOW FIELDS FROM `replies`
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  Digg Load (1.8ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 10) 
  Reply Load (2.7ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 10) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  User Load (3.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  Digg Load (1.8ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 11) 
  Reply Load (2.1ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 11) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (1.9ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 12) 
  Reply Load (1.7ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 12) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (2.5ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 13) 
  Reply Load (1.8ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 13) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (11.0ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 14) 
  Reply Load (1.7ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 14) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (1.7ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 15) 
  Reply Load (1.9ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 15) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (1.7ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 16) 
  Reply Load (11.7ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 16) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (1.7ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 17) 
  Reply Load (1.8ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 17) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (10.8ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 18) 
  Reply Load (2.0ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 18) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg load (1.8ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 19) 
  Reply Load (2.1ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 19) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (8.6ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 20) 
  Reply Load (1.6ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 20) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (1.8ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 21) 
  Reply Load (1.6ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 21) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (1.8ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 22) 
  Reply Load (1.6ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 22) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (1.7ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 23) 
  Reply Load (7.3ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 23) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (1.8ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 24) 
  Reply Load (8.6ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 24) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (1.8ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 25) 
  Reply Load (2.0ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 25) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 9) 
  Digg Load (1.8ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 26) 
  Reply Load (1.8ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 26) 
  CACHE (0.0ms)   SELECT * FROM `users` WHERE (`users`.`id` = 4) 
  Digg Load (1.6ms)   SELECT * FROM `diggs` WHERE (`diggs`.answer_id = 27) 
  Reply Load (1.8ms)   SELECT * FROM `replies` WHERE (`replies`.answer_id = 27) 
Rendered common/_header (3.2ms)
Rendered common/_categories (1.5ms)
Rendered common/_footer (10.8ms)
Completed in 7407ms (View: 6376, DB: 194) | 200 OK [http://localhost/posts/1]

更新

似乎我的连接表中没有索引。

3 个答案:

答案 0 :(得分:1)

有很多事情可以导致这种情况:

  • 您的服务器很慢。考虑使用mod_rails。如果您已经这样做了,可能需要更多RAM或其他类型的硬件升级?

  • 您对数据库的请求太多了。查看日志以查看正在进行的操作

  • 您没有以有效的方式使用缓存。根据您的服务器,有各种解决方案

  • 您的代码未经过优化。尝试运行基准测试以了解您可以改进的内容。

答案 1 :(得分:0)

我的步骤:配置文件,修复通过这种方式找到的问题......如果仍然无效,请查看Rails中的缓存。

但仅在分析后缓存

答案 2 :(得分:0)

让我们看一下日志的更多内容,从控制器的“Processing ...”开始一直到“Completed ...”