Online Class • Next Lesson
In this lesson, you will learn how to make queries faster in a professional, repeatable way. You’ll understand how SQL Server
chooses execution plans, what “expensive” really means (reads, CPU, memory, and waiting), and how to rewrite queries to reduce work.
This is one of the highest-value skills for DBAs and developers because most real performance issues are workload + query related.
Query optimization is the practice of making SQL Server do less work while returning the same correct results.
That “work” usually shows up as: too many page reads (logical/physical reads), too much CPU, large memory grants, spills to TempDB,
or blocking/waits. In this class, students learn to:
measure query cost, understand execution plans at a practical level, and apply
high-impact rewrites that improve performance without breaking correctness.
The fastest query is the one that reads the fewest pages and processes the fewest rows.
Optimization is not about “tricks”—it’s about controlling:
row counts, predicate quality, join strategy, sort/hash work,
and plan stability.
90–120 minutes (concepts + demo + rewrite lab)
Every query has two parts: the plan (strategy) and the execution (what actually happened).
Optimization is learning to improve the strategy and validate the execution.
Key teaching point: Most tuning failures happen because estimates are wrong or predicates prevent good index usage.
If the plan is based on wrong assumptions, performance becomes unpredictable.
Before you rewrite anything, you need evidence. In SQL Server, the most beginner-friendly proof is:
reads and CPU time. Reads are often the best indicator of overall workload pressure.
-- Measurement baseline (use in dev/lab safely) SET STATISTICS IO ON; SET STATISTICS TIME ON; -- Run the query here (before any changes) SET STATISTICS IO OFF; SET STATISTICS TIME OFF;
Rule: You are not allowed to claim “improvement” unless you can show before/after metrics
(reads and time) or you can explain why the query is doing less work.
An execution plan is a map of what SQL Server decided to do. You do not need to memorize every operator to tune effectively.
You need to recognize the top operators that usually drive cost and slowdowns.
Key teaching point: Do not tune a plan by “removing operators.” Tune the workload by reducing rows,
improving predicate quality, and choosing correct indexing. Operators are symptoms of required work.
A predicate is SARGable when SQL Server can use an index efficiently (seek or efficient range scan).
Non-SARGable predicates force SQL Server to compute something for many rows, often turning seeks into scans.
WHERE YEAR(OrderDate) = 2026LIKE '%term' (usually cannot seek)YEAR(OrderDate)=2026 with a range:
OrderDate >= '20260101' AND OrderDate < '20270101'
LIKE 'term%' over LIKE '%term' when possibleKey teaching point: Great indexing cannot save a non-SARGable predicate.
Query rewriting and indexing work together.
SQL Server has to process rows before it can return results. If your query reads 10 million rows to return 100 rows,
you have a row-reduction problem. Most of the time, performance improves by pushing filters earlier and selecting fewer columns.
Joins and aggregations are where SQL Server often spends the most time. The goal is to make joins join fewer rows,
use indexes correctly, and avoid unnecessary sorting/hashing.
Key teaching point: “Expensive” usually means “large row sets.”
If you reduce rows before joins/aggregations, the plan usually gets faster automatically.
Demo goal: take a slow query and improve it using a small rewrite (SARGability, fewer columns, better filtering),
then prove improvement with IO/TIME and plan changes.
-- Demo: measure first SET STATISTICS IO ON; SET STATISTICS TIME ON; -- 1) Run the original query (capture reads + CPU time) -- - Identify: scan? lookup? big sort? hash join? warnings? -- 2) Apply ONE change (example: SARGable date range, remove function on column) -- 3) Re-run query and compare: -- - logical reads -- - CPU time -- - elapsed time -- - plan operators SET STATISTICS IO OFF; SET STATISTICS TIME OFF;
Students will identify a non-SARGable predicate, rewrite it into a SARGable form, and prove improvement using reads/time.
This lab trains students to tune safely and communicate results with evidence.
Prompt:
“Which is more valuable for performance: better indexes or better queries? Choose a side and justify it.
Your answer must mention reads (IO) and either plan stability, concurrency, or maintenance cost.”
In the next lesson, we move into Monitoring and Troubleshooting SQL Server Performance.
Query optimization improves individual queries, but DBAs must also diagnose system-wide bottlenecks using waits, DMVs,
Query Store, blocking analysis, and performance baselines.
Not a member yet? Register now
Are you a member? Login now