Ruby on rails Rails-通过数据库查询加速我的项目索引操作
我注意到,随着我不断开发我的应用程序,创建了很多项目/列表,加载时间越来越长。我怀疑这是我在控制器中查询数据库的方式。视图使用erb执行@item.each do并检查@item.orders.any?并且只显示没有订单的项目。代码基本上必须遍历数据库中的每一项执行此检查,然后呈现结果,我怀疑这将是一场超过100项的噩梦 有没有更聪明的方法 这是一个简单的市场应用程序,买家和卖家可以在其中进行交易。卖家列出一个项目,它与其他项目一起显示在项目索引页面上。买家点击它,然后点击购买,这会将他们带到一个订单页面,然后他们可以与贝宝签出。收到PayPal IPN后,订单将提交到数据库 代码如下: 加载索引操作时从服务器日志中摘录 项目控制器Ruby on rails Rails-通过数据库查询加速我的项目索引操作,ruby-on-rails,ruby,database,postgresql,model-view-controller,Ruby On Rails,Ruby,Database,Postgresql,Model View Controller,我注意到,随着我不断开发我的应用程序,创建了很多项目/列表,加载时间越来越长。我怀疑这是我在控制器中查询数据库的方式。视图使用erb执行@item.each do并检查@item.orders.any?并且只显示没有订单的项目。代码基本上必须遍历数据库中的每一项执行此检查,然后呈现结果,我怀疑这将是一场超过100项的噩梦 有没有更聪明的方法 这是一个简单的市场应用程序,买家和卖家可以在其中进行交易。卖家列出一个项目,它与其他项目一起显示在项目索引页面上。买家点击它,然后点击购买,这会将他们带到一
class ItemsController
没有包含该术语的项目。
项目
以前
谢谢你的时间 尝试使用
includes
加载关联
class ItemsController < ApplicationController
def index
if params[:search]
@items = Item.search(params[:search]).includes(:orders).order("created_at DESC")
else
@items = Item.all.includes(:orders).order("created_at DESC")
end
end
end
class ItemsController
谢谢!这很有效。服务器日志不再显示每个项目查询并执行订单加载,而不是订单存在。平均加载时间减少了约25毫秒。@bloodrunner好极了!作为表示感谢的方式,如果你对答案感到满意,你能接受吗?
class ItemsController < ApplicationController
before_action :set_item, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
rescue_from ActiveRecord::RecordNotFound, with: :deny_access
rescue_from ActionView::MissingTemplate, with: :template_not_found
def garage
@items = current_user.items
end
def show
@item = Item.find(params[:id])
end
# GET /items
# GET /items.json
def index
@items = Item.all
if params[:search]
@items = Item.search(params[:search]).order("created_at DESC")
else
@items = Item.all.order("created_at DESC")
end
end
# GET /items/new
def new
@item = current_user.items.build
end
# GET /items/1/edit
def edit
end
# POST /items
# POST /items.json
def create
@item = current_user.items.build(item_params)
@item.username = current_user.username
respond_to do |format|
if @item.save
format.html { redirect_to @item, notice: 'Item was successfully created.' }
format.json { render :show, status: :created, location: @item }
else
format.html { render :new }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /items/1
# PATCH/PUT /items/1.json
def update
respond_to do |format|
if @item.update(item_params)
format.html { redirect_to @item, notice: 'Item was successfully updated.' }
format.json { render :show, status: :ok, location: @item }
else
format.html { render :edit }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /items/1
# DELETE /items/1.json
def destroy
@item.destroy
respond_to do |format|
format.html { redirect_to items_url, notice: 'Item was successfully destroyed.' }
format.json { head :no_content }
end
end
def favorite
@item = Item.find(params[:id])
type = params[:type]
if type == "favorite"
current_user.favorites << @item
redirect_to :back
elsif type == "unfavorite"
current_user.favorites.delete(@item)
redirect_to :back
else
redirect_to :back
end
end
def favorites
@items = current_user.favorites
end
def deny_access
redirect_to :back
rescue ActionController::RedirectBackError
redirect_to root_path
end
def template_not_found
redirect_to :back
rescue ActionView::MissingTemplate
redirect_to root_path
end
private
# Use callbacks to share common setup or constraints between actions.
def set_item
@item = Item.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def item_params
params.require(:item).permit(:title, :content, :filepicker_url, :price, :shipping_price, :paypal_email)
end
end
<div class="row">
<%= form_tag(items_path, :method => "get") do %>
<div class="col-lg-6 col-lg-offset-3">
<div class="input-group">
<%= text_field_tag :search, params[:search], placeholder: "Search items...", class: "form-control" %>
<div class="input-group-btn">
<%= submit_tag "Search", :name => nil, class: "btn btn-default btn-block" %>
</div>
</div>
</div>
<% end %>
</div>
<br>
<% if @items.blank? %>
<h4>There are no items containing the term <%= params[:search] %>.</h4>
<% end %>
<h1>Items</h1>
<% @items.each do |item| %>
<% if item.orders.any? %>
<% else %>
<div class="col-md-3">
<%= link_to(filepicker_image_tag(item.filepicker_url.split(",").last, w: 200, h: 200, fit: 'crop'), item) %><br>
<p class="bold"><%= item.title %><br></p>
<p><%= link_to number_to_currency(item.price), item %></p>
<p class="small"><%= time_ago_in_words(item.created_at) %> ago</p>
</div>
<% end %>
<% end %>
<br>
class ItemsController < ApplicationController
def index
if params[:search]
@items = Item.search(params[:search]).includes(:orders).order("created_at DESC")
else
@items = Item.all.includes(:orders).order("created_at DESC")
end
end
end