11

Is it possible to get the execution plan of a LINQ to SQL or ADO.NET Query programatically for displaying in debug information? If so, how?

Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
Chad Moran
  • 12,834
  • 2
  • 50
  • 72
  • I don't know about LINQ specifically but here is a solution for programatically obtaining query plan. To do the same for LINQ it seems you'd have to modify the generated files heavily. Should be doable, complex, but doable. http://social.msdn.microsoft.com/Forums/en-US/sqldatabaseengine/thread/aaf5e9c9-a222-414a-baed-5c0adcc329e2 – RThomas May 12 '11 at 15:57
  • 1
    Did you accomplish this? I'd kind of like to see the solution if you can re post at some point. – RThomas May 14 '11 at 05:42

1 Answers1

4

Sure, there are 2 things you will need.

A custom implementation of DbConnection, DbCommand and DbDataReader. You can use that to intercept all the SQL sent to the DB. You basically set it up so you have a layer that logs all the SQL that is run. (we plan to open source something in this area in the next few months, so stay tuned)

A way to display an make sense of the data, which happens to be open source here: https://data.stackexchange.com/stackoverflow/s/345/how-unsung-am-i (see the include execution plan option)


Another approach is to do the diagnostics after the fact by looking at the proc cache. sys.dm_exec_query_stats contains cached plan handles which you can expand.

Community
  • 1
  • 1
Sam Saffron
  • 128,308
  • 78
  • 326
  • 506