I need to get the execution plan of all Linq To Db queries for my application, and them to a database table for any query that takes longer than 2 minutes. How do I get an execution plan, and is there one place where I can do this for all queries in the app?
Asked
Active
Viewed 94 times
0
-
For sure there is no way. Execution plan is created by Database Engine, SQL Server for example. You have to setup some monitoring tool like [Solarwinds DPA](https://www.solarwinds.com/database-performance-analyzer) (free for 14 days) and analyse performance gaps. – Svyatoslav Danyliv Mar 29 '23 at 19:55
-
Check this out https://linq2db.github.io/ – Muhammad Saqlain Mar 29 '23 at 19:57
-
If it is difficult to identity which LINQ query is slow, use `query.TagQuery("My super dashboard query")` and `linq2db` will add comment to the query. – Svyatoslav Danyliv Mar 29 '23 at 20:00
1 Answers
1
You can add Tracing to DataOptions
var options = new DataOptions();
options = options
.UseSqlServer(connectionString)
.UseTracing(TraceLevel.Info, ti =>
{
if (ti.TraceInfoStep == TraceInfoStep.AfterExecute && ti.ExecutionTime != null)
{
Console.WriteLine(ti.SqlText);
// here you can filter out long running queries
Console.WriteLine("Elapsed: " + ti.ExecutionTime.TotalSeconds);
}
});
using var dc = new DataConnection(options);
Collecting execution plan is not possible. It is done on the Database engine side.

Svyatoslav Danyliv
- 21,911
- 3
- 16
- 32