这个Rails查询是否与它看起来一样糟糕?

时间:2011-06-20 15:18:03

标签: ruby-on-rails

我是RoR土地上的新手,来自20年的非动态语言,并且正在开发一个我没有创建的应用程序,并且仍然试图了解通过常规发生的所有事情(即'魔术' '直到你知道是什么导致它)并尝试调试问题。

我的问题不是针对我追踪的问题,而是想知道这个问题;是这个调试输出真的告诉我10个单独的调用发生在SQL?

Processing OwnersController#stest (for 127.0.0.1 at 2011-06-20 11:08:26) [GET]
  Parameters: {"action"=>"stest", "controller"=>"owners"}
User Columns (4.8ms)  SHOW FIELDS FROM `users`
User Load (0.4ms)  SELECT * FROM `users` WHERE (`users`.`id` = 45241) LIMIT 1
Owner Columns (4.8ms)  SHOW FIELDS FROM `users`
Provider Load (121.6ms)  SELECT * FROM `providers` WHERE (`providers`.owner_id = 45241) LIMIT 1
Provider Columns (4.1ms)  SHOW FIELDS FROM `providers`
FeedItem Load (43.2ms)  SELECT * FROM `feed_items` WHERE (((4 & item_id) > 0)) AND ((feed_items.created_at >= '2011-06-13 15:08:27') AND (feed_items.event = 5)) GROUP BY initiator_type, initiator_id ORDER BY id ASC
Rendering template within layouts/front_end
Rendering owners/stest
FeedItem Columns (2.0ms)  SHOW FIELDS FROM `feed_items`
User Load (0.4ms)  SELECT * FROM `users` WHERE (`users`.`id` = 45268) 
Parent Columns (4.7ms)  SHOW FIELDS FROM `users`
Rendered feed_items/_user_saved_provider_search (23.4ms)
User Load (0.4ms)  SELECT * FROM `users` WHERE (`users`.`id` = 45269) 
Rendered feed_items/_user_saved_provider_search (4.3ms)
User Load (0.4ms)  SELECT * FROM `users` WHERE (`users`.`id` = 45236) 
Rendered feed_items/_user_saved_provider_search (3.7ms)
InHome Columns (3.7ms)  SHOW FIELDS FROM `providers`
ZipCode Load (32.5ms)  SELECT * FROM `zip_codes` WHERE (`zip_codes`.`city` = 'Plano') LIMIT 1
City Columns (3.1ms)  SHOW FIELDS FROM `cities`
City Load (0.4ms)  SELECT * FROM `cities` WHERE (`cities`.`name` = 'Plano') LIMIT 1
CACHE (0.0ms)  SELECT * FROM `zip_codes` WHERE (`zip_codes`.`city` = 'Plano') LIMIT 1
CACHE (0.0ms)  SELECT * FROM `cities` WHERE (`cities`.`name` = 'Plano') LIMIT 1
Rendered layouts/_extra_links (1.7ms)
Completed in 552ms (View: 81, DB: 230) | 200 OK [http://0.0.0.0/owners/stest]

1 个答案:

答案 0 :(得分:2)

根据我的统计,有8个查询,7个描述和2个缓存查询。

如果您处理实际上受数据库查询瓶颈的应用程序,则有几种方法可以优化生成的查询数量。设置范围和使用包含可以减少跨关系获取模型时的查询数。

Rails通常也不会两次发出相同的查询,而是将其缓存,因此为什么这些行以最后两个查询的CACHE开头。