我正在使用大约 1600 行和 4 列的 Excel 电子表格来填充数据库以在 Ruby 应用程序中显示。这导致页面加载时间超过 20 秒。我怎样才能提高性能?
从 db\seeds.rb:
oo = Excelx.new("RatingsDB.xlsx")
oo.default_sheet = oo.sheets.first
2.upto(oo.last_row) do |line|
Rating.create(name: oo.cell(line,'B'), score: oo.cell(line,'C'), when_rated: oo.cell(line,'A'), comment: oo.cell(line,'D'))
end
从 app\controllers\ratings_controller.rb:
def index
@ratings_by_name = Rating.paginate(:order => 'name ASC', :page => params[:page], :per_page =>10, :conditions => ['name like ?', "%#{params[:search]}%"])
@ratings_by_score = Rating.paginate(:order => 'score DESC', :page => params[:page], :per_page =>10, :conditions => ['name like ?', "%#{params[:search]}%"])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @ratings_by_name }
end
end
从 app\views\ratings\index.html.erb:
<div class="tabbable">
<ul class="nav nav-pills">
<li class="active"><a href="#tab1" data-toggle="tab">By Name</a></li>
<li><a href="#tab2" data-toggle="tab">By Score</a></li>
<li><form class="navbar-search">
<input type="text" class="search-query span3" placeholder="Search" name="search">
<div class="icon-search"></div>
</form></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1">
<% @ratings_by_name.each do |rating| %>
<div class="rating">
<div class="row">
<div class="span7">
<strong><%= rating.name %></strong>
<div class="meta">
| <%= link_to 'View', rating %> |
<span class="admin">
<%= link_to 'Edit', edit_rating_path(rating) %> |
</span>
Rated on <%= rating.when_rated %>
</div>
</div>
<div class="span1">
<%= rating.score %>
</div>
</div>
</div>
<% end %>
<%= will_paginate @ratings_by_name %>
</div>