Slow queries waste time and hardware. This guide shows how to find out why a query is slow before you change anything: read an execution plan, measure with STATISTICS IO, understand scan, seek and key lookup, write sargable queries, build a covering index, and keep statistics fresh.
Part of the SQL Server for Beginners series. This article is the written version of lesson 13 of the Bigland Network video course, with the same sample data. The results shown were worked out by hand from that data, so run every example on a test database, not on production data. The page counts in the measuring section are illustrative numbers for a big table.
Watch lesson 13 as a video or open it on YouTube. The written lesson below follows the same steps and the same sample data.
Contents
- 1. Why is a query slow?
- 2. Execution plans and operators
- 3. Measure with STATISTICS IO and TIME
- 4. Scan or seek: create an index
- 5. Sargable queries
- 6. LIKE and the leading wildcard
- 7. Select only needed columns, covering indexes
- 8. Statistics, data types and hints
- 9. Find slow queries with Query Store
- 10. A fix checklist, mistakes and best practices
- 11. Practice
1. Why is a query slow?
- A scan: SQL Server reads every row of the table.
- No index: nothing helps it find rows fast.
- Old statistics: its estimates are wrong, so it picks a bad plan.
- Too much:
SELECT *, or far more rows than needed.
First find the reason, then fix it. Changing things at random is like pressing every button on a slow coffee machine.
2. Execution plans and operators
In SQL Server Management Studio press Ctrl+L for the estimated plan (the query does not run), or Ctrl+M and then run the query for the actual plan. Read the plan from right to left, from the data to the result. Thick arrows and a high cost percent show the hot spot.
| Operator | Meaning |
|---|---|
| Scan | Reads the whole table or index. Fine for small tables. |
| Seek | Jumps straight to the rows using an index. Usually good. |
| Key lookup | An extra trip back to the table for missing columns |
| Sort / Hash | Heavy steps for ORDER BY, joins and grouping |
3. Measure with STATISTICS IO and TIME
SET STATISTICS IO ON;
SET STATISTICS TIME ON;
SELECT FirstName, Salary
FROM Employees
WHERE Salary > 4500;
The Messages tab shows the logical reads (pages read) and the time. Fewer reads means a faster query. Example numbers for a big table:
| Query | Logical reads |
|---|---|
| Scan, no index | 5200 |
| Seek, with index | 3 |
4. Scan or seek: create an index
Think of a phone book. A scan reads it from page one to find one name. A seek opens it at the right letter. The index is the alphabet order that makes the seek possible.
CREATE NONCLUSTERED INDEX IX_Emp_Salary
ON Employees (Salary)
INCLUDE (FirstName);
SELECT FirstName, Salary FROM Employees
WHERE Salary > 4500 ORDER BY Salary;
| FirstName | Salary |
|---|---|
| Nadia | 4800 |
| Youssef | 5200 |
The plan now shows an Index Seek. More about indexes: SQL Server Indexes Explained.
5. Sargable queries
A calculation on the column hides it from the index. This returns the right rows, but SQL Server must compute for every row, so it cannot seek:
SELECT FirstName, Salary
FROM Employees
WHERE Salary * 12 > 54000;
Move the math to the other side, so the column stays alone on the left:
SELECT FirstName, Salary
FROM Employees
WHERE Salary > 54000 / 12;
| FirstName | Salary |
|---|---|
| Youssef | 5200 |
| Nadia | 4800 |
Both queries return the same two rows (54000 divided by 12 is 4500), but only the second one can seek. Such a query is called sargable, short for “search argument able”. The rule: no math and no function on the column.
6. LIKE and the leading wildcard
WHERE FirstName LIKE 'A%' -- Amina: can seek
WHERE FirstName LIKE '%a' -- Amina, Salma, Nadia: must scan
| Pattern | Rows |
|---|---|
| LIKE ‘A%’ | 1 |
| LIKE ‘%a’ | 3 |
A percent sign at the start kills the seek. LEFT(FirstName, 1) = 'A' is a function on the column, so it is not sargable either.
7. Select only needed columns, covering indexes
-- risky
SELECT * FROM Employees WHERE Salary > 4500;
-- better
SELECT FirstName, Salary FROM Employees WHERE Salary > 4500;
With SELECT *, the index above cannot cover the query, and SQL Server adds a key lookup for every row. The cure is a covering index: put the needed columns in INCLUDE, and the index has everything. But every index makes INSERT, UPDATE and DELETE a little slower, so index for your real, frequent queries only.
8. Statistics, data types and hints
Statistics are the map of your data: they tell SQL Server how many rows match. If the map is old, the plan is bad.
UPDATE STATISTICS Employees;
UPDATE STATISTICS Employees IX_Emp_Salary
WITH FULLSCAN;
EXEC sp_updatestats;
Keep AUTO_UPDATE_STATISTICS on. Another quiet trap is the data type:
-- FirstName is VARCHAR, but we send NVARCHAR
SELECT Id FROM Employees
WHERE FirstName = N'Amina';
SQL Server may convert the whole column first, and the seek is lost. The plan shows a yellow warning triangle. Match the type of the column. SQL Server can also suggest indexes it wishes it had:
SELECT TOP 3 d.statement AS TableName,
d.equality_columns, d.included_columns
FROM sys.dm_db_missing_index_details d;
Read these as ideas, not orders, and never create them all.
9. Find slow queries with Query Store
ALTER DATABASE CompanyDB SET QUERY_STORE = ON;
Query Store is built in and remembers plans and run times for each query. In SSMS open the report Top Resource Consuming Queries and start from the worst one. For a wider guide see How to Find and Fix Slow Queries in SQL Server.
10. A fix checklist, mistakes and best practices
- Look at the actual plan. Find the thick, costly step.
- Scan on a big table? Check that the WHERE clause is sargable.
- Key lookups? Try an index with INCLUDE.
- Estimates far from actual rows? Update statistics.
- Measure again. Keep the change only if it is better.
- A function or math on the column in WHERE.
SELECT *everywhere.- An index on every column, just in case.
- Tuning without measuring first.
- Using
NOLOCKor hints as a magic fix. - Best practice: measure with the actual plan and
STATISTICS IO, write sargable conditions, select only needed columns, index for real queries, keep statistics fresh, use Query Store, test on realistic data (everything is fast with six employees), and change one thing at a time.
11. Practice
- Rewrite
WHERE Salary * 12 > 54000so that an index can be used. - Create an index that covers FirstName and Salary of department 2, ordered by Salary.
- Rewrite
LEFT(FirstName, 1) = 'A'as sargable, then update the statistics.
SELECT FirstName, Salary FROM Employees
WHERE Salary > 54000 / 12;
CREATE NONCLUSTERED INDEX IX_Emp_Dept
ON Employees (DeptId, Salary)
INCLUDE (FirstName);
SELECT FirstName, Salary FROM Employees
WHERE DeptId = 2 ORDER BY Salary; -- Nadia 4800, Youssef 5200
SELECT FirstName FROM Employees
WHERE FirstName LIKE 'A%'; -- Amina
UPDATE STATISTICS Employees;
Frequently asked questions
What is a sargable query?
A query whose conditions let SQL Server use an index seek. The column stands alone in the condition, with no math or function applied to it.
Is a scan always bad?
No. On a small table, or when you need most of the rows, a scan is fine. It becomes a problem on big tables when you only need a few rows.
Should I create every missing index SQL Server suggests?
No. Treat the suggestions as ideas, check them against your real queries, and remember that each index slows down writes.
What to learn next
The next lesson, Lesson 14: SQL Server Agent Jobs and Maintenance, covers how to automate backups and index care. See also SQL Server Agent Jobs. Previous lesson: Lesson 12: Backup, Restore and Recovery Models.
The whole series
- Lesson 1: SELECT, WHERE and ORDER BY
- Lesson 2: JOINs
- Lesson 3: GROUP BY, HAVING and Subqueries
- Lesson 4: INSERT, UPDATE, DELETE and Transactions
- Lesson 5: CREATE TABLE, Data Types and Constraints
- Lesson 6: Indexes, Views and Stored Procedures
- Lesson 7: CTEs and Window Functions
- Lesson 8: Built-in Functions
- Lesson 9: Functions, Temp Tables and Table Variables
- Lesson 10: Triggers and Audit Tables
- Lesson 11: Security: Logins, Users and Roles
- Lesson 12: Backup, Restore and Recovery Models
- Lesson 13: Query Performance Tuning (this article)
- Lesson 14: SQL Server Agent Jobs and Maintenance
- Lesson 15: Encryption and Auditing Basics
- Lesson 16: Working with JSON
- Lesson 17: Temporal Tables and Row History
- Lesson 18: Partitioning and Large Tables
- lesson 19: Full Text Search
- lesson 20: Query Store and Slow Queries
Sources and further reading
- Microsoft Learn, Transact-SQL reference (Database Engine), on learn.microsoft.com: the official syntax for the statements used here.
- Microsoft Learn, SQL Server documentation, for SQL Server Management Studio (SSMS) and the sample concepts.
More tutorials like this
Msg 208 Invalid Object Name in SQL Server: Causes and Fix
Fix SQL Server Msg 208 Invalid object name: check the spelling, the database and the schema, with real SSMS examples and scripts to find…
SQL Server Query Store Tutorial: Find Slow Queries, Compare Plans and Force a Plan
Turn on Query Store, find the most expensive queries with the sys.query_store views and the SSMS reports, compare the plans of one query and…
SQL Server Full Text Search Tutorial: CONTAINS, FORMSOF and CONTAINSTABLE
Search words inside text columns fast: create a full text catalog and index, use CONTAINS, word forms, phrases and rank the best matches with…
