LuisC
LuisC

Reputation: 335

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

<h1>Clientes</h1>
<style>
.container {
}

</style>




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

    <tr><!--active es para sombrear la fila-->
      <th>Clave</th>
      <th>Nombre</th>
      <th>Nombre Corto</th>
      <th>Dirección</th>
      <th>Colonia</th>
      <th>Credito</th>
      <th>DiasCredito</th>
      <th>LimiteCredito</th>
      <th>Saldo</th>
      <th>Ruta</th>
      <th>Promociones</th>
      <th>Acción</th>
      <th></th>

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

my partial cliente.html.erb

<tr id="cliente_<%= cliente.id %>">
  <td><%=cliente.IdCli%>

</td>
  <td><%=cliente.Nombre%></td>
  <td><%=cliente.NombreCorto%></td>
  <td><%=cliente.Direccion%></td>
  <td><%=cliente.Colonia%></td>
  <td>
    <% if cliente.Credito == true%>
      <input type="checkbox" disabled="true" checked="true">
    <%else%>
      <input type="checkbox" disabled="true" >
    <%end%>
  </td>
  <td><%=cliente.DiasCreedito%></td>
  <td><%=cliente.LimiteCredito%></td>
  <td>
    <% if cliente.detallecob.last != nil%>
        <%=cliente.detallecob.last.Saldo%>
      <%else%>
        <%=cliente.Saldo%>
    <%end%>
  </td>
  <td>
    <% if cliente.relclirutas != nil%>
      <% cliente.relclirutas.each do |varias| %>
        <%=varias.ruta.Ruta%>
      <%end%>
    <%end%>
  </td>
  <td>
    <% if cliente.relclili != nil%>
      <%=cliente.relclili.listapromomast.ListaMaster%>
    <%end%>
  </td>


    <td>
        <%= 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_<%= cliente.id %>">
          Editar
        </button>
        <!--Destroy-->
        <% if cliente.Status == true%>
          <%= link_to 'Eliminar', cliente, method: :delete, class: "btn btn-danger btn-xs", remote:true %>
        <%else%>
          <%= link_to 'Habilitar', cliente, method: :delete, class: "btn btn-success btn-xs", remote:true %>
        <%end%>
      </td>


<td class="no" >
    </td>
</tr>

Upvotes: 2

Views: 202

Answers (3)

Ruby Racer
Ruby Racer

Reputation: 5740

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
            }
        end
    else
        @clients = Client.limit(50)
    end
end

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.

Upvotes: 1

gaga5lala
gaga5lala

Reputation: 1228

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

Cliente.page(1).per(50)

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.

Upvotes: 1

Dan Bracuk
Dan Bracuk

Reputation: 20804

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

<td>
    <% if cliente.relclirutas != nil%>
      <% cliente.relclirutas.each do |varias| %>
        <%=varias.ruta.Ruta%>
      <%end%>
    <%end%>
  </td>

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.

Upvotes: 0

Related Questions