Why does a query slow down drastically if in the WHERE clause a constant is replaced by a parameter (having the same value)?
问题 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 having the same value. Query #1 - with constant ;WITH Hierarchy (Id, ParentId, Data, Depth) AS ( SELECT Id, ParentId, NULL AS Data, 0 AS Depth FROM Test UNION ALL SELECT h.Id, t.ParentId, COALESCE(h.Data, t.Data), Depth + 1 AS Depth FROM Hierarchy h INNER JOIN Test t ON t.Id = h.ParentId ) SELECT * FROM Hierarchy WHERE Id = 69 Query #2 -