Make sure you have an index on RefDate. Probably another one on FormatCode and AcctName. Beyond that, you need to use the database tools to track down which part is taking the most time. If it is Microsoft Sql Server then you can use the Estimated Execution Plan and other tools.