3

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 Answer 1

6

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.

4
  • Thanks for the detailed response. That makes sense. In my case I wasn't joining to the CTE twice, just one join from a normal table to the CTE. But I think what you said still applies. It seems strange to me that SQL Server doesn't treat the CTE as a temporary table - that's how I assumed it would work.
    – Josh M.
    Feb 8, 2013 at 3:50
  • A CTE is a local view. So, when is used, the CTE will be expanded like any other view. Feb 8, 2013 at 6:20
  • 1
    Just saw this post when trying to figure out why my query with 12 CTE's was slow. Over 8000 executions on each of them. Thanks for the tip! Oct 30, 2013 at 16:17
  • @BillSambrone: I'm glad this answer helped you. Thank you for your kind words. Oct 31, 2013 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.