Sql-server – reliable way to determine how long it took the optimizer to generate a query plan

execution-planoptimizationsql serversql-server-2016statistics

I'm not sure where I'd start, but is there a way to see how long it took the optimizer to generate a query plan for a query? Is it stored in any of the DMVs or something that's part of one of the statistics? Or can I calculate it somehow if I include live statistics or the actual execution plan? Maybe in the query store?

Best Answer

There are many ways :

  • If you can run the query
    • you can use set statistics time on which will give you:

      SQL Server parse and compile time: CPU time = xx ms, elapsed time = xx ms.

  • If you cant run the query, then mine the plan cache to check for query compile time along with many other significant insights.

There are many other ways like profiler, server side trace or XEvents - but they are expensive and might have negative perf impact on your sql server.

You can use sp_BlitzCache to find out Query Compilation Timeout from plan cache.