Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Compute Scalar

Compute Scalar is an operator that computes new values from existing data within a row. It does not read data from disk, only processes data in memory.

Common Use Cases

  • Calculations: Quantity * Price AS TotalAmount
  • Functions: UPPER(FirstName), YEAR(OrderDate)
  • Type Casting: CAST(OrderId AS VARCHAR)
  • Concatenation: FirstName + ' ' + LastName

Parameters Explained

1. Physical Operation / Logical Operation

Physical Operation: Compute Scalar
Logical Operation: Compute Scalar
  • Physical Operation: How SQL Server executes (compute scalar)
  • Logical Operation: Logical meaning (also compute scalar)

In this case, both are the same since the operator has no special variants.

2. Estimated Execution Mode

Estimated Execution Mode: Row
  • Row: Processes row-by-row
  • Batch: Processes in batch mode (usually with columnstore indexes)

This is a simple operator, always running in row mode.

3. Estimated Operator Cost

Estimated Operator Cost: 0.001982 (2%)

Most important!

  • 0.001982: Absolute cost of this operator
  • (2%): Relative cost compared to the entire query (2% of total cost)

Meaning:

  • This operator only takes 2% of the total query cost → NOT A BOTTLENECK
  • If this number is > 30-50%, you should consider optimization

4. Estimated I/O Cost

Estimated I/O Cost: 0
  • This is the I/O cost (reading from disk)
  • 0 means this operator does not read data from disk, only processes data already in memory

✅ This is good - no I/O here.

5. Estimated Subtree Cost

Estimated Subtree Cost: 0.117937
  • Total cost of the entire subtree from this operator downward
  • Includes the current operator and all child operators

Meaning:

  • This is the accumulated cost
  • In the plan, you’ll see this value increase as you go up the tree toward the root

6. Estimated CPU Cost

Estimated CPU Cost: 0.001982
  • CPU cost specifically for this operator
  • In this case, it equals the Estimated Operator Cost (0.001982) because there is no I/O

Comparison with I/O Cost:

CPU Cost = 0.001982
I/O Cost = 0
Total = 0.001982

7. Estimated Number of Executions

Estimated Number of Executions: 1
  • How many times this operator is executed
  • 1 is normal for top-level operators
  • If > 1, the operator may be in a loop (nested loop join) and could be a performance issue

8. Estimated Number of Rows

Estimated Number of Rows: 19820
  • SQL Server estimates this operator will process 19,820 rows
  • This is the cardinality estimate - very important for plan selection

Comparison with Actual Rows:

  • If Actual Rows differs significantly from Estimated Rows → statistics issue

9. Estimated Row Size

Estimated Row Size: 56 B
  • Each row takes approximately 56 bytes in memory
  • Used to estimate memory grant

Calculation:

56 bytes × 19,820 rows ≈ 1.1 MB (memory required)

10. Node ID

Node ID: 0
  • ID of the operator in the execution plan
  • Used for referencing, debugging, or searching in the XML plan

Overall Analysis

Based on these parameters, here’s the assessment:

✅ Good Points:

MetricValueAssessment
I/O Cost0No disk reads
CPU Cost0.001982Very small
% Cost2%Not a bottleneck
Executions1No loop repetition

⚠️ Needs Checking:

  • Estimated Rows = 19,820: Compare with Actual Rows in the actual plan (SET STATISTICS XML ON)
  • If Actual Rows >> 19,820 (e.g., 200,000) → Statistics outdated → Run UPDATE STATISTICS

Real-world Examples: When Compute Scalar Becomes an Issue?

Bad Case (bottleneck):

| Operator          | Cost    | Rows   | Executions |
|-------------------|---------|--------|------------|
| Compute Scalar    | 85%     | 1M     | 1000       |

Problem:

  • Takes 85% CPU, runs 1000 times (in a loop)
  • Consider: move calculation to application, use computed column, or optimize join logic

Your Case (good):

| Operator          | Cost    | Rows   | Executions |
|-------------------|---------|--------|------------|
| Compute Scalar    | 2%      | 19,820 | 1          |

No optimization needed, focus on other operators with higher cost.

How to View Actual vs Estimated in SSMS

To see actual metrics:

-- Enable Actual Execution Plan (Ctrl + M)
SET STATISTICS XML ON; -- Or use SSMS UI

-- Run your query
SELECT 
    OrderId,
    Quantity * Price AS TotalAmount, -- Compute Scalar will appear
    UPPER(CustomerName) AS UpperName
FROM Orders
WHERE OrderDate > '2024-01-01';

Then in the Execution Plan, hover over the operator to see both Estimated and Actual:

Actual Number of Rows: 19,820    (if it matches Estimated → good)
Actual Number of Executions: 1   (if > 1 → potential issue)

Summary

ParameterYour ValueMeaning
Operator Cost0.001982 (2%)✅ No concern, only 2% of query
I/O Cost0✅ No disk reads, CPU only
Estimated Rows19,820⚠️ Need to compare with Actual Rows
Executions1✅ No loop
Row Size56 B✅ Small, low memory footprint

Conclusion: This Compute Scalar operator is not a problem to optimize. If the query is slow, look for operators with higher cost (table scan, index scan, hash join) or check if Estimated Rows differs significantly from Actual Rows.