Row Count Spool

Introduction

The Row Count Spool operator is one of the four spool operators that SQL Server supports. It counts the number of rows in its input data, and can then later return that same amount of rows, without having to call its child operators to produce the input again.

The Row Count Spool can be viewed as similar to Table Spool, but optimized for cases where the amount of rows is relevant but their content is not. Because the content of the rows is not relevant, the operator does not need to use tempdb to store its input in a worktable; it only has to keep a running count as it reads the input. The other two spool operators have different use cases: Index Spool is used to enable the spool operator to return specific subsets of the input multiple times, and the Window Spool operator is used to support the ROWS and RANGE specifications of windowing functions.

Because Row Count Spool is used in cases where the same number of rows is needed, but not their content, it returns empty rows. These rows are 9 bytes long, for the standard row header; there are no actual data columns in them. And since the operator ignores whatever data columns are in its input, its child operator typically returns empty rows as well.

Unlike the other spool operators, Row Count Spool does not need to store any rows. The rows it reads are discarded; the empty rows it returns are constructed on the spot. The only things a Row Count Spool store are a counter of the number of rows it found in its input, and a counter of the number of rows already returned on the current execution.

You may for example find a Row Count Spool when you perform a cross join for the sole purpose of generating extra copies of each row, or when a query includes an EXISTS predicate with a non-correlated subquery.

Execution modes

A Row Count Spool operator can function in two different execution modes, as determined by the Logical Operation property:

  • Eager Spool:
    On the first execution, an eager spool first reads the entire input when the first row is requested to count the number of rows, then returns a single empty row; it then returns additional empty rows up to the number of rows in the input as more rows are requested. Subsequent executions return empty rows on each request up to the number of rows previously counted on the input, unless a rebind is issued.
  • Lazy Spool:
    On the first execution, a lazy spool reads a row, counts it, and immediately returns an empty row for each read request. Subsequent executions return empty rows on each request up to the number of rows previously counted on the input, unless a rebind is issued.

Unlike the Table Spool operator, a Row Count Spool does not have the ability to operate as a “consumer”, nor as a “producer” for other spool operators.

Visual appearance in execution plans

Depending on the tool being used, a Row Count Spool operator is displayed in a graphical execution plan as shown below:

SQL Server Management Studio

Azure Data Studio

Plan Explorer

(version 17.4 and up)

(until version 17.3)

Algorithm

The basic algorithm for the Row Count Spool operator is as shown below:

Note that this flowchart is a simplification. It doesn’t show that execution stops whenever a row is returned, and resumes where it was upon next being called. It also does not show that the operator’s input is closed when the operator itself is closed.

Rebind or rewind

A Row Count Spool distinguishes between being initialized for a rebind or a rewind (see Nested Loops). Whether rebind or rewind is needed is known when Initialize() is called and then stored in operator memory to be used on the first GetNext() call.

The first execution of the operator cannot be a rewind, so it should always be considered a rebind (even though SQL Server itself does not consistently consider it as such; it is sometimes considered as a rebind, but sometimes also considered as neither of the two).

Input counter

The input counter is simply a memory location in the operator’s working memory that stores a tally of the number of rows read in the input. When the operator runs as an eager spool, the entire input is consumed and counted at once, after which this counter is the number of rows in the input. When the operator runs as a lazy spool, then each rebind counts rows as they are read and corresponding empty rows are immediately returned; in this case the counter is a running count. On a rewind, the input counter never changes and indicates the total number of empty rows that need to be returned.

Iteration counter

The iteration counter is another memory location in the operator’s working memory, storing another tally. In this case of the number of empty rows returned. For a rebind of a lazy spool, the iteration counter is not needed. For rewinds, as well as for all executions of eager spools, the iteration counter keeps track of the number of empty rows returned since the execution started; once this is equal to the input counter, the operator will return an end of data signal.

Operator properties

The properties below are specific to the Row Count Spool operator, or have a specific meaning when appearing on it. For all other properties, see Common properties. Properties that are included on the Common properties page but are also included below for their specific meaning for the Row Count Spool operator are marked with a *.

Property nameDescription
Estimated Row Size *Always 9 bytes (the size of the row header), because a Row Count Spool always returns empty rows.
Logical Operation *Can be Eager Spool or Lazy Spool, as described above.
Output List *Since a Row Count Spool always returns empty columns, it does not have an actual Output List. The property is still present, though, but empty.

Implicit properties

This table below lists the behaviour of the implicit properties for the Table Spool operator.

Property nameDescription
Batch Mode enabledThe Row Count Spool operator supports row mode execution only.
BlockingThe Row Count Spool operator is fully blocking when it executes as an eager spool.
The Row Count Spool operator is non-blocking when it executes as a lazy spool.
Memory requirementThe Row Count Spool operator does not have any special memory requirement.
Order-preservingAll rows returned by a Row Count Spool operator are empty, so they are all equal. The result can be considered both ordered and unordered, and neither has any relevance for other operators.
Parallelism awareThe Row Count Spool operator is not parallelism aware.
Segment awareThe Row Count Spool operator is not segment aware.
Menu

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close