What to do when SQL Server’s query optimizer gets stuck with the wrong plan
Situation Recently I was trying to optimize a SQL query in our legacy code. The code, schema, and indexes had not changed in several years, but suddenly we were seeing severe timeout errors in our logs, and users were getting frustrated. Initial diagnostic steps The problem could not be reproduced in our SQL Server 2012…