HiveBrain v1.2.0
Get Started
← Back to all entries
gotchasqlModerate

Why does this execution plan have Compute Scalars?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
thiswhycomputehavescalarsplandoesexecution

Problem

I have a simple SELECT statement.

USE [AdventureWorks2014]
GO

SELECT *
FROM Sales.SalesOrderDetail sod


The execution plan has two Compute Scalar.

Why is this? I was expecting to just get the Index Scan or maybe a Table Scan?

The first (rght-most) has

[[AdventureWorks2014].[Sales].[SalesOrderDetail].LineTotal] = Scalar Operator(isnull(CONVERT_IMPLICIT(numeric(19,4),[AdventureWorks2014].[Sales].[SalesOrderDetail].[UnitPrice] as [sod].[UnitPrice],0)*((1.0)-CONVERT_IMPLICIT(numeric(19,4),[AdventureWorks2014].[Sales].[SalesOrderDetail].[UnitPriceDiscount] as [sod].[UnitPriceDiscount],0))*CONVERT_IMPLICIT(numeric(5,0),[AdventureWorks2014].[Sales].[SalesOrderDetail].[OrderQty] as [sod].[OrderQty],0),(0.000000)))


When second has:

[[sod].LineTotal] = Scalar Operator([AdventureWorks2014].[Sales].[SalesOrderDetail].[LineTotal] as [sod].[LineTotal])

Solution

The Compute Scalars are related to the computed field LineTotal. Script out the table, and you'll see that field defined as:

[LineTotal]  AS (isnull(([UnitPrice]*((1.0)-[UnitPriceDiscount]))*[OrderQty],(0.0))),


SQL Server has two operations to perform. It has to first run the calculation of:

[UnitPrice]*((1.0)-[UnitPriceDiscount]))*[OrderQty]


Then it has to check to see if that value is null, and if so, substitute 0.0.

Code Snippets

[LineTotal]  AS (isnull(([UnitPrice]*((1.0)-[UnitPriceDiscount]))*[OrderQty],(0.0))),
[UnitPrice]*((1.0)-[UnitPriceDiscount]))*[OrderQty]

Context

StackExchange Database Administrators Q#152418, answer score: 18

Revisions (0)

No revisions yet.