
SELECT * Costs More Than Bandwidth: Covering Indexes and Key Lookups
- كاتب موثّق
محتويات المقال
CreatedAt, and the query is SELECT * ... ORDER BY CreatedAt DESC. In a test on SQL Server 2022 with 1,000,000 orders, it ignored that index, scanned all 50,391 pages of the table, and reserved 267 MB of memory to sort 41,096 rows. Asking for the four columns the screen shows, with those columns in the index, read 196 pages and needed no sort at all. Over three runs each, that was about 309 ms against 23 ms on average.
The common misconception
SELECT * only sends some extra bytes over the network, so it's a matter of style.
What actually happens
A nonclustered index holds a few columns and a pointer
SELECT *. The plan's Key Lookup fetched Email, ShippingAddress and Notes, columns the page never showed. With the needed columns in the index, the same query read 3 pages.
Past a point, the optimizer drops your index
SELECT * with the CreatedAt index went from fine to useless as the range grew:
SELECT * plan | ||||
|---|---|---|---|---|
Wide rows make sorts reserve more memory
Notes column is nvarchar(max), and the plan estimated the average row at 4,574 bytes, while the real rows held about 230 bytes of data. The first run was granted 267 MB and used 21 MB. Row mode memory grant feedback, on at compatibility level 150 and above, cut later runs to 164 MB, still nearly eight times what was used. Microsoft's page on the feature puts it plainly: excessive grants result in wasted memory and reduced concurrency.
No index covers SELECT *, and it grows on its own
SELECT *, and it grows on its ownSELECT * only if it includes every column, which makes it a second copy of the table. And the list isn't fixed: when someone adds a JSON payload or an attachment column next year, every SELECT * starts reading and sending it, with no change to the code that runs it.
Name the columns, then cover them
-- Admin list: the screen shows CreatedAt, Status and TotalSELECT *FROM dbo.OrdersWHERE CreatedAt >= '2026-10-02'ORDER BY CreatedAt DESC;-- Tested: Clustered Index Scan + Sort, 50,391 reads,-- 267 MB memory grant on the first run-- Key: what the query filters and sorts on; INCLUDE: what it returnsCREATE INDEX IX_Orders_CreatedAt ON dbo.Orders (CreatedAt) INCLUDE (Status, Total) WITH (DROP_EXISTING = ON);SELECT Id, CreatedAt, Status, TotalFROM dbo.OrdersWHERE CreatedAt >= '2026-10-02'ORDER BY CreatedAt DESC;-- Tested: Index Seek, 196 reads, no Sort, no memory grantId costs nothing extra: as the clustered key, it's already in every nonclustered index row. The index's order also serves the ORDER BY, which is why the sort disappeared. In PostgreSQL the index points to heap rows, not to the primary key, so id has to be included too:
CREATE INDEX orders_created_at_idx ON orders (created_at) INCLUDE (id, status, total);INCLUDE syntax and wasn't run for this article. If an ORM writes your SQL, the same rule applies: a query that loads whole entities selects every mapped column, while a projection into a DTO selects only the ones it names. The .NET path's article "Beyond Include: Fixing N+1 in EF Core With Projections and Split Queries" covers that side.
How to apply it in your project
Find the lookups Look for Key Lookup operators in the plans of your most frequent and most expensive queries, or for index scans that aren't index-only in PostgreSQL. Check the row counts: a lookup per row adds up fast. Name the columns Replace SELECT *and whole-entity loads with the columns the caller actually uses. On its own this shrinks rows and memory grants, and it makes covering possible.Cover the hot query shapes Put filter and sort columns in the key and the returned columns in INCLUDE. Only do it for queries that run often or read a lot, not for every list in the app.Check what it costs Compare index size and write timings before and after, and confirm with the actual plan that the lookup is gone.
Key takeaways
SELECT *costs reads, not just bytes: every column outside the index means a key lookup per row.Past a few thousand rows, the optimizer may skip your index for a full scan: 50,391 reads instead of 196 in the test. Wide columns inflate memory grants: 267 MB reserved, 21 MB used for one sort in the test. A covering index answers from the index alone; in PostgreSQL that's an index-only scan, which also depends on the visibility map. Included columns are copies, so cover the hot queries only and measure the write cost.
التعليقات (0)