Paramclasses Academy - Blog

Tutorials Details

←
Post 32 of 40
→

SQL Server Execution Plans Explained: How to Identify and Fix Slow Queries | Practical Tutorial

SQL Server Execution Plans Explained: How to Identify and Fix Slow Queries | Practical Tutorial

November 18, 2026

???? SQL Server Execution Plans: Identify and Fix Slow Queries

Welcome to Param Classes! In this practical SQL Server tutorial, we will learn how to read SQL Server Execution Plans, identify slow query operations, understand execution plan operators, and troubleshoot database performance problems with practical examples.

Have you ever written a SQL query that returns the correct result but takes too much time to execute? Do you want to understand why SQL Server chooses an Index Scan instead of an Index Seek, or why a JOIN operation consumes more resources than expected?

The answer is often found in the SQL Server Execution Plan.

An execution plan provides a graphical or XML representation of how SQL Server processes a query. By understanding it, developers and database administrators can identify expensive operations and make informed optimization decisions.


???? 1. What Is a SQL Server Execution Plan?

A SQL Server Execution Plan is a set of operations selected by the SQL Server Query Optimizer to execute a query.

It describes how SQL Server accesses tables, uses indexes, joins records, filters data, sorts results, and performs other operations.

Why are execution plans important?

  • ???? Identify expensive query operations.

  • ⚡ Understand slow-running SQL queries.

  • ???? Examine Index Seek and Index Scan operations.

  • ???? Compare estimated and actual row counts.

  • ????️ Find possible indexing and query-design improvements.

  • ???? Understand how SQL Server processes data.

The Query Optimizer considers the SQL statement, database schema, indexes, and statistics when selecting an execution plan.


???? 2. Types of SQL Server Execution Plans

SQL Server Management Studio (SSMS) provides three useful ways to inspect execution plans.

1. Estimated Execution Plan

An Estimated Execution Plan shows the plan SQL Server expects to use without executing the query.

In SSMS:

  • Open a query window.

  • Write your SQL query.

  • Select Display Estimated Execution Plan from the Query menu.

  • You can also use the Ctrl + L shortcut in SSMS.

2. Actual Execution Plan

An Actual Execution Plan shows the plan used after the query has executed. It includes runtime information such as actual rows and execution warnings.

In SSMS:

  • Open your SQL query.

  • Select Include Actual Execution Plan from the Query menu.

  • You can also press Ctrl + M.

  • Execute the query.

  • Open the Execution Plan tab.

3. Live Query Statistics

Live Query Statistics displays execution progress and runtime information while a query is running. It can help investigate long-running queries.

Important Difference:

Feature Estimated Plan Actual Plan
Executes query No Yes
Estimated rows Yes Yes
Actual rows No Yes
Runtime warnings Not runtime-based Available
Best use Inspect planned operations Investigate real execution

????️ 3. Create a Sample Database for Practice

Let's create a database and table to understand execution plans.

CREATE DATABASE ExecutionPlanDB;
GO

USE ExecutionPlanDB;
GO

CREATE TABLE Employees
(
    EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
    EmployeeName VARCHAR(100),
    Department VARCHAR(50),
    Salary DECIMAL(10,2),
    City VARCHAR(50)
);
GO

Insert sample records:

INSERT INTO Employees
(EmployeeName, Department, Salary, City)
VALUES
('Amit Sharma', 'IT', 45000, 'Raipur'),
('Priya Verma', 'HR', 35000, 'Bilaspur'),
('Rahul Patel', 'IT', 55000, 'Raipur'),
('Neha Sahu', 'Finance', 40000, 'Durg'),
('Rohit Kumar', 'IT', 60000, 'Raipur');
GO

Now execute:

SELECT *
FROM Employees
WHERE Department = 'IT';

Enable the Actual Execution Plan in SSMS before executing this query. You can inspect the operations SQL Server selected.

A small table may be scanned because scanning a few pages can be cheaper than using an index. Do not assume every scan indicates a problem.


???? 4. Understand Index Seek and Index Scan

Two important operators you will frequently see in execution plans are Index Seek and Index Scan.

Index Seek

An Index Seek allows SQL Server to navigate an index to locate qualifying key values.

Example:

CREATE NONCLUSTERED INDEX IX_Employees_Department
ON Employees(Department);
GO

SELECT EmployeeID, EmployeeName, Department
FROM Employees
WHERE Department = 'IT';

SQL Server may choose an Index Seek when the index is suitable and the estimated cost supports it.

Index Scan

An Index Scan reads through an index structure to find the required rows. Depending on the query, it may read many or all index entries.

Difference:

Index Seek Index Scan
Navigates to matching key ranges Reads through index entries
Often useful for selective searches Can be suitable for broad searches
May examine fewer rows May examine many rows
Not always the best plan Not always a bad plan

The correct operator depends on the query, table size, selectivity, and available indexes.


???? 5. Understand Important Execution Plan Operators

Execution plans contain different operators that describe how SQL Server processes data.

1. Nested Loops

Nested Loops is a JOIN operator that processes rows from one input and searches the other input for matching rows.

It can be effective when one input contains relatively few rows and the other input has an appropriate access path.

2. Hash Match

Hash Match can be used for joins and other operations. For a hash join, SQL Server builds a hash structure from one input and probes it using rows from another input.

It can be useful when joining larger sets of rows, but may require significant memory.

3. Sort

Sort arranges rows according to required ordering.

For example, an ORDER BY clause may require sorting if an existing access path cannot provide the requested order.

4. Key Lookup

A Key Lookup retrieves additional columns from the clustered index when a nonclustered index does not contain all the data needed by a query.

For a frequently executed query, a suitable covering index may reduce repeated lookups.

5. Table Scan

A Table Scan reads the table's data pages. It may be appropriate when a query needs a large portion of a heap table.

Practical Tip: Never judge performance by an operator name alone. Check the number of rows, execution frequency, logical reads, and the overall query workload.


???? 6. Understand Estimated Rows vs Actual Rows

One important part of execution plan analysis is comparing estimated rows with actual rows.

  • Estimated Rows: The number of rows SQL Server expects an operator to process.

  • Actual Rows: The number of rows the operator really processes during execution.

Example:

Suppose an execution plan estimates 10 rows, but the operator actually processes 50,000 rows.

This large difference may indicate that SQL Server's cardinality estimates are inaccurate.

Possible causes include:

  • Outdated or insufficient statistics.

  • Skewed data distribution.

  • Complex predicates.

  • Correlated column conditions.

  • Parameter-sensitive workloads.

Large estimation errors can influence JOIN selection, memory grants, and other plan decisions.


⚠️ 7. Identify Expensive Operations in an Execution Plan

When analyzing a query, review the following carefully:

  1. Operators processing unexpectedly large numbers of rows.

  2. Repeated Key Lookups.

  3. Large Sort operations.

  4. Hash operations with warnings or spills.

  5. Expensive scans on large tables.

  6. Differences between estimated and actual rows.

  7. Implicit conversions in predicates.

  8. Excessive logical reads.

Important: The percentage cost displayed in a graphical execution plan is an estimated relative cost within the plan. It is not a direct measurement of actual elapsed time. Use runtime metrics and logical reads to understand real performance.


????️ 8. Fix Slow Queries Using Suitable Indexes

Let's consider a query that searches employees by department.

SELECT EmployeeID, EmployeeName, Department
FROM Employees
WHERE Department = 'IT';

If the table contains a large number of records, a suitable index may help:

CREATE NONCLUSTERED INDEX IX_Employees_Department
ON Employees(Department);

For a query that also retrieves salary, consider a covering index:

CREATE NONCLUSTERED INDEX IX_Employees_Department_Include
ON Employees(Department)
INCLUDE (EmployeeName, Salary);

After creating an index, execute the query again and compare the plans and runtime measurements.

Remember: An index should be created only when it supports the actual workload. It also adds storage and maintenance costs.


???? 9. Use STATISTICS IO and TIME

Execution plans show how SQL Server processes a query. Statistics IO and TIME provide additional performance measurements.

SET STATISTICS IO ON;
SET STATISTICS TIME ON;

SELECT EmployeeID, EmployeeName, Salary
FROM Employees
WHERE Department = 'IT';

SET STATISTICS IO OFF;
SET STATISTICS TIME OFF;

Check the Messages tab in SSMS.

Observe:

  • Logical Reads

  • Physical Reads

  • CPU Time

  • Elapsed Time

Compare the results before and after a query or index change under similar test conditions.


???? 10. Save and Compare Execution Plans

SSMS allows you to save execution plans for later analysis.

You can:

  1. Generate an execution plan.

  2. Right-click the plan.

  3. Select Save Execution Plan As.

  4. Save it as an .sqlplan file.

  5. Open and compare it with another execution plan.

Comparing plans can help identify changes in index usage, JOIN operators, row estimates, and other execution details.

This is particularly useful when a query becomes slower after a database or application change.


???? 11. Common Reasons for Slow SQL Queries

Problem Possible Improvement
Missing suitable index Evaluate an appropriate index
Incorrect row estimates Review statistics and predicates
Repeated Key Lookups Consider a covering index
Unnecessary Sort Review ORDER BY and index design
Implicit conversion Use compatible data types
Excessive data retrieval Select only required columns
Large logical reads Review filters and access paths
Hash or Sort spills Investigate memory grants and row estimates

These are investigation points, not automatic fixes. Always verify the actual cause before changing database objects.


❓ 12. Frequently Asked Questions (FAQs)

Q1. What is a SQL Server Execution Plan?

It is a graphical or XML representation of the operations SQL Server selects to execute a query.

Q2. What is the difference between Estimated and Actual Execution Plans?

An Estimated Plan shows the compiled plan without executing the query. An Actual Plan includes runtime information collected after execution.

Q3. Is Index Seek always better than Index Scan?

No. Index Seek is often useful for selective searches, while Index Scan can be more efficient when many rows are required.

Q4. What is a Key Lookup?

A Key Lookup retrieves additional columns from the clustered index when a nonclustered index does not contain all the required data.

Q5. What does a large estimated-versus-actual row difference mean?

It can indicate inaccurate cardinality estimates, potentially caused by statistics, data distribution, or query predicates.

Q6. How can I find slow SQL queries?

Use execution plans, Query Store, STATISTICS IO, STATISTICS TIME, and appropriate SQL Server monitoring tools.


???? 13. Conclusion

SQL Server Execution Plans are essential tools for understanding query performance and troubleshooting slow database operations.

In this tutorial, we learned how to generate Estimated and Actual Execution Plans, identify Index Seek and Index Scan operations, understand Nested Loops, Hash Match, Sort, and Key Lookup operators, compare estimated and actual rows, and investigate expensive query operations.

At Param Classes, we focus on practical SQL Server training so students can understand database performance concepts and apply them to real-world projects.

???? Learn SQL Server. Analyze Execution Plans. Fix Slow Queries. Grow with Param Classes!

←
Post 32 of 40
→