← Back to Blog
📅 02.12.2026 🏷️ SQL Optimization, SQL Server

Aksel Rohtsalu: TOP 100 vs. TOP 101: How a Single Extra Row Eats SQL Server Memory

How much difference is there between a query that asks for 100 rows and one that asks for 101 rows? One row. Does the user notice a difference? No. Does the server notice? It shouldn't either.

Most of the time, it doesn't. In MS SQL Server, however, that single extra row can make a query's memory requirement hundreds of times larger. This happens when a query has an ORDER BY and the sorted column has no index. Then memory no longer depends on how many rows we want, but on how large the table is.

Measure first

The test is simple. The database is StackOverflow2013, with tables Users (2.5 million rows) and Posts (17 million rows, 37 GB). There is no index on Age or Score, so SQL Server has to sort the result itself.

SELECT TOP (100) Id FROM dbo.Users ORDER BY Age;
SELECT TOP (101) Id FROM dbo.Users ORDER BY Age;

SELECT TOP (100) Id FROM dbo.Posts ORDER BY Score;
SELECT TOP (101) Id FROM dbo.Posts ORDER BY Score;

The server is SQL Server 2025 with max server memory set to 8 GB. I checked the memory grant in sys.dm_exec_query_stats (last_grant_kb, last_used_grant_kb, last_spills) and in the actual plan.

TOP 100: almost free

For TOP 100, SQL Server uses the TopN Sort operator, which keeps only those top 100 rows in memory. Each incoming row either fits in and pushes out the worst one, or is discarded right away. The table can be any size, and there are still 100 rows in memory.

The result is the same for both tables: a 1 MB grant, of which 16 KB was used. No more, no less.

TOP 101: sorting the whole table

Add one row and the picture changes completely. The plan still has TopN Sort, but memory is now calculated as if sorting the entire input: estimated row count times row width.

For the Users table, the query received 166 MB of memory to return 101 rows. For the Posts table, the optimizer wanted 1.1 GB. The server gave 786 MB, because that is the maximum for a single query and no single query gets more.

What's interesting is that TOP 1000 and TOP 100000 also got the same 166 MB. The size of N no longer matters; only the size of the table matters. If the table doubles in size, the memory request for each such query doubles too.

And another surprise. All of these queries used up all the memory they received and still spilled to tempdb. The actual plan shows the SpillToTempDb warning. A query one row larger requested hundreds of megabytes of memory, used it all, and still wrote to disk. Execution time changed too: on the Posts table with a single thread (MAXDOP 1), TOP 100 took 26 seconds and TOP 101 took 44 seconds.

Why this is a problem

One such query on its own is not a problem. Memory is granted, the query finishes, and the memory is released.

The problem starts when this query runs multiple times concurrently. The memory grant is reserved before the query starts executing. If the server has, say, 6 GB of available query memory and each query wants 786 MB, the eighth query won't start right away. It waits on RESOURCE_SEMAPHORE. Along with it, every other query that needs memory waits too—even those that only want a few megabytes. At the same time, that memory is taken from the buffer pool, which means more reads from disk.

All the user sees is that "the system is slow." And when someone goes looking for the query, that query itself only pulls 101 rows.

Do other database engines behave the same way? I tested the same query on other SQL engines: MySQL, PostgreSQL, and TiDB.

How others do the same thing

I ran the same test on MySQL 8.4, PostgreSQL 18, and TiDB 8.5, each on tables with a few million to 14 million rows and sorting by an unindexed column.

MySQL uses a priority queue as long as N rows fit in the sort buffer (sort_buffer_size, 256 KB by default). 100 rows took 2.4 KB, and 101 rows took 2.4 KB as well. Even 10,000 rows fit inside 240 KB. Only at 100,000 rows did MySQL switch to regular sorting and spill the rest to disk. Memory still stayed within 256 KB.

PostgreSQL does the same thing under a different name: top-N heapsort. 100 and 101 rows both took 31 KB, and 1,000 rows took 114 KB. At 100,000 rows, work_mem (4 MB) filled up and PostgreSQL switched to external merge sort on disk.

TiDB pushes the TopN operation straight down to the storage layer (TiKV). Each data region returns only its own top N rows, and TiDB picks the final result from those. 101 rows took 2.6 KB, and one million rows took 23 MB.

In all three, the logic is the same: memory depends on how many rows we want and how wide they are, not on the size of the table. Nothing is reserved upfront; memory is taken incrementally and capped by a single setting. Table size only affects how long the scan takes.

SQL Server is the only one of the four with a hard cutoff right at 100, where exceeding it makes memory depend on the table.

What to do about it

The best solution is an index on the column being sorted. Then SQL Server doesn't have to sort at all—it reads the first N rows from the index and stops. No memory is needed and the table isn't scanned.

If you can't add an index, it's worth asking whether the business actually needs 101 rows or more. A screen often shows 50 rows, while the query has TOP 500 or TOP 1000 "just in case." If TOP 100 gives the same result, the memory need drops right to 1 MB.

On newer compatibility levels (150+), there is memory grant feedback, which reduces an overly large grant on repeated executions. Still, the first execution gets the big grant, and if the plan is evicted from cache, the cycle starts over. It softens the issue, but doesn't solve it. At the query level, you can cap the grant with the MAX_GRANT_PERCENT hint, and across a workload with Resource Governor. Both are more of a band-aid than a cure.

And most importantly: you have to know how to find these queries. Watch for queries where last_grant_kb is large and last_used_grant_kb or the returned row count is small. Also watch for RESOURCE_SEMAPHORE waits. They don't stand out on lists of the longest-running queries because each individual run is fast.

Summary

TOP 100 and TOP 101 differ by one row. For SQL Server, however, that is the difference between a sort that costs 16 KB and a sort that costs hundreds of megabytes proportional to table size—and spills to tempdb on top of it.

Other engines don't have this cutoff. There, the 101st row is just one more row.

So next time the server is "slow" and seems short on memory, before ordering more RAM, ask: how many of our queries ask for TOP 101 or more and sort on a column without an index?

Author: Aksel Rohtsalu · MS SQL Server consultant

Author: Aksel Rohtsalu · MS SQL Server consultant · LinkedIn ↗