只是做一些Rails并注意到当使用@products.each do
在单个页面上显示30个产品时,好像后台有很多查询,请参阅下面的我的控制台输出。这是对的,还是我读错了?
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE ("categories"."name" = 'bras') LIMIT 1
Product Load (28.0ms) SELECT "products".* FROM "products" WHERE ("products".category_id = 48)
Brand Load (0.2ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
Merchant Load (0.1ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (16.9ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.8ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.7ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.4ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.4ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.6ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.6ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.4ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (2.0ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.4ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.6ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
Rendered products/_product.html.erb (1.5ms)
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
更新:对不起,我是专门讨论CACHE线路,这些是不是每次都会运行这些查询?
CACHE (0.0ms) SELECT "brands".* FROM "brands" WHERE ("brands"."id" = 408) LIMIT 1
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" = 48) LIMIT 1
CACHE (0.0ms) SELECT "merchants".* FROM "merchants" WHERE ("merchants"."id" = 2) LIMIT 1
以下是产品部分代码:
<h3><%= product.product_name %></h3>
<img src="<%= product.image %>" align="right" alt="<%= product.product_name %>" height="200" />
<p><%= truncate(product.product_description, :length => 300, :omission => "...") %></p>
<p><%= product.price %></p>
<p>Brand: <%= product.brand.brand_name %></p>
<p>Category: <%= product.category.category_name %></p>
<p>Merchant: <%= product.merchant.merchant_name %></p>
<p><a href="<%= product.link %>" target="_blank">More information</a></p>
<hr />
答案 0 :(得分:5)
您可能希望急于加载您的产品/商家/等
@product = Product.find(conditions, :include => {:category, :brand, :merchant})
(我不知道你的关系,所以根据需要使用)
编辑你的缓存问题,没有那些查询不是针对服务器运行的,但是你一定要考虑急切加载,这样你就不会进行不必要的查询了
有关详细信息,请参阅以下
http://rails-bestpractices.com/posts/29-fix-n-1-queries
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html&lt; - 参见Eager loading
答案 1 :(得分:2)
那些CACHEd查询只运行一次,但您仍然希望最初加入它们。在您的控制器中,您需要执行@products = Product.all(:include => [:brand, :category, :merchant])
此外,重复渲染一小部分也有点贵。我有更好的运气使部分自己循环在集合上:
产品/ _products.html.erb
<% products.each do |product| %>
<h3><%= product.product_name %></h3>
<img src="<%= product.image %>" align="right" alt="<%= product.product_name %>" height="200" />
<p><%= truncate(product.product_description, :length => 300, :omission => "...") %></p>
<p><%= product.price %></p>
<p>Brand: <%= product.brand.brand_name %></p>
<p>Category: <%= product.category.category_name %></p>
<p>Merchant: <%= product.merchant.merchant_name %></p>
<p><a href="<%= product.link %>" target="_blank">More information</a></p>
<hr />
<% end %>