wasAsked.

What is an execution plan in SQL Server and how do you read it?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · SQL

Community answers

Posted by wasAsked editorial collection

An execution plan shows how SQL Server decided to run a query: which indexes it uses, how tables are joined, the order of operations and estimated costs. Reading plans is the main skill for diagnosing slow queries. Types of plans - Estimated execution plan: produced without running the query (SSMS: Ctrl+L, or SET SHOWPLAN_XML ON). Shows estimates only. - Actual execution plan: produced by running the query (SSMS: Ctrl+M, then execute; or SET STATISTICS XML ON). Includes actual row counts, executions and runtime warnings — always prefer it when diagnosing. - Live query statistics: watch progress of a long-running query. - Query Store: stores plans and runtime statistics history, so you can find regressions and force a good plan. How to read a graphical plan - Read it right to left and top to bottom: data flows from the operators on the right (data access) to the SELECT operator on the left. - Each operator shows an estimated cost percentage relative to the whole plan; look for the most expensive operators first. - Arrows show data flow; thicker arrows mean more rows. - Hover over operators (or open Properties) to see estimated vs actual rows, number of executions, predicates, output columns and warnings. Important operators - Clustered Index Seek / Index Seek: navigates the index directly to matching rows — usually good. - Index Scan / Clustered Index Scan / Table Scan: reads the whole index or table — expensive on large tables when only a few rows are needed. - Key Lookup (or RID Lookup on heaps): for each row found in a non-clustered index, SQL Server fetches the remaining columns from the clustered index. Many lookups are costly; fix with a covering index (INCLUDE columns). - Nested Loops: good when one input is small and the other is indexed. - Hash Match: joins or aggregates large unsorted inputs; needs memory. - Merge Join: efficient for two inputs sorted on the join key. - Sort: expensive on large inputs; an index providing the order can remove it. - Parallelism operators: the query used multiple threads. What to look for when a query is slow 1. Big differences between estimated and actual rows: bad estimates lead to bad plans. Causes include outdated statistics, parameter sniffing, table variables, functions in predicates and complex expressions. 2. Scans where seeks are expected: missing indexes or non-sargable predicates such as WHERE YEAR(OrderDate) = 2026 or WHERE LEFT(Name, 3) = 'abc'. Rewrite as ranges or add computed column indexes. 3. Expensive key lookups: add INCLUDE columns to make the index covering. 4. Warnings (yellow triangle): implicit conversions (for example varchar column compared with an nvarchar parameter, preventing seeks), spills to tempdb in sorts or hashes (insufficient memory grant), missing statistics. 5. Missing index suggestions: green text at the top. Treat them as hints, not instructions — evaluate column order and overlap with existing indexes. Supporting tools SET STATISTICS IO, TIME ON; Shows logical reads per table and CPU and elapsed time — a reliable way to compare versions of a query. Example improvement A query filtering WHERE CustomerId = @id ORDER BY OrderDate DESC showed a clustered index scan and a sort. Adding CREATE INDEX IX_Orders_Customer_Date ON Orders(CustomerId, OrderDate DESC) INCLUDE (Total) changed the plan to an index seek with no sort and reduced logical reads from 120,000 to 12. Interview summary: "An execution plan shows how SQL Server executes a query. I read the actual plan right to left, focus on the most expensive operators, compare estimated and actual rows, look for scans, key lookups, sorts and warnings, and confirm improvements with STATISTICS IO and Query Store."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics