Inside TSQL Querying - Chapter 2. Physical Query Processing
Summary Description
The SQL language is spoken by most database experts, and all relational database products include some dialect of the SQL standard. Nevertheless, each product has its own particular query-processing mechanism. Understanding the way a database engine processes queries helps software architects, designers, and programmers make good choices when designing database schemas and writing queries.
When a query reaches the database engine, the SQL Server performs two major steps to produce the desired query result. The first step is query compilation, which generates a query plan, and the second step is the execution of the query plan.
Query compilation in SQL Server 2005 consists of three steps: parsing, algebrization, and query optimization. After those steps are completed, the compiler stores the optimized query plan in the procedure cache. There, the execution engine copies the plan into its executable form and subsequently executes the steps in the query plan to produce the query result. If the same query or stored procedure is executed again and the plan is located in the procedure cache, the compilation step is skipped and the query or stored procedure proceeds directly to execution reusing the stored plan.
Commands Generating Various Formats of a Showplan
|
Content |
Text |
XML |
Graphical |
|---|---|---|---|
|
Operators |
SET SHOWPLAN_TEXT ON |
N/A |
N/A |
|
Operators and estimated costs |
SET SHOWPLAN_ALL ON |
SET SHOWPLAN_XML ON |
Display Estimated Execution Plan in Management Studio |
|
Run-time info |
SET STATISTICS PROFILE ON |
SET STATISTICS XML ON |
Include Actual Execution Plan in Management Studio |
Showplan-Related Trace Event Classes
|
Trace Event Class |
Compile or Run |
Includes run-time info |
Includes XML showplan |
Generates trace against SQL Server 2000 |
|---|---|---|---|---|
|
Showplan All |
Run |
No |
No |
Yes |
|
Showplan All for Query Compile |
Compile |
No |
No |
No |
|
Showplan Statistics Profile |
Run |
Yes[1] |
No |
Yes |
|
Showplan Text |
Run |
No |
No |
Yes |
|
Showplan Text (Unencoded) |
Run |
No |
No |
Yes[2] |
|
Showplan XML |
Run |
No |
Yes |
No |
|
Showplan XML for Query Compile |
Compile |
No |
Yes |
No |
|
Showplan XML Statistics Profile |
Run |
Yes[3] |
Yes |
No |
|
Performance Statistics |
Compile and Run[4] |
Yes[5] |
Yes |
No |
Update Plans
The query optimizer must take care of several specific issues when optimizing INSERT, UPDATE, and DELETEor, in other words, data modifyingstatements. Here I will describe the techniques employed by the SQL Server to process these statements.
The IUD (shorthand I will use for "INSERT, UPDATE, and DELETE") plans have two stages. The first stage is read only, and it determines which rows need to be inserted/updated/deleted by generating a data stream describing the changes to be made. For INSERTs, the data stream contains column values; for DELETEs, it has the table keys; and for UPDATEs, it has both the table keys and the values of changed columns. The second stage applies changes in the data stream to the table; additionally, it takes actions necessary to preserve data integrity by performing constraint validation, it maintains nonclustered indexes and indexed views, and it fires triggers if they exist. Usually the UPDATE and DELETE query plans contain two references to the target table: the first reference is used to identify the affected rows and the second to perform the change. The INSERT plans contain only one reference to the target table unless the same target table also participates in generating the inserted rows.
In some simple cases, SQL Server merges the read and write stages of the IUD plans together. This is the case, for example, when inserting values directly into a table (a process known as a scalar insert) or updating/deleting rows identified by a value of a primary key on the target table.
The Assert operator is automatically included in the query plans in the second phase if SQL Server needs to perform constraint validation. SQL Server validates the CHECK constraints for INSERTs and UPDATEs by evaluating a usually inexpensive scalar expression on each affected row and column. Foreign key constraints are enforced on INSERTs and UPDATEs to the table containing the foreign key constraint, and they're enforced on UPDATEs and DELETEs to the table containing the referenced key. The related table that is not the target of the IUD operation is scanned to verify the constraint; therefore, data access is involved. Declaring a primary key automatically creates a unique index on the key columns, but this is not the case for a foreign key. UPDATEs and DELETEs of referenced keys must access the foreign key table for each updated or deleted primary key value, either to validate nonexistence of the removed key or to propagate the change if it is a cascading referential integrity constraint. Therefore, you should ensure there is an index on the foreign key if you plan to perform UPDATEs affecting the key values or DELETEs from the primary table.
In addition to performing the IUD operation on the clustered index or heap, processing of the INSERT and DELETE queries also maintains all nonclustered indexes, and the UPDATE queries maintain indexes containing the modified columns. Because nonclustered indexes include the clustered index and partitioning keys to allow efficient access to the table row, updating columns that participate in the clustered index or in the partitioning key is expensive because it requires maintenance of all indexes. Updating the partitioning key might also cause rows to move between partitions. Therefore, when you have a choice, choose clustering and partitioning keys that you don't plan to update.
Note
|
SQL Server 2005 restricts the partitioning keys to a single column; therefore, "partitioning key" and "partitioning column" are synonyms. |
In general, the performance of IUD statements is closely tied to the number of maintained indexes that include the target columns, because those must all be modified. Performing single-row INSERT and DELETE operations to an index requires a single index-tree traversal. SQL Server implements update to an index or partitioning key as a DELETE followed by an INSERT therefore, it is roughly twice as expensive as a nonkey UPDATE.
Others
You can use DBCC FREEPROCCACHE to clear up the procedure cache to ensure the compilation will take place afterwards. However, you should be careful using the DBCC FREEPROCCACHE on production servers because it will delete the contents of the procedure cache, and all statements and stored procedures will have to be compiled anew
The DOP is 0 (which means serial plan), which shows the actual degree of parallelism (or DOP, which is the number of concurrent threads working on the single query) of the execution
Observe that in the preceding example I used SET SHOWPLAN_TEXT OFF following the query. This is because SET SHOWPLAN_TEXT ON is not only causing the query plan to show up, it is also turning off query execution for the connection. Query execution will stay turned off until SQL Server executes SET SHOWPLAN_TEXT OFF on the same connection
When examining the plan for a particular query, a good way to find potential problems is to find the biggest discrepancies between the optimizer's estimates and the real number of executes and returned rows. Here we must be careful because the optimizer's estimate in the column EstimateRows is per estimated execution, while the Rows in the showplan output mentioned previously is the cumulative number of rows returned by the operator from all its executions. Therefore, to assess the optimizer's discrepancy we must multiply the EstimateRows by EstimateExecutions and compare the result with the actual number of all rows returned in the Rows column of the SET STATISTICS PROFILE output.
After the query optimizer produces the plan for the batch or stored procedure, the plan is placed in the procedure cache. You can examine the procedure cache using several dynamic management views (DMV) and functions (DMF), DBCC PROCCACHE, and the deprecated catalog view sys.syscacheobjects. I will show how you can access a showplan in XML format for the queries currently in the procedure cache.
Inside TSQL Querying - Chapter 2. Physical Query Processing的更多相关文章
- Inside TSQL Querying - Chapter 1. Logical Query Processing
Logical Query Processing Phases Summary (8) SELECT (9) DISTINCT (11) <TOP_specification> <s ...
- Inside TSQL Querying - Chapter 3. Query Tuning
Tuning Methodology When dealing with performance problems, database professionals tend to focus on t ...
- Sentry 监控 - Snuba 数据中台架构(Query Processing 简介)
系列 1 分钟快速使用 Docker 上手最新版 Sentry-CLI - 创建版本 快速使用 Docker 上手 Sentry-CLI - 30 秒上手 Source Maps Sentry For ...
- Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记之查询优化
一. 自顶向下优化方法论 1. 分析实例级别的等待 在实例级找出什么类型的等待占用大部分的时间,通过sys.dm_os_wait_stats select wait_type, --等待类型 wait ...
- Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记1
(5)SELECT (5-2) DISTINCT (5-3)TOP(<top_specifications>) (5-1)<select_list> (1)FRO ...
- MySQL Crash Course #18# Chapter 26. Managing Transaction Processing
InnoDB 支持 transaction ,MyISAM 不支持. 索引: Changing the Default Commit Behavior SAVEPOINT 与 ROLLBACK TO ...
- CMU Database Systems - Query Processing
Query Model Query处理有三种方式, 首先是Iterator model,这是最基本的model,又称为volcano,pipeline模式 他是top-down的模式,通过next函数 ...
- adaptive query processing
http://www.cs.umd.edu/~amol/talks/VLDB07-AQP-Tutorial.pdf https://www.cis.upenn.edu/~zives/research/ ...
- 动态Pivot(1)
原文 http://book.51cto.com/art/200710/58874.htm 7.7 动态Pivot 作为另外一个练习,假设你要编写一个存储过程,它生成动态Pivot查询.这个存储过程 ...
随机推荐
- golang AES/ECB/PKCS5 加密解密 url-safe-base64
因为项目的需要用到golang的一种特殊的加密解密算法AES/ECB/PKCS5,但是算法并没有包含在标准库中,经过多次失败的尝试,终于解码成功,特此分享: /* 描述 : golang AES/EC ...
- linux php
ubuntu php -v 查看php版本号 查看目录 cd / ls apache: 如果采用RPM包安装,安装路径应在/etc/httpd目录下 apache配置文件:/etc/httpd/con ...
- java三大框架学习总结(1)
企业里并不一定就会用这三种框架,关键是要你能懂得面向对象的原理,以及对服务器客户端请求响应方式的理解,再加上你对缓存的利用,这才能成为真正的高手,框架就好比是一把武器,它最多是能帮你更好的杀敌,而如果 ...
- CentOS 6.6 yum 搭建LAMP环境
CentOS 查看操作系统版本 [root@oa ~]# cat /etc/redhat-releaseCentOS release 6.6 (Final) 参考linux centos yum安装L ...
- selenium 基本了解
Selenium的界面 白色:还未执行 浅青色:动作成功 深青色:判断成功 浅粉红色:判断失败,但不影响测试案例的运行 深粉红色:判断失败,且测试案例无法正常运行 Command 存在的命令 Acti ...
- Windows网络共享权限设置
文件共享权限有两种权限设置,只要理解这两种权限设置就可以在域控灵活运用. 第一种是网络共享权限 共享权限是控制用户通过网络访问共享文件夹的手段,共享权限仅当用户通过网络访问时才有效,本地用户不受此权限 ...
- 第一个Json.Net Demo
//序列化 private void btnShow_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); DataC ...
- MongoDB操作
创建.删除数据库 格式 use DATABASE_NAME 如果不存在,则创建,否则直接切换到该数据库 显示当前所在的数据库 db 显示所有数据库 show dbs 删除数据库 db.dropData ...
- canvas保存为data:image扩展功能的实现
[已知]canvas提供了toDataURL的接口,可以方便的将canvas画布转化成base64编码的image.目前支持的最好的是png格式,jpeg格式的现代浏览器基本也支持,但是支持的不是很好 ...
- Mysql 5.7.7
1.安装Mysql(需要管理员权限) 2.启动Mysql 3.连接Mysql Mysql刚安装成功后可输入 mysql -u root -p ,然后回车,提示输入密码,由于是第一次连接,不用输入密码也 ...