【问题标题】:Why is my Ruby On Rails database query so slow?为什么我的 Ruby On Rails 数据库查询这么慢?
【发布时间】:2011-11-04 19:26:47
【问题描述】:

以下代码总是使用十秒以上。我已经升级了服务器,但它没有帮助。我知道我有一些数据库设计问题,但我无法修改。

我正在显示同一类别产品的不同位置的所有价格也在不同的时间范围内,因为每个位置的价格每 15 天变化一次。

控制器

def prods_x_cat
 # This will load all the products of a category
 @products =  Product.prods_x_cat(params[:id],:include => :raw_datas)
 @cortes = RawData.cortes
  respond_to do |format|
    format.js { render :layout=>false}
  end
end

prods_x_cat.js.erb

 var jqxhr1 = $.ajax($("#loading_overlay .loading_message, #loading_overlay").fadeIn());
 $('#datos').html("<%= escape_javascript render :partial=>'/shared/prods_x_cat'%>")

查看

    <%@cortes.each do |c|%>
  <a href="#<%=c.corte%>" class="round_top"><%=c.corte%></a>
    <%end%>
    <%@cortes.each do |c|%>
     <%@fecha = c.corte_real%>
     <div id="<%=c.corte%>" class="block no_padding">
     <table class="display datatable">
    <thead>
    <tr>
      <th>SKU</th>
            <%Company.active.order('table_field ASC').each do |c|%>
                <th><%=c.abbr%></th>
            <%end%>
      <th>Average</th>
      <th>Mode</th>
      <th>Minimum</th>
      <th>Maximum</th>
     </tr>
    </thead>
    <tbody>
    <%@products.each do |p|%>
     <tr class="gradeA">
    <td><%=p.name%></td>
    <%p.raw_datas.where("product_id='#{p.id}' and corte_real='#{@fecha}'").each do |prd|%>
      <td><%=prd.location1.to_f.round(2)%></td>
      <td><%=prd.location2.to_f.round(2)%></td>
      <td><%=prd.location3.to_f.round(2)%></td>
      <td><%=prd.location4.to_f.round(2)%></td>
      <td><%=prd.location5.to_f.round(2)%></td>
      <td><%=prd.location6.to_f.round(2)%></td>
      <td><%=prd.location7.to_f.round(2)%></td>
      <td><%=prd.location8.to_f.round(2)%></td>
      <td><%=prd.promedio.to_f.round(2)%></td>
      <td><%=prd.moda%></td>
      <td><%=prd.minimo.to_f.round(2)%></td>
      <td><%=prd.maximo.to_f.round(2)%></td>
     <%end%>
     </tr>
    <%end%>
    </tbody>
    </table>
   </div>
   <%end%>
   </div> 

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 activerecord ruby-on-rails-3.1


    【解决方案1】:

    如果不查看所涉及的所有代码,几乎不可能回答此类问题。相反,我可以帮助您找出问题所在。

    有一些很好的工具可以找到性能问题所在(例如 ruby​​-prof),但如果您想要一种快速的原始方法来查找问题所在,只需使用 Time.now。例如,您可以将控制器操作更改为:

    def prods_x_cat
     # This will load all the products of a category
     a1 = Time.now
     @products =  Product.prods_x_cat(params[:id],:include => :raw_datas)
     b1 = Time.now
     p "Time for finding products: #{b1 - a1}"
     a2 = Time.now
     @cortes = RawData.cortes
     b2 = Time.now
     p "Time for finding cortes: #{b2 - a2}"
     respond_to do |format|
       format.js { render :layout=>false}
     end
    end
    

    如果打印输出表明时间被其他地方占用,请开始在您的模板中执行类似的操作。专注于数据库调用。

    【讨论】:

    • 是的,我刚刚安装了 NewRelic,问题出在对数据库的调用上,操作系统我应该找到减少查询的方法,但我不知道如何,也许我可以将我的查询保存在对象对其进行迭代。因为每次我遍历调用 DB 的对象时,我都会在日志中看到调用数据库
    猜你喜欢
    • 2011-03-07
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2017-01-21
    • 2021-03-12
    相关资源
    最近更新 更多