Why does a query slow down drastically if in the WHERE clause a constant is replaced by a parameter (having the same value)?

前端 未结 4 1949
清歌不尽
清歌不尽 2021-01-02 03:19

I have a recursive query which executes very fast if the WHERE clause contains a constant but becomes very slow if I replace the constant with a parameter havin

4条回答
  •  难免孤独
    2021-01-02 04:03

    This could be the worse suggestion ever, but have you considered creating a sproc to create your query as a string and execute it using sp_executesql?

    I know nothing about the caching behaviour of SQL executed by sp_executesql, it was just the first thing to pop into my head.

提交回复
热议问题