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?

6
  • 1
    I don't have time to make this a complete answer right now, but make your table variables temp tables instead. Commented Jun 9, 2018 at 12:05
  • 1
    Is the large table you mentioned DbsTrade? How many rows? You might experiment with columnstore and perhaps a tabular model to maximize performance of large aggregations.. Commented Jun 9, 2018 at 12:20
  • About 500000 rows Commented Jun 9, 2018 at 13:17
  • Thanks for your help, I am weak in sql can you give me some hints... please. Commented Jun 9, 2018 at 13:18
  • 2
    Please see paste the plan for a better way to include an execution plan in your question. Commented Jun 9, 2018 at 13:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You should explain why you think this will help. (e.g. improve cardinality estimates of the table variables) rather than leaving it as a mysterious incantation.
I added Option(Recompile) but it steel slow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.