ruby - Rails too many queries? -
just doing rails , noticed when displaying 30 products on single page using @products.each do
appears though there lot of queries going on in background, see below console output. right or reading entirely wrong?
category load (0.1ms) select "categories".* "categories" ("categories"."name" = 'bras') limit 1 product load (28.0ms) select "products".* "products" ("products".category_id = 48) brand load (0.2ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 category load (0.1ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 merchant load (0.1ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (16.9ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.8ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.7ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.4ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.4ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.6ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.6ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.4ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (2.0ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.4ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.6ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1 rendered products/_product.html.erb (1.5ms) cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1
update: sorry talking cache lines, these mean these queries not run everytime?
cache (0.0ms) select "brands".* "brands" ("brands"."id" = 408) limit 1 cache (0.0ms) select "categories".* "categories" ("categories"."id" = 48) limit 1 cache (0.0ms) select "merchants".* "merchants" ("merchants"."id" = 2) limit 1
here code product partial:
<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 />
you might want eager load products/merchants/etc
@product = product.find(conditions, :include => {:category, :brand, :merchant})
(i don't know relations use needed)
edit in regards cache question, no queries aren't run against server, should eager loading don't have unnecessary querying going on
for more details see following
http://rails-bestpractices.com/posts/29-fix-n-1-queries
http://api.rubyonrails.org/classes/activerecord/associations/classmethods.html <- see eager loading
Comments
Post a Comment