Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3407502
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:45:32+00:00 2026-05-18T05:45:32+00:00

I have a recursive query which executes very fast if the WHERE clause contains

  • 0

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 – with parameter

DECLARE @Id INT
SELECT @Id = 69

;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 = @Id

In case of a table with 50,000 rows the query with the constant runs for 10 milliseconds and the one with the parameter runs for 30 seconds (3,000 times slower).

It is not an option to move the last WHERE clause to the anchor definition of the recursion, as I would like to use the query to create a view (without the last WHERE). The select from the view would have the WHERE clause (WHERE Id = @Id) – I need this because of Entity Framework, but that is another story.

Can anybody suggest a way to force query #2 (with the parameter) to use the same query plan as query #1 (with the constant)?

I already tried playing with indexes but that did not help.

If somebody would like I can post the table definition and some sample data as well.
I am using SQL 2008 R2.

Thank you for your help in advance!

Execution plan – Query #1 – with constant

alt text

Execution plan – Query #2 – with parameter

alt text

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:45:32+00:00Added an answer on May 18, 2026 at 5:45 am

    As Martin suggested in a comment under the question, the problem is that SQL server does not push down properly the predicate from the WHERE clause – see the link in his comment.

    I ended up with creating a user defined table-valued function and use it with the CROSS APPLY operator for creating the view.

    Let’s see the solution itself.

    User Defined Table-valued Function

    CREATE FUNCTION [dbo].[TestFunction] (@Id INT)
    RETURNS TABLE 
    AS
    RETURN 
    (
        WITH
        Hierarchy (Id,  ParentId, Data, Depth)
        AS(
        SELECT Id, ParentId, NULL AS Data, 0 AS Depth FROM Test Where Id = @Id
        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
    )
    

    View

    CREATE VIEW [dbo].[TestView]
    AS
    SELECT t.Id, t.ParentId, f.Data, f.Depth
    FROM
        Test AS t
        CROSS APPLY TestFunction(Id) as f
    

    Query with constant

    SELECT * FROM TestView WHERE Id = 69
    

    Query with parameter

    DECLARE @Id INT
    SELECT @Id = 69
    SELECT * FROM TestView WHERE Id = @Id
    

    The query with the parmater executes basically as fast as the query with the constant.

    Thank You Martin and for the others as well!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question: I have a view which I want to derive from a recursive query.
I have a recursive algorithm which steps through a string, character by character, and
I have been experimenting with Lambda expressions in Oxygene. Very simple recursive lambda expression
I have a recursive table in which each record has an ID and a
I have the following recursive function which works... up until a point. Then the
i hoping to create a recursive function which i don't have an idea yet
If you have a recursive structure, say, child tables located inside td cells of
In the Fibonacci sequence, I have seen conventional implementations which recursively call the same
Below is the code snippet with comments which describes the problem statement. We have
I am having extreme difficulty constructing a query which returns an XML style hierarchy.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.