Get an execution plan

How to get a query execution plan in SQL Server

Every query is optimized by the SQL Server query optimizer, which builds an execution plan — also called a query plan — to decide the strategy the execution engine will follow.

Looking at that plan is how you find opportunities to optimize a query.

There are two of them:

  • the estimated plan, more accurately described as the plan before execution;
  • the actual plan, more accurately described as the plan after execution.

Estimated plan

  • Display the plan before execution by selecting a query in SSMS and pressing Ctrl+L.
  • You can also use this button in the SSMS toolbar.

Estimated execution plan button in the SSMS toolbar

  • Once the plan is displayed, right-click the graphical plan and choose Save execution plan as… from the context menu.
  • Save it to a file with the .sqlplan extension.

Actual plan

  • Display the plan after execution by selecting a query in SSMS and pressing Ctrl+M.
  • You can also use this button in the SSMS toolbar.

Actual execution plan button in the SSMS toolbar

  • Then run the query. Once it completes, the execution plan appears graphically in a tab of the results pane.

Graphical execution plan in the SSMS results pane

  • Once the plan is displayed, right-click the graphical plan and choose Save execution plan as… from the context menu.
  • Save it to a file with the .sqlplan extension.