I am brand new to RoR land, coming from 20 years of non-dynamic languages, and working on an app I did not originate, and still trying to get my head around all the things that happen through convention (i.e. 'magic' until you know what caused it) and trying to debug an issue.
My question isn't specific to the problem I'm tracking down, but rather wanting to know this; is this debug output really telling me that 10 separate calls are happening to 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]
By my count there are 8 Queries, 7 Describes and 2 Cached queries.
If your dealing with an application that is actually being bottlenecked by database queries there are a few ways to optimize the number of queries generated. Setting up scopes and using includes can reduce the number of queries when fetching models across relations.
Rails also won't normally issue the same query twice, instead it caches it, hence why the lines start with CACHE for those last two queries.
精彩评论