Listing employees, categorized by license expiration date











up vote
1
down vote

favorite












I am working on a personal project and I have hit a wall. I know I am writing bad code and I really want to refactor the code below. The application has three tables on the same page. Each table contains data from a has-many-though relationship. In essence I have an employees page which contains three tables of employee licenses that all expire in grouped intervals:



ALL EMPLOYEE PAGE




  • Employees with licenses Expiring in 30 days

  • Employees with licenses Expiring in 30-90 days

  • Employees with licenses Expiring in 90 days


All three of these tables are independently paginated and I am allowing the user to enter a search term and search across all three tables.



However I have over 1200 licenses so the page is taking forever to load. How can I optimize this functionality? Any help would be greatly appreciated.



Model



  def self.emp_lic_small
self.all.map{|se| se.employee_licenses.less_than_thirty}.flatten
end

def self.emp_lic_medium
self.all.map{|se| se.employee_licenses.between_thirty_and_ninty}.flatten
end

def self.emp_lic_large
self.all.map{|se| se.employee_licenses.greater_than_ninty}.flatten
end


Controller



    @small_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_small.paginate(:page => params[:small_lic], :per_page => 20)
@medium_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_medium.paginate(:page => params[:med_lic], :per_page => 20)
@large_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_large.paginate(:page => params[:large_lic], :per_page => 20)


View



<div class="panel panel-danger">
<div class="panel-heading"><strong>Employee Licenses Expiring in Less Than 30 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @small_employee_licenses.present? %>
<% @small_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @small_employee_licenses, param_name:'small_lic' unless @small_employee_licenses.blank? %>

<div class="panel panel-warning">
<div class="panel-heading"><strong>Employee Licenses Expiring in 30-90 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @medium_employee_licenses.present? %>
<% @medium_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @medium_employee_licenses, param_name:'med_lic' unless @medium_employee_licenses.blank? %>
<div class="panel panel-success">
<div class="panel-heading"><strong>Employee Licenses Expiring in 30-90 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @large_employee_licenses.present? %>
<% @large_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @large_employee_licenses, param_name:'large_lic' unless @large_employee_licenses.blank? %>









share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • I'd wager the bottleneck is in the model methods. You're mapping all records. I can't tell from the code exactly what you're scoping that to – what model is this from? And what's se? Are the lists scoped to something before searching? – but you're looking for licenses, but seem to be going the long way of finding licenses through something. It also implies that the pagination happens late - after a lot of stuff has been loaded. So the pagination is the act of throwing a lot of it away, rather than loading what you need. But your question doesn't have enough detail, so I'm not sure.
    – Flambino
    Jan 8 '17 at 19:41










  • Could you post how these tables are related? (the has_many, etc, for the tables in the example?)
    – lcguida
    Jan 26 '17 at 10:48










  • Do you really need all 1200 employees to be displayed before asking the user to search for one? Why not first ask for a search term and then to accordingly display results?
    – BKSpurgeon
    Oct 8 '17 at 5:49










  • @Flambino i think 'se' stands for SiteEmployee
    – BKSpurgeon
    Oct 8 '17 at 5:51















up vote
1
down vote

favorite












I am working on a personal project and I have hit a wall. I know I am writing bad code and I really want to refactor the code below. The application has three tables on the same page. Each table contains data from a has-many-though relationship. In essence I have an employees page which contains three tables of employee licenses that all expire in grouped intervals:



ALL EMPLOYEE PAGE




  • Employees with licenses Expiring in 30 days

  • Employees with licenses Expiring in 30-90 days

  • Employees with licenses Expiring in 90 days


All three of these tables are independently paginated and I am allowing the user to enter a search term and search across all three tables.



However I have over 1200 licenses so the page is taking forever to load. How can I optimize this functionality? Any help would be greatly appreciated.



Model



  def self.emp_lic_small
self.all.map{|se| se.employee_licenses.less_than_thirty}.flatten
end

def self.emp_lic_medium
self.all.map{|se| se.employee_licenses.between_thirty_and_ninty}.flatten
end

def self.emp_lic_large
self.all.map{|se| se.employee_licenses.greater_than_ninty}.flatten
end


Controller



    @small_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_small.paginate(:page => params[:small_lic], :per_page => 20)
@medium_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_medium.paginate(:page => params[:med_lic], :per_page => 20)
@large_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_large.paginate(:page => params[:large_lic], :per_page => 20)


View



<div class="panel panel-danger">
<div class="panel-heading"><strong>Employee Licenses Expiring in Less Than 30 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @small_employee_licenses.present? %>
<% @small_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @small_employee_licenses, param_name:'small_lic' unless @small_employee_licenses.blank? %>

<div class="panel panel-warning">
<div class="panel-heading"><strong>Employee Licenses Expiring in 30-90 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @medium_employee_licenses.present? %>
<% @medium_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @medium_employee_licenses, param_name:'med_lic' unless @medium_employee_licenses.blank? %>
<div class="panel panel-success">
<div class="panel-heading"><strong>Employee Licenses Expiring in 30-90 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @large_employee_licenses.present? %>
<% @large_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @large_employee_licenses, param_name:'large_lic' unless @large_employee_licenses.blank? %>









share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • I'd wager the bottleneck is in the model methods. You're mapping all records. I can't tell from the code exactly what you're scoping that to – what model is this from? And what's se? Are the lists scoped to something before searching? – but you're looking for licenses, but seem to be going the long way of finding licenses through something. It also implies that the pagination happens late - after a lot of stuff has been loaded. So the pagination is the act of throwing a lot of it away, rather than loading what you need. But your question doesn't have enough detail, so I'm not sure.
    – Flambino
    Jan 8 '17 at 19:41










  • Could you post how these tables are related? (the has_many, etc, for the tables in the example?)
    – lcguida
    Jan 26 '17 at 10:48










  • Do you really need all 1200 employees to be displayed before asking the user to search for one? Why not first ask for a search term and then to accordingly display results?
    – BKSpurgeon
    Oct 8 '17 at 5:49










  • @Flambino i think 'se' stands for SiteEmployee
    – BKSpurgeon
    Oct 8 '17 at 5:51













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am working on a personal project and I have hit a wall. I know I am writing bad code and I really want to refactor the code below. The application has three tables on the same page. Each table contains data from a has-many-though relationship. In essence I have an employees page which contains three tables of employee licenses that all expire in grouped intervals:



ALL EMPLOYEE PAGE




  • Employees with licenses Expiring in 30 days

  • Employees with licenses Expiring in 30-90 days

  • Employees with licenses Expiring in 90 days


All three of these tables are independently paginated and I am allowing the user to enter a search term and search across all three tables.



However I have over 1200 licenses so the page is taking forever to load. How can I optimize this functionality? Any help would be greatly appreciated.



Model



  def self.emp_lic_small
self.all.map{|se| se.employee_licenses.less_than_thirty}.flatten
end

def self.emp_lic_medium
self.all.map{|se| se.employee_licenses.between_thirty_and_ninty}.flatten
end

def self.emp_lic_large
self.all.map{|se| se.employee_licenses.greater_than_ninty}.flatten
end


Controller



    @small_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_small.paginate(:page => params[:small_lic], :per_page => 20)
@medium_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_medium.paginate(:page => params[:med_lic], :per_page => 20)
@large_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_large.paginate(:page => params[:large_lic], :per_page => 20)


View



<div class="panel panel-danger">
<div class="panel-heading"><strong>Employee Licenses Expiring in Less Than 30 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @small_employee_licenses.present? %>
<% @small_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @small_employee_licenses, param_name:'small_lic' unless @small_employee_licenses.blank? %>

<div class="panel panel-warning">
<div class="panel-heading"><strong>Employee Licenses Expiring in 30-90 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @medium_employee_licenses.present? %>
<% @medium_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @medium_employee_licenses, param_name:'med_lic' unless @medium_employee_licenses.blank? %>
<div class="panel panel-success">
<div class="panel-heading"><strong>Employee Licenses Expiring in 30-90 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @large_employee_licenses.present? %>
<% @large_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @large_employee_licenses, param_name:'large_lic' unless @large_employee_licenses.blank? %>









share|improve this question















I am working on a personal project and I have hit a wall. I know I am writing bad code and I really want to refactor the code below. The application has three tables on the same page. Each table contains data from a has-many-though relationship. In essence I have an employees page which contains three tables of employee licenses that all expire in grouped intervals:



ALL EMPLOYEE PAGE




  • Employees with licenses Expiring in 30 days

  • Employees with licenses Expiring in 30-90 days

  • Employees with licenses Expiring in 90 days


All three of these tables are independently paginated and I am allowing the user to enter a search term and search across all three tables.



However I have over 1200 licenses so the page is taking forever to load. How can I optimize this functionality? Any help would be greatly appreciated.



Model



  def self.emp_lic_small
self.all.map{|se| se.employee_licenses.less_than_thirty}.flatten
end

def self.emp_lic_medium
self.all.map{|se| se.employee_licenses.between_thirty_and_ninty}.flatten
end

def self.emp_lic_large
self.all.map{|se| se.employee_licenses.greater_than_ninty}.flatten
end


Controller



    @small_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_small.paginate(:page => params[:small_lic], :per_page => 20)
@medium_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_medium.paginate(:page => params[:med_lic], :per_page => 20)
@large_employee_licenses = SiteEmployee.search(params[:search]).emp_lic_large.paginate(:page => params[:large_lic], :per_page => 20)


View



<div class="panel panel-danger">
<div class="panel-heading"><strong>Employee Licenses Expiring in Less Than 30 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @small_employee_licenses.present? %>
<% @small_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @small_employee_licenses, param_name:'small_lic' unless @small_employee_licenses.blank? %>

<div class="panel panel-warning">
<div class="panel-heading"><strong>Employee Licenses Expiring in 30-90 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @medium_employee_licenses.present? %>
<% @medium_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @medium_employee_licenses, param_name:'med_lic' unless @medium_employee_licenses.blank? %>
<div class="panel panel-success">
<div class="panel-heading"><strong>Employee Licenses Expiring in 30-90 Days</strong></div>
<table class="table">
<thead>
<th class="text-center">Employee Name</th>
<th class="text-center">Employed By</th>
<th class="text-center">License Name</th>
<th class="text-center">Expiration Date</th>
<th class="text-center">Obtained?</th>
</tr>
</thead>
<tbody>
<% if @large_employee_licenses.present? %>
<% @large_employee_licenses.each do |e| %>
<tr>
<td class="text-center"><%= link_to e.site_employee.to_s, site_employee_path(e.site_employee)%></td>
<td class="text-center"><%= link_to e.site_employee.site.name, site_path(e.site_employee.site)%></td>
<td class="text-center"><%= e.license.name %></td>
<td class="text-center"><%= e.expiration_date.strftime("%m/%d/%Y") %></td>
<td class="text-center"><%= e.obtained? ? "Yes" : "No" %></td>
</tr>
<%end%>
<% else %>
<tr><td colspan="3">There are currently no Licenses due in the next 30 days.</td></tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @large_employee_licenses, param_name:'large_lic' unless @large_employee_licenses.blank? %>






performance ruby ruby-on-rails postgresql pagination






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 15 '17 at 16:26









200_success

127k15148412




127k15148412










asked Jan 8 '17 at 19:01









Benjamin

111




111





bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • I'd wager the bottleneck is in the model methods. You're mapping all records. I can't tell from the code exactly what you're scoping that to – what model is this from? And what's se? Are the lists scoped to something before searching? – but you're looking for licenses, but seem to be going the long way of finding licenses through something. It also implies that the pagination happens late - after a lot of stuff has been loaded. So the pagination is the act of throwing a lot of it away, rather than loading what you need. But your question doesn't have enough detail, so I'm not sure.
    – Flambino
    Jan 8 '17 at 19:41










  • Could you post how these tables are related? (the has_many, etc, for the tables in the example?)
    – lcguida
    Jan 26 '17 at 10:48










  • Do you really need all 1200 employees to be displayed before asking the user to search for one? Why not first ask for a search term and then to accordingly display results?
    – BKSpurgeon
    Oct 8 '17 at 5:49










  • @Flambino i think 'se' stands for SiteEmployee
    – BKSpurgeon
    Oct 8 '17 at 5:51


















  • I'd wager the bottleneck is in the model methods. You're mapping all records. I can't tell from the code exactly what you're scoping that to – what model is this from? And what's se? Are the lists scoped to something before searching? – but you're looking for licenses, but seem to be going the long way of finding licenses through something. It also implies that the pagination happens late - after a lot of stuff has been loaded. So the pagination is the act of throwing a lot of it away, rather than loading what you need. But your question doesn't have enough detail, so I'm not sure.
    – Flambino
    Jan 8 '17 at 19:41










  • Could you post how these tables are related? (the has_many, etc, for the tables in the example?)
    – lcguida
    Jan 26 '17 at 10:48










  • Do you really need all 1200 employees to be displayed before asking the user to search for one? Why not first ask for a search term and then to accordingly display results?
    – BKSpurgeon
    Oct 8 '17 at 5:49










  • @Flambino i think 'se' stands for SiteEmployee
    – BKSpurgeon
    Oct 8 '17 at 5:51
















I'd wager the bottleneck is in the model methods. You're mapping all records. I can't tell from the code exactly what you're scoping that to – what model is this from? And what's se? Are the lists scoped to something before searching? – but you're looking for licenses, but seem to be going the long way of finding licenses through something. It also implies that the pagination happens late - after a lot of stuff has been loaded. So the pagination is the act of throwing a lot of it away, rather than loading what you need. But your question doesn't have enough detail, so I'm not sure.
– Flambino
Jan 8 '17 at 19:41




I'd wager the bottleneck is in the model methods. You're mapping all records. I can't tell from the code exactly what you're scoping that to – what model is this from? And what's se? Are the lists scoped to something before searching? – but you're looking for licenses, but seem to be going the long way of finding licenses through something. It also implies that the pagination happens late - after a lot of stuff has been loaded. So the pagination is the act of throwing a lot of it away, rather than loading what you need. But your question doesn't have enough detail, so I'm not sure.
– Flambino
Jan 8 '17 at 19:41












Could you post how these tables are related? (the has_many, etc, for the tables in the example?)
– lcguida
Jan 26 '17 at 10:48




Could you post how these tables are related? (the has_many, etc, for the tables in the example?)
– lcguida
Jan 26 '17 at 10:48












Do you really need all 1200 employees to be displayed before asking the user to search for one? Why not first ask for a search term and then to accordingly display results?
– BKSpurgeon
Oct 8 '17 at 5:49




Do you really need all 1200 employees to be displayed before asking the user to search for one? Why not first ask for a search term and then to accordingly display results?
– BKSpurgeon
Oct 8 '17 at 5:49












@Flambino i think 'se' stands for SiteEmployee
– BKSpurgeon
Oct 8 '17 at 5:51




@Flambino i think 'se' stands for SiteEmployee
– BKSpurgeon
Oct 8 '17 at 5:51










1 Answer
1






active

oldest

votes

















up vote
0
down vote













You should probably reduce the number of database queries, first you repeat the SiteEmployee search 3 times. Do it once and store it in a a variable. I would also query the database once with an order clause together with the Enumerable group_by method to get all of the results in one go.






share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f152067%2flisting-employees-categorized-by-license-expiration-date%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You should probably reduce the number of database queries, first you repeat the SiteEmployee search 3 times. Do it once and store it in a a variable. I would also query the database once with an order clause together with the Enumerable group_by method to get all of the results in one go.






    share|improve this answer

























      up vote
      0
      down vote













      You should probably reduce the number of database queries, first you repeat the SiteEmployee search 3 times. Do it once and store it in a a variable. I would also query the database once with an order clause together with the Enumerable group_by method to get all of the results in one go.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You should probably reduce the number of database queries, first you repeat the SiteEmployee search 3 times. Do it once and store it in a a variable. I would also query the database once with an order clause together with the Enumerable group_by method to get all of the results in one go.






        share|improve this answer












        You should probably reduce the number of database queries, first you repeat the SiteEmployee search 3 times. Do it once and store it in a a variable. I would also query the database once with an order clause together with the Enumerable group_by method to get all of the results in one go.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 10 '17 at 22:45









        Marc Rohloff

        2,98736




        2,98736






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f152067%2flisting-employees-categorized-by-license-expiration-date%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre