Ruby On Rails运行缓慢...? [英] Ruby On Rails is slow...?

查看:102
本文介绍了Ruby On Rails运行缓慢...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Web应用程序来监视家具工厂的生产流程.它有成千上万的数据要处理.到目前为止,我在Mongrel + MySQL上运行RoR,它确实非常慢(某些视图需要2-4分钟).当我查看RoR日志时,数据库查询似乎并不慢(0-10毫秒).

I'm writing a web application to monitor a furniture factory production flow. It has thousand of data to handle. So far, I run RoR on Mongrel + MySQL and it's really really slow (2-4min for some views). When I look at RoR logs, it seems that database queries aren't slow (0-10ms).

RoR将数据库数据转换为对象时会很慢吗?杂种慢吗?

Is RoR slow when it converts database data to object? Is Mongrel slow?

编辑:第一件事:我在开发中.环境在生产环境中,最慢的视图需要2分钟(如果我的计算机已经使用5年,那么在一台好的计算机上,它的显示时间会缩短到不足1分钟).通过使用ruby-prof和一些常识,我发现了哪些方法使应用程序变慢了.问题在于单个SQL查询在大型数据集上循环调用:

Edit: First thing: I was in dev. env. In the production environment, the slowest view takes 2min (which would turn down to less than 1min on a good computer, mine is 5 years old). With ruby-prof and a bit of common sense, I've found out which methods were slowing down the application. The problem is that single SQL queries are called in loops on larges datasets:

ofs = Ofkb.find_by_sql ["..some large SQL query..."]

for of in ofs # About 700-1000 elements
   ops = Operation.find(..the single query..)
   etc.
end

以下是使用这些方法的红宝石分析结果:

Here are ruby-prof results on those methods:

 %self     total     self     wait    child    calls  name
 32.19     97.91    97.91     0.00     0.00       55  IO#gets (ruby_runtime:0}
 28.31     86.39    86.08     0.00     0.32    32128  Mysql#query (ruby_runtime:0}
  6.14     18.66    18.66     0.00     0.00    12432  IO#write (ruby_runtime:0}
  0.80      2.53     2.42     0.00     0.11    32122  Mysql::Result#each_hash (ruby_runtime:0}

问题是:我真的无法避免那些单一查询.我有成千上万的事件必须从中计算复杂的数据.现在,除非您是第一个请求该页面的人,否则我会在那些可以使用的方法上使用memcached.

The problem is: I can't really avoid those single queries. I've got thousands of events from which I have to compute complex data. Right now I'm using memcached on those methods which are OK unless you're the first to request the page.

推荐答案

我会同意其他所有人的观点.您必须配置文件.在您不知道是什么原因导致速度缓慢之前,对您的代码进行任何操作都没有意义.在不了解原因的情况下尝试解决问题就像感到不适,并决定进行大量手术,直到您感觉好些为止.首先诊断您的问题.它可能有点小,例如网络设置,也可能是代码中的一条错误行.

I'll agree with everyone else. You have to profile. There is no point in doing anything to your code until you know what specifically is causing the slowness. Trying to fixing a problem without understanding the cause is like feeling ill and deciding to have lots of surgery until you feel better. Diagnose your problem first. It might be something small like a network setting or it could be one bad line in your code.

一些分析技巧:

如何配置Rails应用程序

性能测试Rails应用程序

在Forge-分析Rails应用程序

一旦发现瓶颈,就可以弄清楚该怎么做.

Once you have found the bottleneck you can figure out what to do.

我推荐这些视频: Railslab缩放导轨

现在根据教授的结果进行了修订:

Revised now based on prof results:

好.现在您可以看到问题是您正在使用基于循环浏览另一个活动记录查询的结果的查询进行某种计算,我建议您考虑构建一个结合了初始选择条件和以下内容的自定义SQL语句:循环计算以获取所需的信息.您绝对可以通过优化SQL来加快速度.

OK. Now that you can see that your problem is that you are doing some sort of calculation using a query based on looping through the results of another active record query I'd advise you to look into building a custom SQL statement combining your initial selection criteria and the loop calculation to get what you need. You can definitely speed this up by optimizing the SQL.

这篇关于Ruby On Rails运行缓慢...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆