SQL Server 存储中间结果集

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介:

在SQL Server中执行查询时,有一些操作会产生中间结果集,例如:排序操作,Hash Join和Hash Aggregate操作产生的Hash Table,游标等,SQL Server查询优化器使用 Worktable 和 Workfile 存储中间结果集,对中间结果集执行后续操作,以响应用户的请求。WorkTable和WorkFile的内部结构没有文档说明,如其名字:WorkTable类似关系表(行和列)结构,WorkFile类似文件结构。

一,Spool 操作符

Spool的中文解释是假脱机,是指两个设备处于联机状态,只不过都在后台运行。

假脱机的原理,通俗的说,是在两个设备之间进行数据交换,一个设备传输数据的速度快,一个设备传输数据的速度慢。为了充分利用高速设备,在需要交换数据时,低速设备将数据输入到缓冲区,同时,高速设备无需等待低速设备,而是径直去处理其他Task,等低速设备将全部数据输入到缓存之后,高速设备再去缓冲区读取数据,然后快速处理,这样高速设备就不会闲置,提高设备的利用率。Spool 为了协调两个设备,需要用到一个缓冲区,低速设备向缓冲区中写数据,高速设备从缓冲区中读取数据。所以,假脱机技术的原理,是低速设备在后台利用缓冲区转存数据,高速设备从缓冲区获取数据,执行数据处理。

在 SQL Server的执行计划中,Spool 是逻辑运算符,分为Eager Spool 和 Lazy Spool:

  • Eager Spool 逻辑运算符:一次性将所有数据输入到临时的Spool中
  • Lazy Spool 逻辑运算符:逐批将数据输入到临时的Spool中

如果执行计划出现这两个操作符,表明 SQL Server 需要将中间结果集保存到临时存储区,在SQL Server中,临时存储区是指Worktable 或 Workfile, 用于存储临时结果集:

  • WorkFile: 用于存储Hash Join 和 Hash 聚合的临时结果集,小的临时结果集存储在Memory中;
  • WorkTable: 用于存储 Query Spool(table spool, index spool, and so on),Lob 变量,XML变量和游标的临时结果集;
  • Spill to tempdb:WorkFile和WorkTable都创建在内存中,如果临时结果集太大,分配的Memory容纳不了,那么,临时结果集会被保存到tempdb file中,即:spill to disk(tempdb files);
  • 查询性能降低:一般来说,SQL Server在执行计划中使用WorkFile 或 WorkTable,表明查询语句的性能较低

引用MSDN:

"Work files could be used to store temporary results for hash joins and hash aggregates. The returned value should be less than 20. Tempdb work files are used in processing hash operations when the amount of data being processed is too large to fit into the available memory." Possible problems: High values can indicate thrash in the tempdb file as well as poorly coded queries.

"Work tables could be used to store temporary results for query spool, lob variables, XML variables, and cursors. The returned value should be less than 20. Worktables are used for queries that use various spools (table spool, index spool, and so on)." Possible problems: High values could cause general slowdown.

二,查看Workfile 和 Worktable

查询优化器创建Workfile 和 Worktable 的目的是为了存储临时结果集,在执行查询时,设置 “Set Statistics IO On” ,能够看到查询语句创建的 Workfile 和 Worktable,以及临时存储区的IO信息,如图:

一般情况下,如果临时存储对象(Workfile 和 Worktable)被创建,说明查询的中间结果集太大,服务器的内存资源将被耗尽,导致SQL Server 必须使用Disk存储中间结果集,才能使查询操作继续执行,但是,可用的内存耗尽和额外的IO开销,都会降低SQL Server的的查询性能。

SQL Server在内存中创建Workfile 和 Worktable,在中间结果集较小时,中间结果集保存在内存中;如果结果集过大,保存在内存将增加内存压力,SQL Server将中间结果集转存到tempdb的数据文件中,即,从内存写入到Disk。在需要中间结果集时,从Disk File读取到内存中,产生额外的Disk IO,降低SQL Server的查询性能。

1,使用Worktable

关系引擎在执行 order by、group by或者union操作时,需要创建WorkTable,用于保存中间结果集。例如,在执行Order by 操作时,如果没有在排序字段上创建index,那么SQL Server查询优化器会在tempdb中创建一个WorkTable来缓存中间查询结果集,并对中间结果集进行排序。在系统需要时,自动创建创建在tempdb中创建WorkTable;当系统不再需要该WorkTable时,会自动从tempdb中删除。

优化方法:为了减少WorkTable的使用,应该优化SQL语句,减少不必要的group by,order by或Union操作,在select 子句中剔去不必要的column,使返回的结果集尽可能的小。

引用MSDN:

The relational engine may need to build a worktable to perform a logical operation specified in an SQL statement. Worktables are internal tables that are used to hold intermediate results. Worktables are generated for certain GROUP BY, ORDER BY, or UNION queries. For example, if an ORDER BY clause references columns that are not covered by any indexes, the relational engine may need to generate a worktable to sort the result set into the order requested. Worktables are also sometimes used as spools that temporarily hold the result of executing a part of a query plan. Worktables are built in tempdb and are dropped automatically when they are no longer needed.

2,使用WorkFile

在SQL Server执行Hash Join 或 Hash聚合操作,使用WorkFiles来存储临时结果集。

Hash Join 能够有效处理大量,未排序,未索引的输入;Hash Join在处理复杂查询返回的中间结果集时非常有用,是因为:

  • Intermediate results are not indexed (unless explicitly saved to disk and then indexed) and often are not suitably sorted for the next operation in the query plan.

  • Query optimizers estimate only intermediate result sizes. Because estimates can be very inaccurate for complex queries, algorithms to process intermediate results not only must be efficient, but also must degrade gracefully if an intermediate result turns out to be much larger than anticipated.

优化方法:为了减少WorkFile的使用,在和大表进行Join查询时,尽可能地使用Index Column,避免对无序数据进行Hash Join或Hash 聚合操作。

参考文档:

Operator of the Week – Spools, Eager Spool

Understanding Hash Joins

Tempdb usage: Workfiles

优化临时表使用,SQL语句性能提升100倍

SET STATISTICS IO- worktable/workfile

Beware misleading data from SET STATISTICS IO

Simple Query tuning with STATISTICS IO and Execution plans

Advanced Query Tuning Concepts

SQL Server, Access Methods Object

workfiles created/sec spiking out of control

SQLSERVER中的假脱机

作者悦光阴
本文版权归作者和博客园所有,欢迎转载,但未经作者同意,必须保留此段声明,且在文章页面醒目位置显示原文连接,否则保留追究法律责任的权利。
分类: 数据库管理







本文转自悦光阴博客园博客,原文链接:http://www.cnblogs.com/ljhdo/p/5479032.html,如需转载请自行联系原作者
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
15天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
52 10
|
25天前
|
SQL
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
17 0
|
15天前
|
SQL 算法 数据库
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
88 6
|
2天前
|
SQL 数据管理 关系型数据库
如何在 Windows 上安装 SQL Server,保姆级教程来了!
在Windows上安装SQL Server的详细步骤包括:从官方下载安装程序(如Developer版),选择自定义安装,指定安装位置(非C盘),接受许可条款,选中Microsoft更新,忽略警告,取消“适用于SQL Server的Azure”选项,仅勾选必要功能(不包括Analysis Services)并更改实例目录至非C盘,选择默认实例和Windows身份验证模式,添加当前用户,最后点击安装并等待完成。安装成功后关闭窗口。后续文章将介绍SSMS的安装。
5 0
|
10天前
|
SQL 安全 网络安全
IDEA DataGrip连接sqlserver 提示驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的解决方法
IDEA DataGrip连接sqlserver 提示驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接的解决方法
22 0
|
15天前
|
SQL 存储 数据挖掘
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
服务器数据恢复环境: 一台安装windows server操作系统的服务器。一组由8块硬盘组建的RAID5,划分LUN供这台服务器使用。 在windows服务器内装有SqlServer数据库。存储空间LUN划分了两个逻辑分区。 服务器故障&初检: 由于未知原因,Sql Server数据库文件丢失,丢失数据涉及到3个库,表的数量有3000左右。数据库文件丢失原因还没有查清楚,也不能确定数据存储位置。 数据库文件丢失后服务器仍处于开机状态,所幸没有大量数据写入。 将raid5中所有磁盘编号后取出,经过硬件工程师检测,没有发现明显的硬件故障。以只读方式将所有磁盘进行扇区级的全盘镜像,镜像完成后将所
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
|
19天前
|
SQL 数据安全/隐私保护
SQL Server 2016安装教程
SQL Server 2016安装教程
21 1
|
19天前
|
SQL 安全 Java
SQL server 2017安装教程
SQL server 2017安装教程
17 1
|
1月前
|
SQL 存储 Python
Microsoft SQL Server 编写汉字转拼音函数
Microsoft SQL Server 编写汉字转拼音函数
|
1月前
|
SQL 存储 数据库
数据安全无忧,SQL Server 2014数据库定时备份解密
数据安全无忧,SQL Server 2014数据库定时备份解密