1

I have a large table and would like to know if it's possible to improve performance for queries.

Running a simple query takes several minutes. See the execution plan below: https://files.fm/down.php?cf&i=pxgp25tr&n=ep.sqlplan

I'm using Microsoft SQL Server 2016, and I have this stored procedure:

How can I improve the performance of the procedure above?

This is my execution plan:

https://www.brentozar.com/pastetheplan/?id=r1m43JclQ

Is there a way to improve the select performance?

Elham Azadfar
  • 709
  • 2
  • 17
  • 34

1 Answers1

3

I would add OPTION(RECOMPILE):

SET @SQL = @Pre + @Select + @SQL + @GroupBy
=>
SET @SQL = @Pre + @Select + @SQL + @GroupBy + ' OPTION(RECOMPILE)';

I would use this clause to generate execution plan and/or cardinality estimations for dynamic SQL every time(it is some kind of report so I assume that the overhead shouldn't be too high). More info: OPTION (RECOMPILE) is Always Faster; Why?

It's a good practice to end every statement with semicolon.

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275