-------------------- this takes 4 secs to execute (with 2000 000 rows) WHY?---------------------
DECLARE @AccountId INT DECLARE @Max INT DECLARE @MailingLi
Add OPTION (RECOMPILE) to the end of the query.
OPTION (RECOMPILE)
SQL Server doesn't "sniff" the values of the variables so you will be getting a plan based on guessed statistics rather than one tailored for the actual variable values.