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

推荐订阅源

Engineering at Meta
Engineering at Meta
博客园_首页
H
Help Net Security
WordPress大学
WordPress大学
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
B
Blog
I
InfoQ
SecWiki News
SecWiki News
T
Tailwind CSS Blog
Spread Privacy
Spread Privacy
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
M
MIT News - Artificial intelligence
S
Schneier on Security
T
Threat Research - Cisco Blogs
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The Cloudflare Blog
Recent Announcements
Recent Announcements
Security Latest
Security Latest
G
GRAHAM CLULEY
IT之家
IT之家
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
腾讯CDC
Google DeepMind News
Google DeepMind News
V
V2EX
S
Securelist
TaoSecurity Blog
TaoSecurity Blog
B
Blog RSS Feed
S
SegmentFault 最新的问题
博客园 - 叶小钗
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Project Zero
Project Zero
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure

博客园 - 天蝎

两岸三地在线编程学习网站 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.