C9: Optimize the DB with GitHub Copilot
- Day 2
- Member challenge
- 60 min
Background
Section titled “Background”A migrated database can still be slow. The perf kit planted five performance issues in it. C9 is where you use GitHub Copilot to diagnose each plan and fix it, and prove with Query Store numbers that the fix worked.
Business context. Marcus’s office puts the heaviest load on the database during enrollment weeks. He needs the slow searches fixed before the next one.
Business outcome. Reliable at enrollment peaks. The five slow queries are found, fixed and measured, so the team can say how much faster each one got.
Objective. Be able to find the worst queries in Query Store, fix the cause with Copilot’s help, and show the before and after.
Time and scope. Day 2, 14:00, 60 min, Member.
Challenge
Section titled “Challenge”Starting point. C7’s migrated database on SQL MI. The perf kit’s planted issues and data (200k students, 2M enrollments) are already in it from the datacenter build. If your cutover isn’t complete, work against the source database instead: see the first hint.
Fixed by the kit. The five planted issues, numbered P1 to P5 in this order:
- P1: a missing index.
- P2: a non-sargable search predicate (one the engine can’t answer with an index seek, because the column is wrapped or the pattern starts with
%). - P3: an implicit
sql_variantconversion (the parameter’s type forces SQL Server to convert every row’s value before comparing). - P4: a scalar function forcing a serial plan (a function called once per row that stops the query from running in parallel).
- P5: the database compatibility level.
You decide. How to fix each one, and what the before/after numbers show.
For technical blockers, check Troubleshooting before asking your coach.
Set up once per PowerShell window on vm-dev01
cd C:\src\<your-repo>\factory$s = Get-Content .local/settings.json | ConvertFrom-Jsonaz account set --subscription $s.subscriptionId$n = $s.memberIndex$suffix = $s.suffix$rg = "rg-university-$suffix"$mi = az sql mi show --resource-group $rg --name "sqlmi-university-$suffix" --query fullyQualifiedDomainName --output tsv$mi$ev = "C:\src\<your-repo>\factory\evidence\c09"New-Item -ItemType Directory -Force $ev | Out-NullHow the hour works
Section titled “How the hour works”You run the 5-minute workload twice: once for baseline and once after applying all five fixes (P1–P5). Because the planted issues hit distinct queries, one after run cleanly measures the improvement across all five fixes without needing intermediate checkpoints.
| Minutes | Step |
|---|---|
| 0–8 | Set up, then run the 5-minute baseline workload (Requirement 1) |
| 8–15 | Inspect Top Resource Consuming Queries in Query Store and capture baseline (Requirements 2 and 4) |
| 15–45 | Diagnose and apply all five fixes (p1-*.sql to p5-*.sql) with Copilot (Requirement 3) |
| 45–55 | Run the 5-minute after workload and capture the after Query Store comparison (Requirement 4) |
| 55–60 | Commit the five .sql fix scripts and before/after comparison on main (Requirement 5) |
Requirements
Section titled “Requirements”-
Run the
baselineworkload. vm-dev01 · C:\src\<your-repo>\factory (pwsh) Use the same duration and concurrency for both runs (baselineandafter) so the numbers are comparable:vm-dev01 — C:\src<your-repo>\factory (pwsh) ./db/perf-kit/Start-Workload.ps1 -Server $mi -Authentication ActiveDirectoryInteractive -DurationMinutes 5 -Concurrency 8Sign in as yourself in the browser prompt that opens on the VM and approve MFA. Note the
until HH:mm UTCtime it prints at the start and theFinished at HH:mm UTCline at the end. Don’t runReset-PerfKit.ps1at any point in C9 unless you need to start over from scratch. -
Find the worst queries in Query Store. vm-dev01 · SSMS In SSMS, connect to
$miwith Microsoft Entra MFA using the account you used forazd. Open Databases > ContosoUniversity > Query Store > Top Resource Consuming Queries and use Configure to inspect:- Metric Duration (ms), statistic Total (or Avg), and CPU Time (ms).
- Metric Execution Count.
- Time range: Custom, covering your
baselinerun in UTC.
Select each top bar to inspect its execution plan and query text. Don’t use Force Plan: it pins a plan rather than fixing the underlying issue.
-
Diagnose and fix all five planted issues (
P1toP5). vm-dev01 · VS Code (@mssql) vm-dev01 · SSMS Use GitHub Copilot in VS Code to diagnose each plan and write the fix (p1-<name>.sqltop5-<name>.sqlinfactory/evidence/c09/):- Change each object in place (
ALTERthe same procedure, view or function, with the same name and parameter names), because the workload callsdbo.usp_SearchStudents,dbo.usp_GetStudentEnrollmentsanddbo.vw_EnrollmentStatisticsby name. - Apply all five fixes against
ContosoUniversityon$miin SSMS.
- Change each object in place (
-
Run the
afterworkload and comparebaselinevsafter. vm-dev01 · C:\src\<your-repo>\factory (pwsh) vm-dev01 · SSMS Wait at least 5 minutes after thebaselinerun finished so the two runs don’t share a Query Store interval, then repeat the exact workload command:vm-dev01 — C:\src<your-repo>\factory (pwsh) ./db/perf-kit/Start-Workload.ps1 -Server $mi -Authentication ActiveDirectoryInteractive -DurationMinutes 5 -Concurrency 8Wait a minute for Query Store to flush, then capture your
baselinevsaftercomparison from Top Resource Consuming Queries (or by runningfactory/db/perf-kit/Get-QueryStoreSnapshot.sqlin SSMS for thebaselineandafterUTC windows). -
Commit your five
.sqlscripts and before/after summary onmain. vm-dev01 · C:\src\<your-repo> (pwsh) Save your five.sqlfix scripts (p1-*.sqlthroughp5-*.sql) and a brief before/after comparison infactory/evidence/c09/, then commit and push onmain:vm-dev01 — C:\src<your-repo> (pwsh) cd C:\src\<your-repo>git pull --rebasegit add factory/evidence/c09git commit -m "docs: C9 query tuning fixes and comparison"git push
Success criteria
Section titled “Success criteria”To complete this challenge, check the following in factory/evidence/c09/ on main:
- All five
.sqlfix scripts (p1-*.sqltop5-*.sql) committed infactory/evidence/c09/onmain. - Query Store
baselinevsaftercomparison showing the duration/CPU and logical-read improvements across the tuned queries. - Outcome check: tell Marcus in one line which search improved the most and by how much, and record the headline CPU/duration improvement in your team’s C10 handover table.
My cutover isn't complete, or the managed instance is stopped
If C7 is unfinished, the source has the same planted issues. Run the workload against the source with -MemberIndex $n:
./db/perf-kit/Start-Workload.ps1 -MemberIndex $n -DurationMinutes 5 -Concurrency 8Check MI state with az sql mi show --resource-group $rg --name "sqlmi-university-$suffix" --query state -o tsv. If Stopped, select Start immediately and allow about 20 minutes.
The snapshot script returns no rows
Check the second result: intervals_in_window must be above 0, and the third result shows which five-minute intervals in your window have executions. Your window must be in UTC and cover whole five-minute intervals around the run, and the run must have finished a minute or more ago so Query Store has flushed (or remove the -- in front of the EXEC sys.sp_query_store_flush_db; line and run it once).
If you need to start over
Only start over if an object or the data is broken (for example you dropped the view), because it deletes your baseline:
./db/perf-kit/Reset-PerfKit.ps1 -Server $mi -Authentication ActiveDirectoryInteractiveWarning: the reset puts the five planted issues back, drops every index added since, and clears Query Store.