add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table #MyTable (
Col_A varchar(5),
Col_B int
)

insert into #MyTable values ('A',1)
insert into #MyTable values ('A',1)
insert into #MyTable values ('A',2)
insert into #MyTable values ('A',2)
insert into #MyTable values ('A',2)
insert into #MyTable values ('A',3)

insert into #MyTable values ('B',4)
insert into #MyTable values ('B',4)
insert into #MyTable values ('B',5)
9 rows affected
CREATE INDEX i ON #MyTable (Col_A, Col_B);
set statistics xml on;

SELECT *
FROM #MyTable AS mt
CROSS APPLY ( SELECT COUNT(DISTINCT mt2.Col_B) AS dc
FROM #MyTable AS mt2
WHERE mt2.Col_A = mt.Col_A
-- GROUP BY mt2.Col_A
) AS ca;
Col_A Col_B dc
A 1 3
A 1 3
A 2 3
A 2 3
A 2 3
A 3 3
B 4 2
B 4 2
B 5 2
Microsoft SQL Server 2005 XML Showplan
<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.481" Build="14.0.3335.7"><BatchSequence><Batch><Statements><StmtSimple StatementText="SELECT *&#xa;FROM #MyTable AS mt&#xa;CROSS APPLY ( SELECT COUNT(DISTINCT mt2.Col_B) AS dc&#xa; FROM #MyTable AS mt2&#xa; WHERE mt2.Col_A = mt.Col_A&#xa; -- GROUP BY mt2.Col_A &#xa; ) AS ca" StatementId="1" StatementCompId="2" StatementType="SELECT" RetrievedFromCache="true" StatementSubTreeCost="0.00797214" StatementEstRows="9" SecurityPolicyApplied="false" StatementOptmLevel="FULL" QueryHash="0x76A40E7FE33C426B" QueryPlanHash="0x95D5E6BB777A4B52" StatementOptmEarlyAbortReason="GoodEnoughPlanFound" CardinalityEstimationModelVersion="140"><StatementSetOptions QUOTED_IDENTIFIER="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" NUMERIC_ROUNDABORT="false"></StatementSetOptions><QueryPlan DegreeOfParallelism="0" NonParallelPlanReason="NoParallelPlansInDesktopOrExpressEdition" CachedPlanSize="32" CompileTime="15" CompileCPU="15" CompileMemory="344"><MemoryGrantInfo SerialRequiredMemory="0" SerialDesiredMemory="0"></MemoryGrantInfo><OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="838808" EstimatedPagesCached="52425" EstimatedAvailableDegreeOfParallelism="1" MaxCompileMemory="904904"></OptimizerHardwareDependentProperties><OptimizerStatsUsage><StatisticsInfo LastUpdate="2020-11-03T17:12:34.90" ModificationCount="0" SamplingPercent="100" Statistics="[_WA_Sys_00000002_B5D7E539]" Table="[#MyTable____________________________________________________________________________________________________________0000000000BC]" Schema="[dbo]" Database="[tempdb]"></StatisticsInfo><StatisticsInfo LastUpdate="2020-11-03T17:12:34.89" ModificationCount="0" SamplingPercent="100" Statistics="[i]" Table="[#MyTable____________________________________________________________________________________________________________0000000000BC]" Schema="[dbo]" Database="[tempdb]"></StatisticsInfo></OptimizerStatsUsage><TraceFlags IsCompileTime="1"><TraceFlag Value="460" Scope="Global"></TraceFlag><TraceFlag Value="8017" Scope="Global"></TraceFlag></TraceFlags><TraceFlags IsCompileTime="0"><TraceFlag Value="460" Scope="Global"></TraceFlag><TraceFlag Value="8017" Scope="Global"></TraceFlag></TraceFlags><QueryTimeStats ElapsedTime="0" CpuTime="0"></QueryTimeStats><RelOp NodeId="0" PhysicalOp="Compute Scalar" LogicalOp="Compute Scalar" EstimateRows="9" EstimateIO="0" EstimateCPU="9e-007" AvgRowSize="21" EstimatedTotalSubtreeCost="0.00797214" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_A"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_B"></ColumnReference><ColumnReference Column="Expr1004"></ColumnReference></OutputList><ComputeScalar><DefinedValues><DefinedValue><ColumnReference Column="Expr1004"></ColumnReference><ScalarOperator ScalarString="CASE WHEN [Expr1004] IS NULL THEN (0) ELSE [Expr1004] END"><IF><Condition><ScalarOperator><Logical Operation="IS NULL"><ScalarOperator><Identifier><ColumnReference Column="Expr1004"></ColumnReference></Identifier></ScalarOperator></Logical></ScalarOperator></Condition><Then><ScalarOperator><Const ConstValue="(0)"></Const></ScalarOperator></Then><Else><ScalarOperator><Identifier><ColumnReference Column="Expr1004"></ColumnReference></Identifier></ScalarOperator></Else></IF></ScalarOperator></DefinedValue></DefinedValues><RelOp NodeId="1" PhysicalOp="Nested Loops" LogicalOp="Left Outer Join" EstimateRows="9" EstimateIO="0" EstimateCPU="3.762e-005" AvgRowSize="21" EstimatedTotalSubtreeCost="0.00797124" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_A"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_B"></ColumnReference><ColumnReference Column="Expr1004"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="9" Batches="0" ActualExecutionMode="Row" ActualElapsedms="0" ActualCPUms="0" ActualEndOfScans="1" ActualExecutions="1"></RunTimeCountersPerThread></RunTimeInformation><NestedLoops Optimized="0"><OuterReferences><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_A"></ColumnReference></OuterReferences><RelOp NodeId="2" PhysicalOp="Index Scan" LogicalOp="Index Scan" EstimateRows="9" EstimatedRowsRead="9" EstimateIO="0.003125" EstimateCPU="0.0001669" AvgRowSize="17" EstimatedTotalSubtreeCost="0.0032919" TableCardinality="9" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_A"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_B"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="9" Batches="0" ActualExecutionMode="Row" ActualElapsedms="0" ActualCPUms="0" ActualScans="1" ActualLogicalReads="2" ActualPhysicalReads="0" ActualReadAheads="0" ActualLobLogicalReads="0" ActualLobPhysicalReads="0" ActualLobReadAheads="0" ActualRowsRead="9" ActualEndOfScans="1" ActualExecutions="1"></RunTimeCountersPerThread></RunTimeInformation><IndexScan Ordered="0" ForcedIndex="0" ForceSeek="0" ForceScan="0" NoExpandHint="0" Storage="RowStore"><DefinedValues><DefinedValue><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_A"></ColumnReference></DefinedValue><DefinedValue><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_B"></ColumnReference></DefinedValue></DefinedValues><Object Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Index="[i]" Alias="[mt]" IndexKind="NonClustered" Storage="RowStore"></Object></IndexScan></RelOp><RelOp NodeId="3" PhysicalOp="Compute Scalar" LogicalOp="Compute Scalar" EstimateRows="1" EstimateIO="0" EstimateCPU="0" AvgRowSize="11" EstimatedTotalSubtreeCost="0.00464172" Parallel="0" EstimateRebinds="4" EstimateRewinds="4" EstimatedExecutionMode="Row"><OutputList><ColumnReference Column="Expr1004"></ColumnReference></OutputList><ComputeScalar><DefinedValues><DefinedValue><ColumnReference Column="Expr1004"></ColumnReference><ScalarOperator ScalarString="CONVERT_IMPLICIT(int,[Expr1007],0)"><Convert DataType="int" Style="0" Implicit="1"><ScalarOperator><Identifier><ColumnReference Column="Expr1007"></ColumnReference></Identifier></ScalarOperator></Convert></ScalarOperator></DefinedValue></DefinedValues><RelOp NodeId="4" PhysicalOp="Stream Aggregate" LogicalOp="Aggregate" EstimateRows="1" EstimateIO="0" EstimateCPU="2.58594e-006" AvgRowSize="11" EstimatedTotalSubtreeCost="0.00464172" Parallel="0" EstimateRebinds="4" EstimateRewinds="4" EstimatedExecutionMode="Row"><OutputList><ColumnReference Column="Expr1007"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="9" Batches="0" ActualExecutionMode="Row" ActualElapsedms="0" ActualCPUms="0" ActualEndOfScans="0" ActualExecutions="9"></RunTimeCountersPerThread></RunTimeInformation><StreamAggregate><DefinedValues><DefinedValue><ColumnReference Column="Expr1007"></ColumnReference><ScalarOperator ScalarString="COUNT([tempdb].[dbo].[#MyTable].[Col_B] as [mt2].[Col_B])"><Aggregate Distinct="0" AggType="COUNT_BIG"><ScalarOperator><Identifier><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_B"></ColumnReference></Identifier></ScalarOperator></Aggregate></ScalarOperator></DefinedValue></DefinedValues><RelOp NodeId="5" PhysicalOp="Stream Aggregate" LogicalOp="Aggregate" EstimateRows="3.47656" EstimateIO="0" EstimateCPU="3.98828e-006" AvgRowSize="11" EstimatedTotalSubtreeCost="0.00461844" Parallel="0" EstimateRebinds="4" EstimateRewinds="4" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_B"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="24" Batches="0" ActualExecutionMode="Row" ActualElapsedms="0" ActualCPUms="0" ActualEndOfScans="9" ActualExecutions="9"></RunTimeCountersPerThread></RunTimeInformation><StreamAggregate><DefinedValues></DefinedValues><GroupBy><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_B"></ColumnReference></GroupBy><RelOp NodeId="6" PhysicalOp="Index Seek" LogicalOp="Index Seek" EstimateRows="4.5" EstimatedRowsRead="4.5" EstimateIO="0.003125" EstimateCPU="0.00016195" AvgRowSize="11" EstimatedTotalSubtreeCost="0.00458255" TableCardinality="9" Parallel="0" EstimateRebinds="4" EstimateRewinds="4" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_B"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="45" Batches="0" ActualExecutionMode="Row" ActualElapsedms="0" ActualCPUms="0" ActualScans="9" ActualLogicalReads="18" ActualPhysicalReads="0" ActualReadAheads="0" ActualLobLogicalReads="0" ActualLobPhysicalReads="0" ActualLobReadAheads="0" ActualRowsRead="45" ActualEndOfScans="9" ActualExecutions="9"></RunTimeCountersPerThread></RunTimeInformation><IndexScan Ordered="1" ScanDirection="FORWARD" ForcedIndex="0" ForceSeek="0" ForceScan="0" NoExpandHint="0" Storage="RowStore"><DefinedValues><DefinedValue><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_B"></ColumnReference></DefinedValue></DefinedValues><Object Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Index="[i]" Alias="[mt2]" IndexKind="NonClustered" Storage="RowStore"></Object><SeekPredicates><SeekPredicateNew><SeekKeys><Prefix ScanType="EQ"><RangeColumns><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_A"></ColumnReference></RangeColumns><RangeExpressions><ScalarOperator ScalarString="[tempdb].[dbo].[#MyTable].[Col_A] as [mt].[Col_A]"><Identifier><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_A"></ColumnReference></Identifier></ScalarOperator></RangeExpressions></Prefix></SeekKeys></SeekPredicateNew></SeekPredicates></IndexScan></RelOp></StreamAggregate></RelOp></StreamAggregate></RelOp></ComputeScalar></RelOp></NestedLoops></RelOp></ComputeScalar></RelOp></QueryPlan></StmtSimple></Statements></Batch></BatchSequence></ShowPlanXML>
SELECT * FROM #MyTable AS mt CROSS APPLY ( SELECT COUNT(DISTINCT mt2.Col_B) AS dc FROM #MyTable AS mt2 WHERE mt2.Col_A = mt.Col_A -- GROUP BY mt2.Col_A ) AS ca
SELECT
SELECT
Cached plan size32 KB
Estimated Operator Cost0 (0%)
Estimated Subtree Cost0.0079721
Degree of Parallelism0
Estimated Number of Rows9
Statement
SELECT * FROM #MyTable AS mt CROSS APPLY ( SELECT COUNT(DISTINCT mt2.Col_B) AS dc FROM #MyTable AS mt2 WHERE mt2.Col_A = mt.Col_A -- GROUP BY mt2.Col_A ) AS ca
Compute Scalar
Cost: 0%
Compute Scalar
Compute new values from existing values in a row.
Physical OperationCompute Scalar
Logical OperationCompute Scalar
Estimated Execution ModeRow
Estimated Operator Cost0.0000009 (0%)
Estimated I/O Cost0
Estimated CPU Cost0.0000009
Estimated Subtree Cost0.0079721
Estimated Number of Executions1
Estimated Number of Rows9
Estimated Row Size21 B
Node ID0
Output List
[tempdb].[dbo].[#MyTable].[mt].Col_A
[tempdb].[dbo].[#MyTable].[mt].Col_B
Expr1004
Nested Loops
(Left Outer Join)
Cost: 0%
Nested Loops
For each row in the top (outer) input, scan the bottom (inner) input, and output matching rows.
Physical OperationNested Loops
Logical OperationLeft Outer Join
Actual Execution ModeRow
Estimated Execution ModeRow
Actual Number of Rows9
Actual Number of Batches0
Estimated Operator Cost0.0000376 (0%)
Estimated I/O Cost0
Estimated CPU Cost0.0000376
Estimated Subtree Cost0.0079712
Estimated Number of Executions1
Number of Executions1
Estimated Number of Rows9
Estimated Row Size21 B
Actual Rebinds0
Actual Rewinds0
Node ID1
Output List
[tempdb].[dbo].[#MyTable].[mt].Col_A
[tempdb].[dbo].[#MyTable].[mt].Col_B
Expr1004
Outer References
[tempdb].[dbo].[#MyTable].[mt].Col_A
Index Scan (NonClustered)
[#MyTable].[i].[mt]
Cost: 41%
Index Scan (NonClustered)
Physical OperationIndex Scan
Logical OperationIndex Scan
Actual Execution ModeRow
Estimated Execution ModeRow
StorageRowStore
Number of Rows Read9
Actual Number of Rows9
Actual Number of Batches0
Estimated Operator Cost0.0032919 (41%)
Estimated I/O Cost0.003125
Estimated CPU Cost0.0001669
Estimated Subtree Cost0.0032919
Estimated Number of Executions1
Number of Executions1
Estimated Number of Rows to be Read9
Estimated Number of Rows9
Estimated Row Size17 B
Actual Rebinds0
Actual Rewinds0
OrderedFalse
Node ID2
Output List
[tempdb].[dbo].[#MyTable].[mt].Col_A
[tempdb].[dbo].[#MyTable].[mt].Col_B
Object
[tempdb].[dbo].[#MyTable].[i].[mt]
Compute Scalar
Cost: 0%
Compute Scalar
Compute new values from existing values in a row.
Physical OperationCompute Scalar
Logical OperationCompute Scalar
Estimated Execution ModeRow
Estimated Operator Cost0 (0%)
Estimated I/O Cost0
Estimated CPU Cost0
Estimated Subtree Cost0.0046417
Estimated Number of Executions5
Estimated Number of Rows1
Estimated Row Size11 B
Node ID3
Output List
Expr1004
Stream Aggregate
(Aggregate)
Cost: 0%
Stream Aggregate
Compute summary values for groups of rows in a suitably sorted stream.
Physical OperationStream Aggregate
Logical OperationAggregate
Actual Execution ModeRow
Estimated Execution ModeRow
Actual Number of Rows9
Actual Number of Batches0
Estimated Operator Cost0.0000233 (0%)
Estimated I/O Cost0
Estimated CPU Cost0.0000026
Estimated Subtree Cost0.0046417
Estimated Number of Executions5
Number of Executions9
Estimated Number of Rows1
Estimated Row Size11 B
Actual Rebinds0
Actual Rewinds0
Node ID4
Output List
Expr1007
Stream Aggregate
(Aggregate)
Cost: 0%
Stream Aggregate
Compute summary values for groups of rows in a suitably sorted stream.
Physical OperationStream Aggregate
Logical OperationAggregate
Actual Execution ModeRow
Estimated Execution ModeRow
Actual Number of Rows24
Actual Number of Batches0
Estimated Operator Cost0.0000359 (0%)
Estimated I/O Cost0
Estimated CPU Cost0.000004
Estimated Subtree Cost0.0046184
Estimated Number of Executions5
Number of Executions9
Estimated Number of Rows3.47656
Estimated Row Size11 B
Actual Rebinds0
Actual Rewinds0
Node ID5
Output List
[tempdb].[dbo].[#MyTable].[mt2].Col_B
Index Seek (NonClustered)
[#MyTable].[i].[mt2]
Cost: 57%
Index Seek (NonClustered)
Scan a particular range of rows from a nonclustered index.
Physical OperationIndex Seek
Logical OperationIndex Seek
Actual Execution ModeRow
Estimated Execution ModeRow
StorageRowStore
Number of Rows Read45
Actual Number of Rows45
Actual Number of Batches0
Estimated Operator Cost0.0045826 (57%)
Estimated I/O Cost0.003125
Estimated CPU Cost0.000162
Estimated Subtree Cost0.0045826
Estimated Number of Executions5
Number of Executions9
Estimated Number of Rows to be Read4.5
Estimated Number of Rows4.5
Estimated Row Size11 B
Actual Rebinds0
Actual Rewinds0
OrderedTrue
Node ID6
Output List
[tempdb].[dbo].[#MyTable].[mt2].Col_B
Object
[tempdb].[dbo].[#MyTable].[i].[mt2]
Seek Predicates
Seek Keys[1]: Prefix: [tempdb].[dbo].[#MyTable].Col_A = Scalar Operator([tempdb].[dbo].[#MyTable].[Col_A] as [mt].[Col_A])
set statistics xml on;

SELECT *
FROM #MyTable AS mt
CROSS APPLY ( SELECT COUNT(DISTINCT mt2.Col_B) AS dc
FROM #MyTable AS mt2
WHERE mt2.Col_A = mt.Col_A
GROUP BY mt2.Col_A
) AS ca;
Col_A Col_B dc
A 1 3
A 1 3
A 2 3
A 2 3
A 2 3
A 3 3
B 4 2
B 4 2
B 5 2
Microsoft SQL Server 2005 XML Showplan
<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan" Version="1.481" Build="14.0.3335.7"><BatchSequence><Batch><Statements><StmtSimple StatementText="SELECT *&#xa;FROM #MyTable AS mt&#xa;CROSS APPLY ( SELECT COUNT(DISTINCT mt2.Col_B) AS dc&#xa; FROM #MyTable AS mt2&#xa; WHERE mt2.Col_A = mt.Col_A&#xa; GROUP BY mt2.Col_A &#xa; ) AS ca" StatementId="1" StatementCompId="2" StatementType="SELECT" RetrievedFromCache="true" StatementSubTreeCost="0.00678942" StatementEstRows="9" SecurityPolicyApplied="false" StatementOptmLevel="FULL" QueryHash="0xC6FF644F2AB61382" QueryPlanHash="0xEB6AA18104A755FE" StatementOptmEarlyAbortReason="GoodEnoughPlanFound" CardinalityEstimationModelVersion="140"><StatementSetOptions QUOTED_IDENTIFIER="true" ARITHABORT="true" CONCAT_NULL_YIELDS_NULL="true" ANSI_NULLS="true" ANSI_PADDING="true" ANSI_WARNINGS="true" NUMERIC_ROUNDABORT="false"></StatementSetOptions><QueryPlan DegreeOfParallelism="0" NonParallelPlanReason="NoParallelPlansInDesktopOrExpressEdition" CachedPlanSize="32" CompileTime="0" CompileCPU="0" CompileMemory="280"><MemoryGrantInfo SerialRequiredMemory="0" SerialDesiredMemory="0"></MemoryGrantInfo><OptimizerHardwareDependentProperties EstimatedAvailableMemoryGrant="838808" EstimatedPagesCached="52425" EstimatedAvailableDegreeOfParallelism="1" MaxCompileMemory="904904"></OptimizerHardwareDependentProperties><OptimizerStatsUsage><StatisticsInfo LastUpdate="2020-11-03T17:12:34.89" ModificationCount="0" SamplingPercent="100" Statistics="[i]" Table="[#MyTable____________________________________________________________________________________________________________0000000000BC]" Schema="[dbo]" Database="[tempdb]"></StatisticsInfo></OptimizerStatsUsage><TraceFlags IsCompileTime="1"><TraceFlag Value="460" Scope="Global"></TraceFlag><TraceFlag Value="8017" Scope="Global"></TraceFlag></TraceFlags><TraceFlags IsCompileTime="0"><TraceFlag Value="460" Scope="Global"></TraceFlag><TraceFlag Value="8017" Scope="Global"></TraceFlag></TraceFlags><QueryTimeStats ElapsedTime="0" CpuTime="0"></QueryTimeStats><RelOp NodeId="0" PhysicalOp="Nested Loops" LogicalOp="Inner Join" EstimateRows="9" EstimateIO="0" EstimateCPU="3.762e-005" AvgRowSize="21" EstimatedTotalSubtreeCost="0.00678942" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_A"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_B"></ColumnReference><ColumnReference Column="Expr1004"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="9" Batches="0" ActualExecutionMode="Row" ActualElapsedms="0" ActualCPUms="0" ActualEndOfScans="1" ActualExecutions="1"></RunTimeCountersPerThread></RunTimeInformation><NestedLoops Optimized="0"><OuterReferences><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_A"></ColumnReference></OuterReferences><RelOp NodeId="1" PhysicalOp="Compute Scalar" LogicalOp="Compute Scalar" EstimateRows="2" EstimateIO="0" EstimateCPU="0" AvgRowSize="16" EstimatedTotalSubtreeCost="0.0033029" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_A"></ColumnReference><ColumnReference Column="Expr1004"></ColumnReference></OutputList><ComputeScalar><DefinedValues><DefinedValue><ColumnReference Column="Expr1004"></ColumnReference><ScalarOperator ScalarString="CONVERT_IMPLICIT(int,[Expr1007],0)"><Convert DataType="int" Style="0" Implicit="1"><ScalarOperator><Identifier><ColumnReference Column="Expr1007"></ColumnReference></Identifier></ScalarOperator></Convert></ScalarOperator></DefinedValue></DefinedValues><RelOp NodeId="2" PhysicalOp="Stream Aggregate" LogicalOp="Aggregate" EstimateRows="2" EstimateIO="0" EstimateCPU="4e-006" AvgRowSize="16" EstimatedTotalSubtreeCost="0.0033029" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_A"></ColumnReference><ColumnReference Column="Expr1007"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="2" Batches="0" ActualExecutionMode="Row" ActualElapsedms="0" ActualCPUms="0" ActualEndOfScans="1" ActualExecutions="1"></RunTimeCountersPerThread></RunTimeInformation><StreamAggregate><DefinedValues><DefinedValue><ColumnReference Column="Expr1007"></ColumnReference><ScalarOperator ScalarString="COUNT([tempdb].[dbo].[#MyTable].[Col_B] as [mt2].[Col_B])"><Aggregate Distinct="0" AggType="COUNT_BIG"><ScalarOperator><Identifier><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_B"></ColumnReference></Identifier></ScalarOperator></Aggregate></ScalarOperator></DefinedValue></DefinedValues><GroupBy><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_A"></ColumnReference></GroupBy><RelOp NodeId="3" PhysicalOp="Stream Aggregate" LogicalOp="Aggregate" EstimateRows="5" EstimateIO="0" EstimateCPU="7e-006" AvgRowSize="16" EstimatedTotalSubtreeCost="0.0032989" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_A"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_B"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="5" Batches="0" ActualExecutionMode="Row" ActualElapsedms="0" ActualCPUms="0" ActualEndOfScans="1" ActualExecutions="1"></RunTimeCountersPerThread></RunTimeInformation><StreamAggregate><DefinedValues></DefinedValues><GroupBy><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_A"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_B"></ColumnReference></GroupBy><RelOp NodeId="4" PhysicalOp="Index Scan" LogicalOp="Index Scan" EstimateRows="9" EstimatedRowsRead="9" EstimateIO="0.003125" EstimateCPU="0.0001669" AvgRowSize="16" EstimatedTotalSubtreeCost="0.0032919" TableCardinality="9" Parallel="0" EstimateRebinds="0" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_A"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_B"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="9" Batches="0" ActualExecutionMode="Row" ActualElapsedms="0" ActualCPUms="0" ActualScans="1" ActualLogicalReads="2" ActualPhysicalReads="0" ActualReadAheads="0" ActualLobLogicalReads="0" ActualLobPhysicalReads="0" ActualLobReadAheads="0" ActualRowsRead="9" ActualEndOfScans="1" ActualExecutions="1"></RunTimeCountersPerThread></RunTimeInformation><IndexScan Ordered="1" ScanDirection="FORWARD" ForcedIndex="0" ForceSeek="0" ForceScan="0" NoExpandHint="0" Storage="RowStore"><DefinedValues><DefinedValue><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_A"></ColumnReference></DefinedValue><DefinedValue><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_B"></ColumnReference></DefinedValue></DefinedValues><Object Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Index="[i]" Alias="[mt2]" IndexKind="NonClustered" Storage="RowStore"></Object></IndexScan></RelOp></StreamAggregate></RelOp></StreamAggregate></RelOp></ComputeScalar></RelOp><RelOp NodeId="5" PhysicalOp="Index Seek" LogicalOp="Index Seek" EstimateRows="4.5" EstimatedRowsRead="4.5" EstimateIO="0.003125" EstimateCPU="0.00016195" AvgRowSize="17" EstimatedTotalSubtreeCost="0.0034489" TableCardinality="9" Parallel="0" EstimateRebinds="1" EstimateRewinds="0" EstimatedExecutionMode="Row"><OutputList><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_A"></ColumnReference><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_B"></ColumnReference></OutputList><RunTimeInformation><RunTimeCountersPerThread Thread="0" ActualRows="9" Batches="0" ActualExecutionMode="Row" ActualElapsedms="0" ActualCPUms="0" ActualScans="2" ActualLogicalReads="4" ActualPhysicalReads="0" ActualReadAheads="0" ActualLobLogicalReads="0" ActualLobPhysicalReads="0" ActualLobReadAheads="0" ActualRowsRead="9" ActualEndOfScans="2" ActualExecutions="2"></RunTimeCountersPerThread></RunTimeInformation><IndexScan Ordered="1" ScanDirection="FORWARD" ForcedIndex="0" ForceSeek="0" ForceScan="0" NoExpandHint="0" Storage="RowStore"><DefinedValues><DefinedValue><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_A"></ColumnReference></DefinedValue><DefinedValue><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_B"></ColumnReference></DefinedValue></DefinedValues><Object Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Index="[i]" Alias="[mt]" IndexKind="NonClustered" Storage="RowStore"></Object><SeekPredicates><SeekPredicateNew><SeekKeys><Prefix ScanType="EQ"><RangeColumns><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt]" Column="Col_A"></ColumnReference></RangeColumns><RangeExpressions><ScalarOperator ScalarString="[tempdb].[dbo].[#MyTable].[Col_A] as [mt2].[Col_A]"><Identifier><ColumnReference Database="[tempdb]" Schema="[dbo]" Table="[#MyTable]" Alias="[mt2]" Column="Col_A"></ColumnReference></Identifier></ScalarOperator></RangeExpressions></Prefix></SeekKeys></SeekPredicateNew></SeekPredicates></IndexScan></RelOp></NestedLoops></RelOp></QueryPlan></StmtSimple></Statements></Batch></BatchSequence></ShowPlanXML>
SELECT * FROM #MyTable AS mt CROSS APPLY ( SELECT COUNT(DISTINCT mt2.Col_B) AS dc FROM #MyTable AS mt2 WHERE mt2.Col_A = mt.Col_A GROUP BY mt2.Col_A ) AS ca
SELECT
SELECT
Cached plan size32 KB
Estimated Operator Cost0 (0%)
Estimated Subtree Cost0.0067894
Degree of Parallelism0
Estimated Number of Rows9
Statement
SELECT * FROM #MyTable AS mt CROSS APPLY ( SELECT COUNT(DISTINCT mt2.Col_B) AS dc FROM #MyTable AS mt2 WHERE mt2.Col_A = mt.Col_A GROUP BY mt2.Col_A ) AS ca
Nested Loops
(Inner Join)
Cost: 1%
Nested Loops
For each row in the top (outer) input, scan the bottom (inner) input, and output matching rows.
Physical OperationNested Loops
Logical OperationInner Join
Actual Execution ModeRow
Estimated Execution ModeRow
Actual Number of Rows9
Actual Number of Batches0
Estimated Operator Cost0.0000376 (1%)
Estimated I/O Cost0
Estimated CPU Cost0.0000376
Estimated Subtree Cost0.0067894
Estimated Number of Executions1
Number of Executions1
Estimated Number of Rows9
Estimated Row Size21 B
Actual Rebinds0
Actual Rewinds0
Node ID0
Output List
[tempdb].[dbo].[#MyTable].[mt].Col_A
[tempdb].[dbo].[#MyTable].[mt].Col_B
Expr1004
Outer References
[tempdb].[dbo].[#MyTable].[mt2].Col_A
Compute Scalar
Cost: 0%
Compute Scalar
Compute new values from existing values in a row.
Physical OperationCompute Scalar
Logical OperationCompute Scalar
Estimated Execution ModeRow
Estimated Operator Cost0 (0%)
Estimated I/O Cost0
Estimated CPU Cost0
Estimated Subtree Cost0.0033029
Estimated Number of Executions1
Estimated Number of Rows2
Estimated Row Size16 B
Node ID1
Output List
[tempdb].[dbo].[#MyTable].[mt2].Col_A
Expr1004
Stream Aggregate
(Aggregate)
Cost: 0%
Stream Aggregate
Compute summary values for groups of rows in a suitably sorted stream.
Physical OperationStream Aggregate
Logical OperationAggregate
Actual Execution ModeRow
Estimated Execution ModeRow
Actual Number of Rows2
Actual Number of Batches0
Estimated Operator Cost0.000004 (0%)
Estimated I/O Cost0
Estimated CPU Cost0.000004
Estimated Subtree Cost0.0033029
Estimated Number of Executions1
Number of Executions1
Estimated Number of Rows2
Estimated Row Size16 B
Actual Rebinds0
Actual Rewinds0
Node ID2
Output List
[tempdb].[dbo].[#MyTable].[mt2].Col_A
Expr1007
Stream Aggregate
(Aggregate)
Cost: 0%
Stream Aggregate
Compute summary values for groups of rows in a suitably sorted stream.
Physical OperationStream Aggregate
Logical OperationAggregate
Actual Execution ModeRow
Estimated Execution ModeRow
Actual Number of Rows5
Actual Number of Batches0
Estimated Operator Cost0.000007 (0%)
Estimated I/O Cost0
Estimated CPU Cost0.000007
Estimated Subtree Cost0.0032989
Estimated Number of Executions1
Number of Executions1
Estimated Number of Rows5
Estimated Row Size16 B
Actual Rebinds0
Actual Rewinds0
Node ID3
Output List
[tempdb].[dbo].[#MyTable].[mt2].Col_A
[tempdb].[dbo].[#MyTable].[mt2].Col_B
Index Scan (NonClustered)
[#MyTable].[i].[mt2]
Cost: 48%
Index Scan (NonClustered)
Physical OperationIndex Scan
Logical OperationIndex Scan
Actual Execution ModeRow
Estimated Execution ModeRow
StorageRowStore
Number of Rows Read9
Actual Number of Rows9
Actual Number of Batches0
Estimated Operator Cost0.0032919 (48%)
Estimated I/O Cost0.003125
Estimated CPU Cost0.0001669
Estimated Subtree Cost0.0032919
Estimated Number of Executions1
Number of Executions1
Estimated Number of Rows to be Read9
Estimated Number of Rows9
Estimated Row Size16 B
Actual Rebinds0
Actual Rewinds0
OrderedTrue
Node ID4
Output List
[tempdb].[dbo].[#MyTable].[mt2].Col_A
[tempdb].[dbo].[#MyTable].[mt2].Col_B
Object
[tempdb].[dbo].[#MyTable].[i].[mt2]
Index Seek (NonClustered)
[#MyTable].[i].[mt]
Cost: 51%
Index Seek (NonClustered)
Scan a particular range of rows from a nonclustered index.
Physical OperationIndex Seek
Logical OperationIndex Seek
Actual Execution ModeRow
Estimated Execution ModeRow
StorageRowStore
Number of Rows Read9
Actual Number of Rows9
Actual Number of Batches0
Estimated Operator Cost0.0034489 (51%)
Estimated I/O Cost0.003125
Estimated CPU Cost0.000162
Estimated Subtree Cost0.0034489
Estimated Number of Executions2
Number of Executions2
Estimated Number of Rows to be Read4.5
Estimated Number of Rows4.5
Estimated Row Size17 B
Actual Rebinds0
Actual Rewinds0
OrderedTrue
Node ID5
Output List
[tempdb].[dbo].[#MyTable].[mt].Col_A
[tempdb].[dbo].[#MyTable].[mt].Col_B
Object
[tempdb].[dbo].[#MyTable].[i].[mt]
Seek Predicates
Seek Keys[1]: Prefix: [tempdb].[dbo].[#MyTable].Col_A = Scalar Operator([tempdb].[dbo].[#MyTable].[Col_A] as [mt2].[Col_A])