Why SQL Server go slow when using variables?

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

    Is ID in an Index (eg Primary Key)? If not, try adding one.

    Another thing could be that in the first (fast) instance the query is getting executed slightly differently. The most common thing I've seen happen is that the joins get done in an inefficient order. Try re-ordering the joins, or converting some to subqueries. If you post more of your query we can assist further.

提交回复
热议问题