【问题标题】:Speeding up Queries in Rails加速 Rails 中的查询
【发布时间】:2012-11-13 03:08:52
【问题描述】:

我正在慢慢地从初级轨道过渡到中级水平。在我的任务中,我遇到了加载某些查询所需时间的问题。

我想知道是否有人可以指出一些可能会提高我的索引操作查询性能的事情。

运行 Rails 3.2.0、ruby 1.9.3p194 和 MySQL

现在我列出了所有机构(大约 7000 个),加载需要 10 秒...

我的索引操作

  def index
    @institution = Institution.includes(:state).all
  end

查看

<tbody>
    <% @institution.each do |c| %>
    <tr>
        <td><%= c.company %></td>
        <td><%= c.state.name %></td>
        <td><%= number_to_human(c.assets) %></td>
        <td><%= c.hadademo %></td>
        <td><%= c.datebecameclient %></td>
        <td><%= c.clientorprospect %></td>
        <% if current_user.admin? %>
            <td><%= link_to 'Edit', edit_institution_path(c), :class => 'btn btn-mini btn-warning'  %></td>
        <% end %>
    </tr>
    <% end %>
</tbody>

机构模式

class Institution < ActiveRecord::Base
  attr_accessible :company, :phone, :clientdate, :street, :city, :state_id, :zip, :source, :source2, :demodate1, :demodate2, :demodate3, :client, :prospect, :notcontacted, :solveid
  belongs_to :state
  has_many :users, :through => :company_reps
  has_many :company_reps


  def points
    points_for_client + points_for_demodate1
  end

  def hadademo
    if demodate3.present?
        demodate3.to_date
    elsif demodate2.present?    
        demodate2.to_date
    elsif demodate1.present?    
        demodate1.to_date
    else
        "No Demo"
    end 
  end

  def datebecameclient
    if clientdate.present?
        clientdate.to_date
    else
        "Not a Client"
    end 
  end

  def clientorprospect
      if client?
            "Client"
        elsif prospect? 
            "Prospect"
        else
            "No Contact"
        end 
  end

  def points_for_demodate1
    if demodate1.present? && demodate1 >= Date.new(2012, 2, 23) or demodate2.present? && demodate2 >= Date.new(2012, 2, 23) or demodate3.present? && demodate3 >= Date.new(2012, 2, 23)
      1
    else
      0
    end
  end

  def clientcount
    if client?
      1
    else
      0
    end
  end

  def points_for_client
    if client?
      10 / users.count
    else
      0
    end
  end


end

使用 Jquery Datatables

jQuery ->
    $("#institutions").dataTable
      sDom: "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>"
      sPaginationType: "bootstrap"

日志文件

Started GET "/institutions" for xxx.xxx.xxx.xxx at 2012-11-13 02:51:47 +0000
Processing by InstitutionsController#index as HTML
  Rendered layouts/_messages.html.erb (1.2ms)
  Rendered institutions/index.html.erb within layouts/application (37190.3ms)
  Rendered layouts/_messages.html.erb (0.1ms)
  Rendered layouts/_navbar.html.erb (17.0ms)
Completed 200 OK in 43334ms (Views: 37983.7ms | ActiveRecord: 340.7ms)
  Rendered institutions/index.html.erb within layouts/application (34329.1ms)
  Rendered layouts/_messages.html.erb (0.1ms)
  Rendered layouts/_navbar.html.erb (23.3ms)
Completed 200 OK in 38824ms (Views: 34374.8ms | ActiveRecord: 82.2ms)

【问题讨论】:

    标签: mysql ruby-on-rails ruby


    【解决方案1】:

    如果您查看日志,您会发现所有时间都花在了呈现您的视图上,而不是实际的查询上。您肯定希望实现视图或动作缓存以加快速度。

    official docs 将为您指明正确的方向。

    【讨论】:

      【解决方案2】:

      一般来说有几件事情要做:

      1. 缓存。研究片段缓存,因为完全跳过数据库将提供巨大的加速。
      2. 数据库索引。由于您正在加载关联模型,因此确保为相关外键编制索引可能有助于提高性能。
      3. 延迟加载。您可能只想呈现前 100 条记录,并在用户浏览它们时通过 AJAX 加载更多记录。

      除了发布生成的查询可能会帮助我们提供更多提示。

      【讨论】:

      • 谢谢您,我会考虑您的建议。我已经在查询中发布了我的日志
      • 不客气。我的意思是发布正在生成的实际 SQL 查询(如果可能,请解释)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-29
      • 1970-01-01
      • 2023-04-08
      • 2019-10-11
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多