惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
量子位
IT之家
IT之家
Jina AI
Jina AI
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
人人都是产品经理
人人都是产品经理
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
AWS News Blog
AWS News Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
P
Proofpoint News Feed
S
Schneier on Security
Spread Privacy
Spread Privacy
The Hacker News
The Hacker News
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
L
LINUX DO - 热门话题
博客园 - 聂微东
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Security Latest
Security Latest
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
K
Kaspersky official blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
Last Week in AI
Last Week in AI
博客园 - Franky
G
GRAHAM CLULEY
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
L
LINUX DO - 最新话题
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
P
Privacy International News Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
Schneier on Security
Schneier on Security
V
V2EX
V
Visual Studio Blog
S
Security @ Cisco Blogs
博客园 - 叶小钗
H
Hacker News: Front Page
小众软件
小众软件
WordPress大学
WordPress大学
V2EX - 技术
V2EX - 技术
美团技术团队

博客园 - 天蝎

两岸三地在线编程学习网站 WebinoImageThumb - ZendFramework 2中操作图片的API PHP依赖管理器- Composer Local Temporary Tables and Table Variables Logging with SQL Server SQL Server Database Partitioning Myths and Truths 转载:什么是SHELL 转载:Plan freezing and other plan guide enhancements in SQL Server 2008 - 天蝎 转载:Breaking ownership chaining within a schema in SQL Server 转载:如何正确理解自动化测试技术 存储过程 SOA的设计理念 转载: The DBA as Detective: Troubleshooting Locking and Blocking 转载:锁的概述 转载: Microsoft SQL Server roles Why BCP connects to SQL Server instance which start with account of Network Service fail? Finding the causes of poor performance in SQL Server Enabling Dedicated Administrator Connection Feature in SQL Server 2008 .net中应用程序域的概念
Execution plans, estimated vs actual
天蝎 · 2009-07-28 · via 博客园 - 天蝎

First however, a bit on query execution, just so that I know everyone’s on the same page.

When a query is submitted to SQL Server (and for simplicity I’m going to assume it’s a straight select statement not a procedure) the query is parsed, then bound to the underlying objects(tables, views, functions, etc). Once the binding is complete, the query passes to the query optimiser. The optimiser produces one or more suitable execution plans for the query (more on that in a later post). The query is then passed into the query execution engine, which does the memory grants, picks a parallelism option, if necessary and executes the various query operations.

Estimated execution plans

When an estimated execution plan is requested for a query, the query goes through the parsing, binding and optimisation phases, but does not get executed.

An estimated execution plan can be obtained by issuing any of the following SET statements before executing the query, or by selecting the ‘Show Estimated Execution Plan’ button from the toolbar in either Management Studio or Query Analyser

SET SHOWPLAN_ALL ON
SET SHOWPLAN_XML ON
SET SHOWPLAN_TEXT ON

Actual execution plan

An actual execution plan is returned after a query has completed executing. Hence the query goes through all four phases.

An actual execution plan can be obtained by issuing any of the following SET statements before executing the query or by selecting the ‘Include Actual execution Plan’ button from the toolbar

SET STATISTICS_PROFILE ON
SET STATISTICS_XML ON

Differences

Since for an estimated execution plan the query is not executed, it will not include the actual number of rows affected by the various query operators. Also the estimated plan will not include details on the number of threads used or the number of rewinds or rebinds.

Since only the estimated number of rows is available in the estimated plan, and that row estimate is derived from table statistics, the estimated plans can look very far off for queries that contain any data source that does not have statistics (table variables, remote data sources, openXML). I have seen an estimated plan that involved several remote data sources where the estimated row count at the end of the query was somewhere around 56 billion. In reality, when the query was executed only 27 rows were returned.

Another point about estimated plans is that, since the queries are not run, any procedure that involves DDL operations may fail to produce an estimated plan (e.g. a procedure that creates a temp table, populates it then queries it will give an error when an estimated plan is requested, since the temp table does not exist).

Finally

So, that’s what they are. As for which to use, personally I prefer the actual execution plan, unless there is some constraint that prevents the running of the query on a test machine, like a very long running query, . Especially now with SQL 2005 where the XML plan can be saved to disk as a sqlplan file, which is a feature that was sorely lacking in SQL 2000.

For more on actual and estimated plans, see the article that Kalen Delaney wrote recently.

I’m not sure at the moment what the next post on execution plans will be about, it depends on what comes to mind. If there’s anything specific about exec plans that you’d like to know, post a comment and I’ll see what I can do.