The row estimate is the bug
SQL Server will pick a nested loop against a multi-terabyte database if it believes the outer side is small. I read estimated rows against actual rows before I add an index.
I tune SQL Server for more than 150 customers on Energy Studio Pro, and each of those databases is more than 4 TB. At that size an index is a write, a log, and a long build. I don't add one because a query feels slow. I add one when the plan names the operator that is doing the damage, and when the row estimate that chose the operator is close to the truth.
Most slow queries I open are not missing a clever index. The optimiser made a reasonable choice from a bad guess, and every join after that guess inherited it.
A page, then a bet
The storage engine reads 8 KB pages. A logical read is one of those pages. A nested loop that expects fifty rows and gets five hundred thousand does not "run a bit longer". It does the inner side half a million times. If the inner side is a key lookup, each of those is a jump back to the clustered index, often a different page, often not in memory. On a database this size that is physical IO, and the query owns the disk until it is done.
The bet is the estimate. I turn on actuals and I read one operator before I read the rest of the plan.
SET STATISTICS IO, TIME ON;
SELECT a.AssetId, a.Name, e.RaisedAt, e.Severity
FROM dbo.Asset AS a
JOIN dbo.Event AS e ON e.AssetId = a.AssetId
WHERE e.RaisedAt >= DATEADD(hour, -1, SYSUTCDATETIME())
ORDER BY e.RaisedAt DESC;The statement is the shape of the screens I care about: a recent slice of a table that only grows. In the plan I look at EstimateRows next to ActualRows on the seek, then on the join. If those two numbers disagree by orders of magnitude, the operators downstream were chosen for a query I did not run. Fixing the index first locks that bad choice in.
Two hundred steps, and today is past the last one
SQL Server builds a histogram of at most two hundred steps for a column statistic. Each step has a high key, how many rows match it, and how the values between this step and the next are spread. Anything the histogram does not have a step for is an average. Averages are fine for a column whose distribution sits still. They are a polite fiction for a datetime that only moves forward.
The rows an operator actually asks for are the recent ones. The last hour, the open alarms, the events since midnight. If statistics were last updated yesterday, those rows sit past the highest step. The optimiser estimates them from density, or from whatever ascending-key handling this version of the cardinality estimator is willing to do, and both can be wildly low. A low estimate is exactly the condition that picks a nested loop and a key lookup.
So the first change is often not DDL. It is making the statistic honest: update it, sample it properly, or add a filtered statistic that covers the recent range you actually query. Then compile again and read the estimate. I want the plan to be about the parameter I ran. A plan sniffed from a different parameter, cached, and reused for every customer after that, is the same bug with a longer life. Recompile has a CPU cost. I use it when the parameter really does change the shape, not as a default.
Local variables are the other way people "turn sniffing off". They also throw away the histogram and estimate from density. You have replaced one lie with the average lie. I would rather see the sniffed value and the actual rows in the same plan.
The lookup is often the SELECT list
A key lookup exists because the nonclustered index that satisfied the predicate does not have the columns the query returns. A covering index, with those columns in INCLUDE, removes the lookup. That advice is true and incomplete.
If the data access layer selects eighty columns and the screen paints six, no honest index will cover the query. You would be building a second copy of the table, and every write would maintain it. On this product the data access is a custom ORM, and the failure I look for is the ORM materialising columns nothing in the request needed. Narrow the SELECT, drop the round trips, and only then look at INCLUDE. Pagination belongs in the same pass. A plan that is fast for fifty rows and then streams four terabytes to the client was never a database problem.
Unused indexes are the quiet version of the same mistake. The optimiser never picks them, and every insert still updates them. On a database this size that tax is permanent. I read index usage before I celebrate a new one.
The same lie, different engine
On Farsight the store is PostgreSQL and TimescaleDB, and the statistic is smaller still: a default statistics target of 100 histogram bounds. The predicate I refuse to write is a function wrapped around the time column. Hypertables skip chunks by constraint. Wrap the column and the planner can no longer prove which chunks are irrelevant, so it opens all of them. I filter on the raw time column and let exclusion work. Rollups that every dashboard was recomputing belong in a continuous aggregate, which is a different object from a cache I hope is warm.
The rule
Estimated rows against actual rows, on the operator that touches the big table. One change. Run it again. Write down the logical reads. An index earns its place when that number moves and the estimate was already in the right order of magnitude. Everything else is a guess with a maintenance cost.