Why SQL Server go slow when using variables?

后端 未结 7 2063
后悔当初
后悔当初 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:27

    Actually, it was answered very well, I just write here a workaround as it worked for me:

    Create a stored procedure with the SQL

    WHERE id BETWEEN @firstId and @lastId
    

    After then call the stored proc with parameters @firstId and @lastId and it will speed up. I still not 100% why it is working, but it works.

提交回复
热议问题