"Doubt is uncomfortable, certainty is ridiculous." - Voiltaire

Answer by jfar for Good way to time SQL queries when using Linq to SQL

Answer by jfar for Good way to time SQL queries when using Linq to SQL
StackOverflow.com

What you could do is add a custom TextWriter implementation to the DataContext.Log which will write the generated sql to a file or memory. Then loop through those queries, executing them with raw ADO.NET code, surrounding each with a stopwatch.

I’ve used a similar technique before because it seemed whenever I was developing some code I never had Profiler open, and it was really easy to output those results to a HTML page. Sure your executing them twice per website request, but its helpful to see execution times asap instead of waiting until you catch something in Profiler.

Also if your going to the SQL Tool route I would recommend googling “slowest query DMV” and getting a stored procedure that can give you stats on the slowest queries in your db. Its not always easy to scroll through profiler results to find the bad queries. Also with the right queries over sql 2005’s dmv you can also do ordering by lets say cpu vs. time and so forth.


 
 
 

Leave a Reply