3

Is there a way to profile LinqToSql to where I can see how long a query takes in the database, and how long the mapping to objects take?

I am at a point where I have a lightning fast Query as far as I can judge from the SQL Profiler but a slow call from the Application side and I wonder if it's the DataReader > Objects mapping that is slow.

Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • keep in mind there are 3 phases: expression -> dynamic method, execution, materialisation. I have seen major delays on both ends but the bigger pain usually seems to be "expression -> dynamic method". I have seen consistent 80ms+ delays for a slightly complex expression, not counting materialisation. Also, there is the inconsistent perf thing. – Sam Saffron Nov 24 '11 at 12:13
  • shameless plugs: http://samsaffron.com/archive/2011/09/05/Digging+ourselves+out+of+the+mess+Linq-2-SQL+created and http://samsaffron.com/archive/2011/05/02/A+day+in+the+life+of+a+slow+page+at+Stack+Overflow – Sam Saffron Nov 24 '11 at 12:14

2 Answers2

4

Welcome to our world! Yes, we have seen this too. In terms of measuring it, we wrote MvcMiniProfiler such that it could wrap the standard db connection/command/reader etc (since ADO.NET is fairly decorator-friendly); so we can then measure the LINQ overhead via:

using(MiniProfiler.Current.Step("Getting awesome data")) {
    var data = {your query that materializes data, i.e. ToList() etc }
}

then MvcMiniProfiler will show you the time etc of "Getting awesome data", along with the time spent in the SQL queries etc.

We found there was often a very big gap, even when using ExecuteQuery<T>(sql, args), which (along with some CPU activity probing) let us to strongly suspect that materialization was the culprit (in particular when running at high usage; we could see, for example, a 4ms query taking 80+ms due to overheads - so 76ms lost to LINQ). So then we wrote dapper-dot-net, which does an awesome job of slashing the materialization cost. A 4ms query takes 4ms again.

As an example:

enter image description here

the cols are (I've only included 2 rows, so the headings are missing):

step name | time in this step (ms) | offset from start (ms) | sql | time in sql (ms)

As you can see, it took 1.3ms to run the sql, and 1.8ms for the step, so 0.5ms overheads. The "1 sql" is actually a hyperlink to the sql (with parameters/values) that was executed. And you can run it 24x7in production without pain (....we do).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

Yes use Linq2SqlProfiler http://l2sprof.com/.. I am using this to check the hit counts and time of execution etc.

check the usage details on website..

if you are looking for sql server profiler then there is an open source download available on google code.

check following stackoverflow thread link for another ASP.NET MVC Mini Profiler

check the MVC Mini Profiler here : http://code.google.com/p/mvc-mini-profiler/

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75