我有一点轨道问题,我无法弄明白。
本地我正在使用Mongrel运行rails 3.1和ruby 1.9.3,在生产中我正在运行Passanger / Apache2。虽然在本地运行,但在开发环境中,我的数据库查询大约需要40到50秒,这是很多方面。我们在产品表中有大约15,000条记录,在游戏室中有5,000条记录,在collections_items中有1,700条记录,看起来rails正在针对这些整个表进行选择,这导致了减速。
以下是我的模型结构概述:
集合
- has_many CollectionItems
CollectionItem
- belongs_to Collection
- has_many产品
- has_many Playrooms
的产品
- has_many LineItems
- belongs_to ProductType
- belongs_to CollectionItem
的车
- has_many LineItems
的的LineItem
- belongs_to产品
- belongs_to订单
- belongs_to购物车
这是我的日志:
Processing by CartController#index as HTML
Cart Load (0.7ms) SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = 7 LIMIT 1
CollectionGroup Load (1.6ms) SELECT `collection_groups`.* FROM `collection_groups` WHERE (status = 'live')
Rendered cart/_checkout_steps.html.erb (1.2ms)
(0.4ms) SELECT COUNT(*) FROM `line_items` WHERE `line_items`.`cart_id` = 7
LineItem Load (2.0ms) SELECT `line_items`.* FROM `line_items` WHERE `line_items`.`cart_id` = 7
Product Load (33592.3ms) SELECT `products`.* FROM `products`
Product Load (1.8ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 1 LIMIT 1
ProductType Load (181.5ms) SELECT `product_types`.* FROM `product_types`
[paperclip] Duplicate URL for image with /system/:attachment/:id/:style/:filename. This will clash with attachment defined in CollectionItem class
ProductType Load (1.5ms) SELECT `product_types`.* FROM `product_types` WHERE `product_types`.`id` = 1 LIMIT 1
CollectionItem Load (**4701.0ms**) SELECT `collection_items`.* FROM `collection_items`
[paperclip] Duplicate URL for image with /system/:attachment/:id/:style/:filename. This will clash with attachment defined in ProductType class
Collection Load (157.8ms) SELECT `collections`.* FROM `collections`
Playroom Load (18056.7ms) SELECT `playrooms`.* FROM `playrooms`
CollectionItem Load (1.0ms) SELECT `collection_items`.* FROM `collection_items` WHERE `collection_items`.`id` = 1 LIMIT 1
Product Load (1.0ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 527 LIMIT 1
ProductType Load (1.0ms) SELECT `product_types`.* FROM `product_types` WHERE `product_types`.`id` = 5 LIMIT 1
CollectionItem Load (0.8ms) SELECT `collection_items`.* FROM `collection_items` WHERE `collection_items`.`id` = 31 LIMIT 1
Product Load (0.8ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 11 LIMIT 1
ProductType Load (0.7ms) SELECT `product_types`.* FROM `product_types` WHERE `product_types`.`id` = 2 LIMIT 1
CollectionItem Load (1.1ms) SELECT `collection_items`.* FROM `collection_items` WHERE `collection_items`.`id` = 2 LIMIT 1
Product Load (1.0ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 2 LIMIT 1
CACHE (0.0ms) SELECT `product_types`.* FROM `product_types` WHERE `product_types`.`id` = 1 LIMIT 1
CACHE (0.0ms) SELECT `collection_items`.* FROM `collection_items` WHERE `collection_items`.`id` = 1 LIMIT 1
生成此代码的代码块如下:
<% @cart.line_items.each do |line_item| %>
line_item.product.product_type.title
虽然整个网站都出现了问题,但我认为这与我们建立关系的方式有关。我无法弄清楚为什么Rails试图选择产品,游戏室和collection_items的完整表格(这需要45秒)
另外,我们在外键上设置了索引。
任何想法或建议都将不胜感激。