الانتقال إلى المحتوى
قواعد البياناتIndexing· 7 دقائق للقراءة

SELECT * Costs More Than Bandwidth: Covering Indexes and Key Lookups

Every column outside the index costs a lookup per row, and past a few thousand rows a full table scan. Name the columns, then cover the queries that matter.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

An admin screen lists the last 30 days of orders: date, status and total. There's an index on 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

The belief: SELECT * only sends some extra bytes over the network, so it's a matter of style.

It's half true, which is why it survives. Fetching one order by its primary key is a single clustered index seek, and the whole row is right there, so the extra columns really do cost only bytes. ORMs load whole entities by default, and on a development database with a few hundred rows nothing is ever slow. The bytes are real, too: in the test, the 30-day result carried about 9 MB of column data against about 1 MB for the four columns. But the bytes were the smallest part of the bill.

What actually happens

A nonclustered index holds a few columns and a pointer

In SQL Server, a nonclustered index row holds its key columns, any included columns, and a row locator: the clustered index key, or a row ID when the table is a heap. Any other column has to come from the table itself, through a : one more seek down the clustered index for every row the index found. In the test, a customer's 52 orders read 171 pages with 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.

PostgreSQL works the same way with different names. Every index is stored apart from the table's heap, so an ordinary index scan visits the heap for each row. When the index holds every column the query references, the planner can use an index-only scan instead.

Past a point, the optimizer drops your index

Lookups are random reads, one per row, so the optimizer weighs them against reading the whole table in order. In the test, SELECT * with the CreatedAt index went from fine to useless as the range grew:

Range (1,000,000 orders, SQL Server 2022)SELECT * planReadsFour columns, coveredReads
A customer's 52 ordersIndex Seek + Key Lookup171Index Seek3
Last day, 1,370 rowsIndex Seek + Key Lookup4,210Index Seek10
Last 7 days, 9,590 rowsIndex Seek + Key Lookup29,401Index Seek49
Last 30 days, 41,096 rowsClustered Index Scan + Sort50,391Index Seek196

The index never changed. What changed is how many rows needed the other columns, and at 30 days the whole table was cheaper. A has no such limit, because there is nothing to look up.

Wide rows make sorts reserve more memory

Before a sort or hash runs, SQL Server reserves memory for it, a , sized from the estimated row count and row width. The 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

An index covers SELECT * 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

SQL
-- 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

Id 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:

SQL postgresql.sql
CREATE INDEX orders_created_at_idx    ON orders (created_at) INCLUDE (id, status, total);

That example follows the PostgreSQL docs' 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

  1. Find the lookupsLook 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.
  2. Name the columnsReplace 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.
  3. Cover the hot query shapesPut 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.
  4. Check what it costsCompare 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.

📚 المصادر

  1. Create Indexes with Included Columns — SQL Server, Microsoft Learn
  2. Index-Only Scans and Covering Indexes — PostgreSQL Documentation
  3. Index Architecture and Design Guide — SQL Server, Microsoft Learn
  4. Memory Grant Feedback — SQL Server, Microsoft Learn

التعليقات (0)