DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Actual Execution Plan Costs

Actual Execution Plan Costs

Let's take a look at actual execution plans and find out why they don't have actual execution plan costs.

Grant Fritchey user avatar by
Grant Fritchey
·
Aug. 22, 18 · Opinion
Like (2)
Save
Tweet
Share
4.76K Views

Join the DZone community and get the full member experience.

Join For Free

Why don't "actual execution plans" have "actual execution plan costs"?

This is a question and a myth I have to fight against all the time. It's so hard to convince people that all execution plans are estimated plans in the first place (by the way, all execution plans are estimated plans). If we execute a query at the same time we capture a plan, we have enabled SQL Server to also capture run-time metrics with that plan. So we end up with what is known as an actual plan, but it's still just an estimated plan plus those run-time metrics.

Execution Plan Costs

When you look at a given operator within an estimated plan, it's going to show you four numbers related to cost:

  • Estimated CPU Cost
  • Estimated I/O Cost
  • Estimated Operator Cost
  • Estimated Subtree Cost

The values in the CPU and I/O cost represent calculations, usually based on the row counts supplied by statistics, but they can come from other sources as well. It's all very internal to the query optimizer, although, with some experimentation and testing, you can derive some of the calculations that arrive at the costs. Those costs are also not reflective of your machine. Yeah, it says CPU and I/O, but it's not measuring your computer to determine it's bandwidth and then reporting back to you. These are just estimated values. I recommend that you don't really think of it as CPU or I/O.

Focus instead on the overall Estimated Operator Cost (which, usually, consists of adding the other two costs). When you think about that cost, just think about it as Cost Units, not a literal interpretation of CPU and I/O. It'll help you keep your sanity.

The final value, the Estimated Subtree Cost, is the cumulative (added up in NodeID order) costs of each individual operator.

Everyone can agree. On an estimated plan, you work with estimated values.

Actual Execution Plan Costs

Then, we get the actual execution plan. The actual execution plan has performance metrics. In Azure SQL Database and in SQL Server 2017, the plan captures Actual Time Statistics, literally, elapsed time and elapsed CPU time if it's greater than 1ms. The plan captures this information for each operator as well as for the overall plan.

Each plan also captures the Actual I/O Statistics. These represent the number of reads, broken down by type.

  • Actual Lob Logical Reads
  • Actual Lob Physical Reads
  • Actual Lob Read Aheads
  • Actual Logical Reads
  • Actual Physical Reads
  • Actual Read Aheads
  • Actual Scans

Therefore, with all this information, clearly, I'm wrong and the costs shown on the plan are the actual execution plan costs, not a repeat of the estimated plan costs.

No.

Let's do an experiment and take a query:

SELECT  c.CustomerID,
a.City,
s.Name,
st.Name
FROM    Sales.Customer AS c
JOIN    Sales.Store AS s
ON c.StoreID = s.BusinessEntityID
JOIN    Sales.SalesTerritory AS st
ON c.TerritoryID = st.TerritoryID
JOIN    Person.BusinessEntityAddress AS bea
ON c.CustomerID = bea.BusinessEntityID
JOIN    Person.Address AS a
ON bea.AddressID = a.AddressID
JOIN    Person.StateProvince AS sp
ON a.StateProvinceID = sp.StateProvinceID
WHERE   st.Name = 'Northeast'
AND sp.Name = 'New York';

We'll capture the actual plan for this query and save it to a file. Then, we'll capture the actual plan. Finally, we compare the plans using that functionality within SSMS 17. The results showing the Operator Cost and the Subtree Cost are right here:

You see that these are the same value. Despite the fact that the estimated number of rows is 141 and the actual number is 3. While the information may be available for an actual execution plan cost to be generated, it is not.

Conclusion

Why doesn't Microsoft show us different costs for different plans? The information exists. We see it in the plans. The next step should be simple. However, as you can see, they are not taking that step. Not yet anyway.

Execution (computing) Database

Published at DZone with permission of Grant Fritchey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Gentle Introduction to Kubernetes
  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything
  • Use AWS Controllers for Kubernetes To Deploy a Serverless Data Processing Solution With SQS, Lambda, and DynamoDB
  • A Beginner's Guide to Infrastructure as Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: