Test an index with measured plan evidence
Compare actual plans and logical reads before and after a covering index on a temporary training table.
What you are trying to do
A reporting query filters events by customer and date. Use SQL Server Management Studio connected to a scratch SQL Server database. The script creates and drops only a local temporary table. Enable Include Actual Execution Plan before running it.
Work through it
- Run the first SELECT and save its actual plan, logical read count, CPU time, and elapsed time from the Messages and Execution Plan panes.
- Create the nonclustered index on the equality key and date range, with the output amount included. Run the same SELECT again.
- Compare rows returned first. Then compare logical reads and inspect whether the new plan reads fewer pages or avoids a lookup. Do not assume a seek must win for every data size or filter.
- Record SQL Server version, compatibility level, row count, and both measurements. A single elapsed-time sample is noisy, so repeat under the same conditions before claiming a gain.
- Drop the temporary table and turn the statistics setting off. Read Microsoft's index design guidance before applying a design to permanent tables. Index design guide · Actual and estimated plans
Check your work
Both SELECT statements return the same rows and values. The evidence log records logical reads and an actual plan for each version. Any performance conclusion is based on repeated measurements from this scratch workload, not on the word seek alone.
Practice SQL
Open in SQL FlowIF OBJECT_ID('tempdb..#LabEvent') IS NOT NULL DROP TABLE #LabEvent;
CREATE TABLE #LabEvent (EventId int NOT NULL PRIMARY KEY, CustomerId int NOT NULL, EventDay date NOT NULL, Amount decimal(9, 2) NOT NULL);
;WITH Numbered AS (
SELECT TOP (30000) CONVERT(int, ROW_NUMBER() OVER (ORDER BY a.object_id, b.object_id)) AS n
FROM sys.all_objects AS a
CROSS JOIN sys.all_objects AS b
)
INSERT INTO #LabEvent (EventId, CustomerId, EventDay, Amount)
SELECT n, n % 1000, DATEADD(day, n % 365, CONVERT(date, '2024-01-01')), CONVERT(decimal(9, 2), n % 500)
FROM Numbered;
SET STATISTICS IO, TIME ON;
SELECT EventId, EventDay, Amount
FROM #LabEvent
WHERE CustomerId = 42 AND EventDay >= '2024-10-01' AND EventDay < '2025-01-01'
ORDER BY EventDay DESC, EventId;
CREATE INDEX IX_LabEvent_Customer_Day ON #LabEvent (CustomerId, EventDay DESC) INCLUDE (Amount);
SELECT EventId, EventDay, Amount
FROM #LabEvent
WHERE CustomerId = 42 AND EventDay >= '2024-10-01' AND EventDay < '2025-01-01'
ORDER BY EventDay DESC, EventId;
SET STATISTICS IO, TIME OFF;
DROP TABLE #LabEvent;
Open a captured execution plan
Knowledge check
Commands in this lesson
Next lesson Explain estimates with skewed data