Tags: how-to rails ruby-on-rails

Tips of the day - Query Trace and Equality

Tip #1 Query Trace So you're profiling your application, cutting down your SQL queries, looking through the logs - "argh where did that query come from ?!" you say to yourself. Then you remember what Luke G said at the last Toronto Rails Project nite - check out Query Trace. Oh how this has made my life so much better. It tells you the stack trace at the point the query was executed, colorised nicely so you don't go blind tailing your log files. Here's a very simple example so you get the idea.. Query trace output Get the plugin from here https://terralien.devguard.com/svn/projects/plugins/query_trace/README Tip #2 Object equality vs checking id's When checking for ownership or association between objects, be careful you're not checking for equality of the objects. For example "did person x create this order y" the temptation is simply to go "if person == order.creator", where you're checking the objects themselves. Careful! As this is actually loading up the creator of the order if it's not already loaded (SELECT * FROM people WHERE person.id = x). Simply check if person.id = order.creator_id, as this won't require the order's creator to be loaded, eliminating one less hit to the DB. Little things like this littered through an application can start really slowing things down if you're not careful.
blog comments powered by Disqus
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.