Explain the execution of a cte

  • Hi,
    I had written a cte which has come complex joins and selected 7 columns for output. It contained 4 million rows.
    Now I write  my Cte like this:

    with cte as
    (
    select col1,col2,col3,col4,col5,col6,col7
    from
    tbl1 Join tbl2 ....
    )
    (above code is just for reference)

    When I execute :
    select * from cte : takes 14 secs
    select count(1) from cte : takes 1 sec.

    Now my question is, when I ask for the count does SQL ignore all the columns defined in the cte ?
    Which would essentially mean that based on how you query CTE, the definition would be compiled ?

  • What is the result of the second query?  My guess is that the first one takes so long because it has to return all the data to the client.  The second one is only returning a single number.  Another possibility is that a COUNT can make use of narrower indexes than a query that needs to return multiple columns and therefore fewer page reads were not necessary.

    Let's not waste our time guessing, though.  Have you compared the execution plans?

    John

  • Yes the plans are different and with select count(1) I do not see other columns being pulled up.
    That is my question, whether the execution of the query underlying cte depends on the way cte is queried.

  • er.mayankshukla - Wednesday, December 27, 2017 5:35 AM

    That is my question, whether the execution of the query underlying cte depends on the way cte is queried.

    Yes. An CTE is an expression. It's not (like people seem to think) a temporary table or variable. They don't behave the same same.

    Say you have a table which is 500 columns wide, and you have 100,000 rows stored in it. You have a CLUSTERED INDEX on the Column PrimaryKey. Doing something simple like;
    WITH CTE AS(
        SELECT *
        FROM MyTable)
    SELECT PrimaryKey
    FROM CTE
    WHERE PrimaryKey <= 1000;

    Wouldn't mean that the data engine needs to scan all the others columns. Why? Because they aren't referenced in the end statement (note that this isn't always true, it depends on what the CTE is doing, but for a simple query like this, it is), you're only worried about PrimaryKey. On the other hand, if you were to use a Temporary table:
    SELECT *
    INTO #Temp
    FROM MyTable;

    SELECT PrimaryKey
    FROM #Temp
    WHERE PrimaryKey <= 1000;

    Now, EVERY column and row's data needs to be read and stored in the Temporary Table. Then, you' very likely have a table scan, rather than a Index Seek, on the Temporary table, as it's not indexed. The two would perform entirely differently.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply