Functions in the WHERE Clause Are Bad… Right?


Nah, not necessarily. SQL Server’s query optimizer behavior keeps changing with every freakin’ version. Let’s illustrate it with a simple query against the Stack Overflow Users table:

CREATE INDEX Location ON dbo.Users(Location); GO SELECT COUNT(*) FROM dbo.Users WHERE Location = N’Ahmadabad, India’ OPTION (RECOMPILE);

Because there are a lot of people in Ahmadabad, SQL Server gets that estimate correct, which is really important when you start joining this table to other tables, like finding their number of comments or posts:

SQL Server brought back 1856 of an estimated 1856 rows. So far, so good. But what happens when we