当前位置:Gxlcms > 数据库问题 > SQL Server的Spool, Worktable 和 Workfile

SQL Server的Spool, Worktable 和 Workfile

时间:2021-07-01 10:21:17 帮助过:2人阅读

一,Spool

Spool的中文解释是假脱机,脱机是offline的翻译。假脱机的同义词,是不是真联机?可以这样理解,因为两个设备确实是联机状态,只不过是后台运行。

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

 

分析假脱机原理,可以看到,spool额外用到一个缓冲区和两次IO(写入和读取)。

二,SQL Server的Spool

桦仔的博客写的非常详细《SQLSERVER中的假脱机》,赞一个!

 

SQL Server的Spool 逻辑运算分为Eager Spool 和 lazy Spool,当出现这两个运算符时,表明SQL Server 需要将计算的中间结果集保存到临时存储区,这个临时存储区创建在tempdb中,临时存储区是Worktable 或 Workfile,存储的内容是不同的。

 

work files could be used to store temporary results for hash joins and hash aggregates.

work tables could be used to store temporary results for query spool, lob variables, XML variables, and cursors.

 

 

"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 的目的是为了临时存储中间结果集,增加系统的IO负担,可以通过“set statistics IO On”命令来查看查询语句创建的Workfile 和worktable,查看临时存储区的IO信息。

技术分享

 

推荐阅读:

SQLSERVER中的假脱机

SQL Server的Spool, Worktable 和 Workfile

标签:

人气教程排行