Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

sql - How to optimize long running cte

I have one query that is taking 10 minute and giving me 6852104 records. How can I optimize it? I have added execution plan as well.

2

My query is like this:

    WITH CTE (columns name 
    )
    AS (
    SELECT  columns
    FROM #tempTable b  WITH (NOLOCK)
    INNER JOIN physicaltable1 A ON A.ParentInvoiceItemId = b.InvoiceItemId
    INNER JOIN physicaltable2 c ON c.LinkedInvoiceItemId = a.LinkedInvoiceItemId
    AND c.HospitalId = B.Hospital_Id
    
    UNION ALL
    
    SELECT columns
    FROM CTE LIP
    JOIN physicaltable1         LIC ON LIP.ChildInvoiceItemId = LIC.ParentInvoiceItemId
    INNER JOIN physicaltable12  LIH ON LIH.LinkedInvoiceItemId = LIC.LinkedInvoiceItemId
    AND LIH.HospitalId = LIP.HospitalId
    WHERE LIP.[Level] < 3
    )
    SELECT *
    INTO #newtemptable
    FROM CTE;
question from:https://stackoverflow.com/questions/65933462/how-to-optimize-long-running-cte

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...