Query Exercise: Solving The 201 Buckets Problem


When you run a query, SQL Server needs to estimate the number of matching rows it’ll find – so that it can decide which indexes to use, whether to go parallel, how much memory to grant, and more.

For example, take any Stack Overflow database, and let’s say I have an index on Location, and I want to find the top-ranking users in Lithuania:

CREATE INDEX Location ON dbo.Users(Location); GO SELECT * FROM dbo.Users WHERE Location = N’lithuania’ ORDER BY Reputation DESC;

Then SQL Server has to guess how many people are in Lithuania so it can decide whether to use