<p>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.</p>

<p>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.</p>

<p>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.</p>

[{'answer_id': 8730, 'author': 'John Downey', 'author_id': 200, 'author_profile': 'https://Stackoverflow.com/users/200', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 8750, 'author': 'BTB', 'author_id': 961, 'author_profile': 'https://Stackoverflow.com/users/961', 'pm_score': 0, 'selected': False, 'text': "<p>If you're writing queries in SQL Management Studio you can enter: <code>SET STATISTICS TIME ON</code> and SQl Server will tell you how long the individual parts of a query took to parse, compile and execute.\nYou might be able to log this information by handling the InfoMessage event of the SqlConnection class (but I think using the SQL Profiler is much easier.)</p>\n"}, {'answer_id': 8802, 'author': 'Guy', 'author_id': 993, 'author_profile': 'https://Stackoverflow.com/users/993', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n\n<p>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.</p>\n\n<p>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!).</p>\n\n<p>The "group by reads" is a useful feature to bring to the top the nastiest queries.</p>\n'}, {'answer_id': 8844, 'author': 'ninesided', 'author_id': 1030, 'author_profile': 'https://Stackoverflow.com/users/1030', 'pm_score': 0, 'selected': False, 'text': '<p>I would have thought that the important thing to ask here is "what database platform are you using?"</p>\n\n<p>For example, in Sybase, installing <a href="http://www.sypron.nl/mda.html" rel="nofollow noreferrer">MDA tables</a> might solve your problem, they provide a whole bunch of statistics from procedure call usage to average logical I/O, CPU time and index coverage. It can be as clever as you want it to be.</p>\n'}, {'answer_id': 9102, 'author': 'Whisk', 'author_id': 908, 'author_profile': 'https://Stackoverflow.com/users/908', 'pm_score': 0, 'selected': False, 'text': '<p>I definitely see the value in using SQL Profiler while you\'re app is running, and EXPLAIN or SET STATISTICS will give you information about individual queries, but does anyone routinely put measurement points into their code to gather information about database queries ongoing - that would pick up on for example, a query on a table that performs fine initially, but as the number of rows grows, becomes slower and slower. </p>\n\n<p>If you\'re using MySQL or Postgre there\'s various tools for seeing query activity in real time, but I haven\'t found a tool as good as the SQL Profiler for measuring query performance over time.</p>\n\n<p>I\'m wondering if there is (or should be?) something similar to <a href="http://code.google.com/p/elmah/" rel="nofollow noreferrer">ELMAH</a> in the way it just plugs in and gives you information without much additional effort?</p>\n'}, {'answer_id': 25162, 'author': 'Douglas Tosi', 'author_id': 626, 'author_profile': 'https://Stackoverflow.com/users/626', 'pm_score': 0, 'selected': False, 'text': '<p>If you\'re into Firebird you may want to watch <a href="http://www.sinatica.com/blog/en/" rel="nofollow noreferrer">sinatica.com</a>.<br>\nWe\'ll soon launch a real-time monitoring tool for Firebird DBAs.</p>\n\n<p>&lt; /shameless plug></p>\n'}, {'answer_id': 2907794, 'author': 'James Kingsbery', 'author_id': 72908, 'author_profile': 'https://Stackoverflow.com/users/72908', 'pm_score': 0, 'selected': False, 'text': '<p>If you use Hibernate (I use the Java version, I\'d imagine NHibernate has something similar), you can have Hibernate collect statistics about lots of different things. See, for example:</p>\n\n<p><a href="http://www.javalobby.org/java/forums/t19807.html" rel="nofollow noreferrer">http://www.javalobby.org/java/forums/t19807.html</a> </p>\n'}]
['https://Stackoverflow.com/questions/8715', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/908/']