【问题标题】:Improving Server-side Datatable performance提高服务器端数据表性能
【发布时间】:2019-06-06 18:03:54
【问题描述】:

我在我的 rails 应用程序中使用数据表,现在加载时间开始变慢,因为表中有大约 16,000 条记录。我已将表配置为在服务器端处理数据并使用 will_paginate gem,但加载时间仍然通常在 5 秒左右。下面的代码有什么特别的问题会导致加载时间过长吗?

注意 - 我在下面的代码 sn-ps 中注释了几行,以查看它是否对加载时间产生了影响 - 它没有。

谢谢! -迈克

class AccountsDatatable
  delegate :params, :h, :link_to, :number_to_currency, to: :@view
  attr_reader :user
  include Rails.application.routes.url_helpers
  include Pundit

  def pundit_user
    User.find(@current_user_id)
  end

  def initialize(view, account_status, current_user_id)
    @view = view
    @account_status = account_status
    @current_user_id = current_user_id
    @current_user = User.find(current_user_id)
  end

  def as_json(options = {})
    {
      sEcho: params[:sEcho].to_i,
      iTotalRecords: Account.count,
      iTotalDisplayRecords: accounts.total_entries,
      aaData: data
    }
  end

  private
  def data
    accounts.map do |account|
      [
        account.account_name,
        account.account_status.to_s.humanize,
        #account.get_departments_with_active_opportunities,
        #account.get_departments_with_active_lobs,
        link_to("Open", account_path(account), class: "btn btn-xs btn-success"),
        ((policy(account).update?) ? link_to("Edit", edit_modal_form_accounts_path(id: account.id), class: "btn btn-xs btn-default", remote: true) : " "),
        ((policy(account).destroy?) ? link_to("Delete", delete_modal_form_accounts_path(id: account.id), class: "btn btn-xs btn-danger", remote: true) : " ")
      ]
    end
  end

  def accounts
    @accounts ||= fetch_accounts
  end

  def fetch_accounts
    if @account_status.nil? || @account_status == "all"
      accounts = Account.order("#{sort_column} #{sort_direction}").all
    else
      accounts = Account.order("#{sort_column} #{sort_direction}").where(account_status: @account_status)
    end
    if params[:sSearch].present?
      search = params[:sSearch]
      begin
        accounts = accounts.or( { account_name: (/.*#{search}.*/i) } )
      rescue
        accounts = accounts.or( { account_name: (/^#{::Regexp.escape(search)}$/i) } )
      end
    end
    accounts = accounts.paginate(:page => page, :per_page => per_page)
    accounts
  end

  def page
    params[:iDisplayStart].to_i/per_page + 1
  end

  def per_page
    params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
  end

  def sort_column
    columns = %w[account_name account_status] 
    columns[params[:iSortCol_0].to_i] 
  end

  def sort_direction
    params[:sSortDir_0] == "desc" ? "desc" : "asc"
  end

end

页面脚本:

$(document).ready(function () {

    const datatable_options = { 
      sPaginationType: 'full_numbers',
      bProcessing: true,
      bServerSide: true,
      sAjaxSource: $('#accounts_table').data('source'),
      columns: [
        null,
        null,
        // { orderable: false },
        // { orderable: false },
        { orderable: false },
        { orderable: false },
        { orderable: false }
      ]
    };
    initialize_datatable('#accounts_table', datatable_options);
  });

查看

<table id = "accounts_table" class="table table-striped accounts_table" data-source="<%= accounts_path(format: "json", account_status: @account_status ) %>" >
          <thead>
            <tr>
              <th>Account Name</th>
              <th>Classification</th>
              <!--<th>Active prospecting departments</th>-->
              <!--<th>Active departments</th>-->
              <th>&nbsp;</th>
              <th>&nbsp;</th>
              <th>&nbsp;</th>
            </tr>
          </thead>
          <tbody>
          </tbody>
        </table>

【问题讨论】:

  • 您的数据库运行查询的速度取决于索引 - 您是否创建并运行迁移以按 account_name 索引您的表?

标签: ruby-on-rails ruby ajax datatables


【解决方案1】:

看看:DataTables is running slow. How can I speed it up?

问。 DataTables 运行缓慢。如何加快速度?
A. 有几种方法可以加速 DataTables。往往是第一个 要做的是启用分页 (paging) 如果已经 禁用,因为这只会减少要显示的数据量。 除此之外,您用于加载数据的方法将 通常对性能有最显着的影响。

  • 客户端处理 - DOM 源数据:~5'000 行。速度选项: orderClasses
  • 客户端处理 - Ajax 源数据 (ajax):~50,000 行。 速度选项: deferRender
  • 服务器端处理(serverSide): 数百万行。

以上数字仅供参考。实际数字和对性能的影响取决于浏览器版本、插件等各种因素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-24
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    相关资源
    最近更新 更多