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.

No Comments, Comment or Ping

Reply to “Tips of the day - Query Trace and Equality”

About

Rowan is a Product Development Manager, specialising in architecting, developing and putting web applications into production - in particular Ruby on Rails based apps. He lives in Toronto, Canada but speaks in a funny accent as he's originally from New Zealand. He's been working in the software and web business for over a decade. This blog covers Web Application development and deployment in the real world, dealing with topics from business fundamentals to Ruby on Rails, Merb, PHP, Flex, MySQL, Apache and more.

Read more ...

 

 

View Rowan Hick's profile on LinkedIn

 

Subscribe to my RSS feed