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 9265463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:04:26+00:00 2026-06-18T14:04:26+00:00

My understanding of the WITH statement (CTE) is that it executes once per query.

  • 0

My understanding of the WITH statement (CTE) is that it executes once per query. With a query like this:

WITH Query1 AS ( ... )
SELECT *
FROM
    SomeTable t1
    LEFT JOIN Query1 t2 ON ...

If this results in 100 rows, I expect that Query1 was executed only once – not 100 times. If that assumption is correct, the time taken to run the entire query is roughly equal to the time taken to: run Query1 + select from SomeTable + join SomeTable to Query1.

I am in a situation where:

  • Query1 when run alone takes ~5 seconds (400k rows).
  • The remainder of the query, after removing the WITH statement and the LEFT JOIN takes ~15 seconds (400k rows).

So, when running the entire query with the WITH statement and the LEFT JOIN in place, I would have expected the query to complete in a timely manner, instead I’ve let it run for over an hour and once stopped it only got as far as 11k rows.

I am clearly wrong, but why?

  • 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-06-18T14:04:27+00:00Added an answer on June 18, 2026 at 2:04 pm

    Example:

    SET NOCOUNT ON;
    SET IMPLICIT_TRANSACTIONS ON;
    
    CREATE TABLE MyTable (MyID INT  PRIMARY KEY);
    GO
    INSERT  MyTable (MyID)
    VALUES  (11), (22), (33), (44), (55);
    
    PRINT 'Test MyCTE:';
    WITH MyCTE
    AS (
        SELECT  *, ROW_NUMBER()OVER(ORDER BY MyID) AS RowNum
        FROM    MyTable
    )
    SELECT  *
    FROM    MyCTE crt
    LEFT JOIN MyCTE prev ON crt.RowNum=prev.RowNum+1;
    
    ROLLBACK;
    

    If you run previous script in SSMS (press Ctrl+M -> Actual Execution Plan) then you will get this execution plan for the last query:
    enter image description here

    In this case, the CTE is executed one time for crt alias and five (!) times for prev alias, once for every row from crt.

    So, the answer for this question

    Does WITH statement execute once per query or once per row?

    is both: once per query (crt) and once per row (prev: once for every for from crt).

    To optimize this query, for the start,
    1) You can try to store the results from CTE (MyCTE or Query) into a table variable or a temp table

    2) Define the primary key of this table as been the join colum(s),

    3) Rewrite the final query to use this table variable or temp table.

    Off course, you can try to rewrite the final query without this self join between CTE.

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

Sidebar

Related Questions

I might have misunderstood the meaning of base.OnLoad(e); My understanding was that this statement
My understanding is that a prepared statement is compiled on the server once, thus
Equal Objects must have equal hashcodes. As per my understanding this statement is valid
My understanding is that when NOLOCK is used in SELECT statement, it could read
I'm having trouble understanding the behavior of the estimated query plans for my statement
Understanding Magento Models by reference of SQL: select * from user_devices where user_id =
I'm fairly new to C++, but my understanding is that a #include statement will
I have difficulty understanding SELECT * FROM myTable WHERE 0 = ... in the
I'm have trouble understanding the evaluation order of statements that are if statement conditions:
here is my mysql statement: $result = mysql_query(select count(*) from usersecurity where email='.$_SESSION['username'].'); echo

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.