how to display thousands of records


I have a table called clientes, this table has around 15536 records, which makes data loading extremely slow. How can I optimize the log load to improve the process?

this is my index view

.container {


  <table id="clientes" class="display"><!--el id clientes es de datatables referenciado en clientes.coffe y display class es una clase de datatables-->

    <tr><!--active es para sombrear la fila-->
      <th>Nombre Corto</th>

    <tbody id="container_clientes">
      <%= render @clientes %><!--carga todos los clientes-->

my partial cliente.html.erb

<tr id="cliente_<%= %>">

    <% if cliente.Credito == true%>
      <input type="checkbox" disabled="true" checked="true">
      <input type="checkbox" disabled="true" >
    <% if cliente.detallecob.last != nil%>
    <% if cliente.relclirutas != nil%>
      <% cliente.relclirutas.each do |varias| %>
    <% if cliente.relclili != nil%>

        <%= link_to '<i class="fa fa-gift" aria-hidden="true"></i> Activos'.html_safe, activos_cliente_path(cliente), class:"btn btn-primary btn-xs boton" %>

        <button type="button" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#myupdatecliente_<%= %>">
        <% if cliente.Status == true%>
          <%= link_to 'Eliminar', cliente, method: :delete, class: "btn btn-danger btn-xs", remote:true %>
          <%= link_to 'Habilitar', cliente, method: :delete, class: "btn btn-success btn-xs", remote:true %>

<td class="no" >

Show source
| ruby   | ruby-on-rails   | sql   | datatable   2016-12-31 17:12 3 Answers

Answers to how to display thousands of records ( 3 )

  1. 2016-12-31 17:12

    This is a bit of a guess because I am not a ruby programmer, but, let's look at this:

        <% if cliente.relclirutas != nil%>
          <% cliente.relclirutas.each do |varias| %>

    If that means,

    if cliente.relclirutas is not null or an empty string
    loop through it and display each value of varias.ruta.Ruta

    Then you don't need the if statement. Simply loop through the null value or empty string. This might save you a few nanoseconds on each record, of which you have 15,000 plus.

  2. 2016-12-31 19:12

    You can use kaminari gem for data paging, just take part of data and require more by access next page.

    kaminari also do the pagination links for you, just use the helper

    <%= paginate @cliente %>

    You can find the usage in README.

    For better UX, you should make the request communicate by API(AJAX), and use some tech like infinite scrolling.

  3. 2016-12-31 20:12

    You could offload your fetching. This means, use a slightly more complex approach with javascript and ajax. I will not use a pagination gem in this example.

    Modify your controller:

    def index
        if !params[:page].blank?
            respond_to do |f|
                f.html {}
                f.json { # only for pagination, work around this to make it more complex
                    offset = 50*params[:page].to_i
                    @clients = Client.offset(offset).limit(50).select("field1, field2") # appended to any other condition you may have
                    render :json => @clients.as_json
            @clients = Client.limit(50)

    I will not post more, you can figure it out. On document load, add an ajax fetch event that will:

    • Call /clientes?page=2 etc (add an increment counter)
    • Do until you get an empty response the following:
      • For each row you get of the json response, append a row to the table, following the partial code

    You may do it with a timer or a loop. But this is a way your page will become responsive after loading a first set of rows (in this example 50) and then, while you work, more rows will be fetching.

Leave a reply to - how to display thousands of records

◀ Go back