DucDigital
DucDigital

Reputation: 4622

Determine the number of Linq-To-Sql queries used in processing an ASP.NET MVC URL?

I'm using Linq To Sql now, and how can I know how many queries was used to show the current page?

thank you very much

Upvotes: 1

Views: 202

Answers (8)

Dmytrii Nagirniak
Dmytrii Nagirniak

Reputation: 24098

Use Linq2Sql Profiler.

Upvotes: 1

DM.
DM.

Reputation: 1847

I believe your best bet is write an HTTP module that would display the number of queries executed as well as the SQL that was generated at the bottom of each page. It's amazingly slick. I'm not going to write it out here for obvious reasons but pick up a copy of Steve Sanderson's book "Pro ASP.NET MVC Framework" and he walks you through it. The output looks like:


Executed 4 SQL queries

  1. SELECT ...

  2. UPDATE ...

  3. UPDATE ...

  4. UPDATE ...


Upvotes: 1

KristoferA
KristoferA

Reputation: 12397

You may want to try out my Linq-to-SQL profiler. You can read more, download it and get a free 45-day trial license from http://www.huagati.com/L2SProfiler/

It give you not only what queries were executed, but what code triggered them, what the I/O cost, db-side timings, even the db side execution plan. You can use it both during development and in production environments to log and profile L2S queries...

Also see this blog post: http://huagati.blogspot.com/2009/06/profiling-linq-to-sql-applications.html

Upvotes: 2

Jonathan
Jonathan

Reputation: 12025

If I were you, I will combine the DaveG solution(+1) with the Jan solution(+1).

You can put the association of the textwriter with the datacontext.log in the constructor of the DataContext, and use the #if preprocessor directive like this:

#if DEBUG
    DataContext.Log = yourTextWriter.out;
#endif

This way will improve your performance in release mode.

Upvotes: 1

TGnat
TGnat

Reputation: 4001

I would use SQL Server Profiler.

Upvotes: 4

Jan Jongboom
Jan Jongboom

Reputation: 27322

You can implement a textwriter and associate your new textwriter with the datacontext.log; when the datacontext tries to write you can update your counter.

Upvotes: 3

David Glenn
David Glenn

Reputation: 24522

The DataContext contains a TextWriter to write the generated SQL to if thats any help to you

DataContext.Log = System.Console.Out;

will output the generated SQL to the console

Upvotes: 4

LiamB
LiamB

Reputation: 18586

I believe you will need to keep an internal counter to keep track.

Upvotes: 1

Related Questions