views:

440

answers:

6

In Maybe Normalizing Isn't Normal Jeff Atwood says, "You're automatically measuring all the queries that flow through your software, right?" I'm not but I'd like to.

Some features of the application in question:

+2  A: 

SQL Profiler is the tool I use to monitor traffic flowing to my SQL Server. It allows you to gather detailed data about your SQL Server. SQL Profiler has been distributed with SQL Server since at least SQL Server 2000 (but probably before that also).

Highly recommended.

Brad Leach
+5  A: 

In addition to Brad's mention of SQL Profiler, if you want to do this in code, then all your database calls need to funnelled through a common library. You insert the timing code there, and voila, you know how long every query in your system takes.

A single point of entry to the database is a fairly standard feature of any ORM or database layer -- or at least it has been in any project I've worked on so far!

Jeff Atwood
A: 

The Dropthings project on CodePlex has a class for timing blocks of code. The class is named TimedLog. It implements IDisposable. You wrap the block of code you wish to time in a using statement.

A: 

If you use rails it automatically logs all the SQL queries, and the time they took to execute, in your development log file.

I find this very useful because if you do see one that's taking a while, it's one step to just copy and paste it straight off the screen/logfile, and put 'explain' in front of it in mysql.

You don't have to go digging through your code and reconstruct what's happening.

Needless to say this doesn't happen in production as it'd run you out of disk space in about an hour.

Orion Edwards
+1  A: 

Take a look at this chapter Jeff Atwood and I wrote about performance optimizations for websites. We cover a lot of stuff, but there's a lot of stuff about database tracing and optimization: Speed Up Your Site: 8 ASP.NET Performance Tips

Jon Galloway
A: 

If you define a factory that creates SqlCommands for you and always call it when you need a new command, you can return a RealProxy to an SqlCommand.

This proxy can then measure how long ExecuteReader / ExecuteScalar etc. take using a StopWatch and log it somewhere. The advantage to using this kind of method over Sql Server Profiler is that you can get full stack traces for each executed piece of SQL.

Sam Saffron