Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instrumenting Database Access

Jeff mentioned in one of the podcasts that one of the things he always does is put in instrumentation for database calls, so that he can tell what queries are causing slowness etc. This is something I've measured in the past using SQL Profiler, but I'm interested in what strategies other people have used to include this as part of the application.

Is it simply a case of including a timer across each database call and logging the result, or is there a 'neater' way of doing it? Maybe there's a framework that does this for you already, or is there a flag I could enable in e.g. Linq-to-SQL that would provide similar functionality.

I mainly use c# but would also be interested in seeing methods from different languages, and I'd be more interested in a 'code' way of doing this over a db platform method like SQL Profiler.

like image 854
Whisk Avatar asked Aug 12 '08 12:08

Whisk


2 Answers

If a query is more then just a simple SELECT on a single table I always run it through EXPLAIN if I am on MySQL or PostgreSQL. If you are using SQL Server then Management Studio has a Display Estimated Execution Plan which is essentially the same. It is useful to see how the engine will access each table and what indexes it will use. Sometimes it will surprise you.

like image 171
John Downey Avatar answered Oct 09 '22 13:10

John Downey


Recording the database calls, the gross timing and the number of records (bytes) returned in the application is useful, but it's not going to give you all the information you need.

It might show you usage patterns you were not expecting. It might show where your using "row-by-row" access instead of "set based" operations.

The best tool to use is SQL Profiler and analyse the number of "Reads" vs the CPU and duration. You want to avoid high CPU queries, high Read's and long durations (duh!).

The "group by reads" is a useful feature to bring to the top the nastiest queries.

like image 35
Guy Avatar answered Oct 09 '22 13:10

Guy