Why SQL Server go slow when using variables?

后端 未结 7 2034
后悔当初
后悔当初 2020-12-05 19:02

I have a sql query that runs super fast, around one second, when not using variables, like:

WHERE id BETWEEN 5461094 and 5461097

But when I

相关标签:
7条回答
  • 2020-12-05 19:35

    It's because when the values are hard coded, it can look up the statistics it has on the data in the table, and figure out the best query to run. Have a look at the execution plans of each of these queries. It must be scanning when your using variables.

    if the range is always small you might be able to use an index hint to help this.

    0 讨论(0)
提交回复
热议问题