How to optimize sql query in Microsoft SQL Server