SQL Server: Two Equality Searches
Two indexes, opposite key orders, the same seek
SELECT
*
FROM
dbo.Users
WHERE
DisplayName = 'alex'
AND
Location = 'Seattle, WA';
IX_DisplayName_Location
sorted by DisplayName, then Location
aaron
· Houston, TX
· Id 8,114
aaron
· Seattle, WA
· Id 19,406
alex
· Miami, FL
· Id 12,006
alex
· Seattle, WA
· Id 41,209
alex
· Seattle, WA
· Id 88,554
alex
· Tampa, FL
· Id 2,290
brent
· Chicago, IL
· Id 26,837
brent
· Seattle, WA
· Id 60,113
chris
· Austin, TX
· Id 3,741
one seek · 2 rows
only 4 alexes in the block — small target, same seek
IX_Location_DisplayName
sorted by Location, then DisplayName
San Diego, CA
· maria
· Id 33,072
Seattle, WA
· aaron
· Id 19,406
Seattle, WA
· alex
· Id 41,209
Seattle, WA
· alex
· Id 88,554
Seattle, WA
· brent
· Id 60,113
Seattle, WA
· chris
· Id 90,125
Seattle, WA
· dana
· Id 7,036
Seattle, WA
· zoe
· Id 55,909
Tacoma, WA
· alex
· Id 15,772
one seek · 2 rows
even if Seattle had 50,000 rows, the seek lands exactly here
either key order: one seek → the same 2 rows