liveBook · Manning

Execution plan

As explained in 100 SQL Server Mistakes and How to Avoid Them

A query plan is the execution strategy selected or generated for processing a database query.

An execution plan is the sequence of operators SQL Server selects to satisfy a query. It makes the optimizer’s strategy visible, including operations such as sorting and joins. Estimated and actual execution plans help investigate whether a reasonable-looking query is doing unnecessary work. section 5.4 section 10.5

Avoid treating a plan as a command sheet for forcing preferred operations:

  • Mistake: ordering every result. ORDER BY is presentational rather than set-based, and the resulting sort represented 91% of estimated query cost in one example. section 5.4
  • Mistake: assuming the optimizer chose badly because one plan is slow. Plans can vary by environment, while indexes, statistics, and other factors influence selection. section 10.5
  • Mistake: forcing a join operator immediately. Query hints override optimizer decisions and should be exceptional; improving indexes may be safer. section 10.5

Compare the ordered and unordered query

Run SET STATISTICS TIME ON, then compare a plain CustomerContacts query with one ordered by CustomerContactEmail. The example reports 20,679 ms without ordering and 27,415 ms with it; inspect the actual plan in SQL Server Management Studio to see the sort’s relative cost. Do not capture plans while collecting timing data, because measurement results can change. section 5.4

Listing 5.10 Creating a covering index to improve performance
CREATE NONCLUSTERED INDEX 
   [NI-CustomerContactEmail-Include-FirstName-LastName] 
       ON dbo.CustomerContacts(CustomerContactEmail) 
       INCLUDE(CustomerContactFirstName, CustomerContactLastName) ;

Use the covering-index definition as a concrete alternative to paying for avoidable sorting when the query needs email, first name, and last name. section 5.4

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Explains optimizer-selected operators, estimated cost, plan access, and hints, but stops short of exhaustive optimizer internals.
Examples
Compares CustomerContacts scans with and without ORDER BY, then contrasts Nested Loops and forced Hash Join plans for a join.
What it assumes
Assumes basic T-SQL querying and introduces optimizer-selected execution steps, plan inspection, and cautious hint usage.
What you can do after
Diagnose query performance with actual plans, compare operator costs, and prefer indexes or investigation before applying hints.
How it connects
Connects query plans with query optimization, indexes, statistics, sorting, joins, query hints, Query Store, and SQL Server Management Studio.

Also explained in

Compare how 4 books explain execution plan →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.