简体   繁体   中英

Why is my Ruby On Rails database query so slow?

The following code always uses more then ten seconds. I have upgraded the server, but it doesn't help. I know I have some database design problems, but I can't modify that.

I am showing all the prices from differents locations of products of a category also in different time range, because the prices change every 15 days in each location.

controller

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'%>")

view

    <%@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> 

This kind of question is pretty much impossible to answer without seeing all the code involved. Instead I can help you try to figure out where the problem is.

There are good tools for finding where your performance problems are (eg ruby-prof) but if you want a quick primitive way to find where your issue is, just use Time.now . For example, you could change your controller action to be:

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

If the printouts suggest that the time is taken up elsewhere, start doing something similar in your template. Focus on the database calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM