SQLSERVER中的假脱机 我发现网上对于假脱机的解释都非常零散,究竟假脱机是什么? 这几天在家里研究了一下,收集了很多网上的资料 假脱机是中文的翻译,而英文的名字叫做 spool 在徐老师写的《SQLSERVER企业级平台管理实践》里提到了一下假脱机 在SQLSERVER
我发现网上对于假脱机的解释都非常零散,究竟假脱机是什么?
这几天在家里研究了一下,收集了很多网上的资料
假脱机是中文的翻译,而英文的名字叫做 spool
在徐老师写的《SQLSERVER企业级平台管理实践》里提到了一下假脱机
在SQLSERVER I/O问题的那一节
在性能监视器里,有一个计数器“worktables/sec” :
每秒创建的工作表数。例如,工作表可用于存储查询假脱机(query spool),LOB变量,XML变量,表变量,游标的临时结果
在《剖析SQLServer执行计划》里也提到了假脱机
(13) 有时查询优化器需要在tempdb数据库中建立临时工作表。如果是这样的话
就意味着图形执行计划中有标识成Index Spool, Row Count Spool或者Table Spool的图标。
任何时候,使用到工作表一般都会防碍到性能,因为需要额外的I/O开销来维护这个工作表。
之前本人也写过一篇文章:对于索引假脱机的一点理解
写这篇文章的时候当时还是对假脱机一知半解
假脱机在MSDN中的执行计划中的逻辑运算符和物理运算符中提到了几个假脱机相关的运算符(详见本文最后面)
Eager Spool
Lazy Spool
Index Spool (有时候也叫 Nonclustered Index Spool)
Row Count Spool
Spool
Table Spool
Window Spool
Spool, Table Spool, Index Spool, Window Spool 和 Row Count Spool是物理运算符
Eager Spool 和 Lazy Spool是逻辑运算符
这些运算符描述了假脱机是如何工作的,在这里你需要非常清楚逻辑运算符和物理运算符的区别
MSDN中的解释:
逻辑运算符:逻辑运算符描述了用于处理语句的关系代数操作。 换言之,逻辑运算符从概念上描述了需要执行哪些操作。
物理运算符:物理运算符实施由逻辑运算符描述的操作。 每个物理运算符都是一个执行某项操作的对象或例程。
例如,某些物理运算符可访问表、索引或视图中的列或行。 其他物理运算符执行其他操作,如计算、聚合、数据完整性检查或联接。
物理运算符具有与其关联的开销。
注意:窗口假脱机是没有Eager Spool和Lazy Spool之分的,因为他既是逻辑运算符也是物理运算符!!
简单来讲SQLSERVER做某项操作由物理运算符来做,而具体怎样做就由逻辑运算符来决定
打个比方:小明在佛山,想去广州,小明可以选择开汽车去广州,踩自行车去广州,骑摩托车去广州(相当于做某项操作)
小明可以根据当时的路况:
(1)踩自行车:如果道路比较拥堵,踩自行车不用怕,最多的车也能过,他可以选择使劲的踩(Eager Spool)或者慢慢踩(Lazy Spool)
(2)开汽车:如果道路比较畅通,他可以选择开快一点(Eager Spool)或者开慢一点(Lazy Spool)
(3)骑摩托车:如果道路比较拥堵,他可以选择抄小路,然后开快一点(Eager Spool)或者开慢一点(Lazy Spool)
不知道这个比喻大家明白没有,不过本人也找不到更好的比喻~
在图形执行计划中,你会发现Table Spool 有时候会带有 Eager Spool ,有时候有会带有 Lazy Spool
因为Table Spool是物理运算符,Eager Spool和Eager Spool 是逻辑运算符
Table Spool(表假脱机)
SQL脚本如下:
表假脱机 Eager Spool
<span> 1</span> <span>--</span><span>--表假脱机 Eager Spool</span> <span> 2</span> <span>USE</span> <span>[</span><span>Spool</span><span>]</span> <span> 3</span> <span>GO</span> <span> 4</span> <span>CREATE</span> <span>TABLE</span> Sales (EmpId <span>INT</span>, Yr <span>INT</span>, Sales <span>MONEY</span><span>) </span><span> 5</span> <span>INSERT</span> Sales <span>VALUES</span>(<span>1</span>, <span>2005</span>, <span>12000</span><span>) </span><span> 6</span> <span>INSERT</span> Sales <span>VALUES</span>(<span>1</span>, <span>2006</span>, <span>18000</span><span>) </span><span> 7</span> <span>INSERT</span> Sales <span>VALUES</span>(<span>1</span>, <span>2007</span>, <span>25000</span><span>) </span><span> 8</span> <span>INSERT</span> Sales <span>VALUES</span>(<span>2</span>, <span>2005</span>, <span>15000</span><span>) </span><span> 9</span> <span>INSERT</span> Sales <span>VALUES</span>(<span>2</span>, <span>2006</span>, <span>6000</span><span>) </span><span>10</span> <span>INSERT</span> Sales <span>VALUES</span>(<span>3</span>, <span>2006</span>, <span>20000</span><span>) </span><span>11</span> <span>INSERT</span> Sales <span>VALUES</span>(<span>3</span>, <span>2007</span>, <span>24000</span><span>) </span><span>12</span> <span>13</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Sales</span><span>]</span> <span>14</span> <span>15</span> <span>16</span> <span>SELECT</span> EmpId, Yr, <span>SUM</span>(Sales) <span>AS</span><span> Sales </span><span>17</span> <span>FROM</span><span> Sales </span><span>18</span> <span>GROUP</span> <span>BY</span> EmpId, Yr <span>WITH</span> CUBE
例子出处:http://www.sqlskills.com/blogs/conor/grouping-sets-rollups-and-cubes-oh-my/
In this case, it writes the data to a temporary spool, sorts the output of that
and then re-reads that spool in the second branch.
表假脱机 Lazy Spool
<span>1</span> <span>--</span><span>表假脱机 Lazy Spool</span> <span>2</span> <span>USE</span> <span>[</span><span>AdventureWorks</span><span>]</span> <span>3</span> <span>GO</span> <span>4</span> <span>SELECT</span> <span>*</span>,<span>COUNT</span>(<span>*</span>) <span>OVER</span><span>() </span><span>5</span> <span>from</span> production.<span>[</span><span>Product</span><span>]</span> <span>AS</span><span> p </span><span>6</span> <span>JOIN</span> production.<span>[</span><span>ProductSubcategory</span><span>]</span> <span>AS</span><span> s </span><span>7</span> <span>ON</span> s.<span>[</span><span>ProductCategoryID</span><span>]</span><span>=</span>p.<span>[</span><span>ProductSubcategoryID</span><span>]</span>
例子出处:http://sqlblog.com/blogs/rob_farley/archive/2013/06/11/spooling-in-sql-execution-plans.aspx
Row Count Spool(行计数假脱机)
SQL脚本如下:
<span> 1</span> <span>--</span><span>行计数假脱机</span> <span> 2</span> <span>USE</span> <span>[</span><span>Spool</span><span>]</span> <span> 3</span> <span>GO</span> <span> 4</span> <span>--</span><span>建表</span> <span> 5</span> <span>CREATE</span> <span>TABLE</span> tb1(ID <span>int</span><span>) </span><span> 6</span> <span>GO</span> <span> 7</span> <span>CREATE</span> <span>TABLE</span> tb2(ID <span>int</span><span>) </span><span> 8</span> <span>GO</span> <span> 9</span> <span>10</span> <span>--</span><span>插入测试数据</span> <span>11</span> <span>DECLARE</span> <span>@i</span> <span>INT</span> <span>12</span> <span>SET</span> <span>@i</span><span>=</span> <span>500</span> <span>13</span> <span>WHILE</span> <span>@i</span> <span>></span> <span>0</span> <span>14</span> <span>begin</span> <span>15</span> <span>INSERT</span> <span>INTO</span><span> dbo.tb1 </span><span>16</span> <span>VALUES</span> ( <span>@i</span> <span>17</span> <span>) </span><span>18</span> <span>SET</span> <span>@i</span> <span>=</span> <span>@i</span> <span>-</span><span>1</span> <span>19</span> <span>end</span> <span>20</span> <span>GO</span> <span>21</span> <span>22</span> <span>DECLARE</span> <span>@i</span> <span>INT</span> <span>23</span> <span>SET</span> <span>@i</span><span>=</span> <span>500</span> <span>24</span> <span>WHILE</span> <span>@i</span> <span>></span> <span>0</span> <span>25</span> <span>begin</span> <span>26</span> <span>INSERT</span> <span>INTO</span><span> dbo.tb2 </span><span>27</span> <span>VALUES</span> ( <span>@i</span> <span>28</span> <span>) </span><span>29</span> <span>SET</span> <span>@i</span> <span>=</span> <span>@i</span> <span>-</span><span>1</span> <span>30</span> <span>end</span> <span>31</span> <span>32</span> <span>33</span> <span>--</span><span>行计数假脱机</span> <span>34</span> <span>SELECT</span> <span>*</span> <span>FROM</span> tb1 <span>WHERE</span> id <span>NOT</span> <span>IN</span>(<span>SELECT</span> id <span>FROM</span> tb2)
例子出处:http://niutuku.com/tech/MsSql/238716.shtml
Index Spool (索引假脱机)
Lazy Spool
SQL脚本如下:
<span> 1</span> <span>--</span><span>索引假脱机(Index Spool)</span> <span> 2</span> <span>USE</span> <span>[</span><span>Spool</span><span>]</span> <span> 3</span> <span>GO</span> <span> 4</span> <span>--</span><span>建表</span> <span> 5</span> <span>create</span> <span>table</span> tb(aa <span>int</span>,bb <span>char</span>(<span>1</span><span>)) </span><span> 6</span> <span>GO</span> <span> 7</span> <span> 8</span> <span>--</span><span>插入测试数据</span> <span> 9</span> <span>insert</span> tb <span>values</span>(<span>1</span>,<span>'</span><span>A</span><span>'</span><span>) </span><span>10</span> <span>insert</span> tb <span>values</span>(<span>1</span>,<span>'</span><span>B</span><span>'</span><span>) </span><span>11</span> <span>insert</span> tb <span>values</span>(<span>1</span>,<span>'</span><span>C</span><span>'</span><span>) </span><span>12</span> <span>insert</span> tb <span>values</span>(<span>1</span>,<span>'</span><span>D</span><span>'</span><span>) </span><span>13</span> <span>14</span> <span>insert</span> tb <span>values</span>(<span>2</span>,<span>'</span><span>E</span><span>'</span><span>) </span><span>15</span> <span>insert</span> tb <span>values</span>(<span>2</span>,<span>'</span><span>F</span><span>'</span><span>) </span><span>16</span> <span>insert</span> tb <span>values</span>(<span>2</span>,<span>'</span><span>G</span><span>'</span><span>) </span><span>17</span> <span>insert</span> tb <span>values</span>(<span>2</span>,<span>'</span><span>H</span><span>'</span><span>) </span><span>18</span> <span>19</span> <span>insert</span> tb <span>values</span>(<span>3</span>,<span>'</span><span>I</span><span>'</span><span>) </span><span>20</span> <span>insert</span> tb <span>values</span>(<span>3</span>,<span>'</span><span>J</span><span>'</span><span>) </span><span>21</span> <span>insert</span> tb <span>values</span>(<span>3</span>,<span>'</span><span>K</span><span>'</span><span>) </span><span>22</span> <span>insert</span> tb <span>values</span>(<span>3</span>,<span>'</span><span>L</span><span>'</span><span>) </span><span>23</span> <span>24</span> <span>--</span><span>查询数据</span> <span>25</span> <span>SELECT</span> <span>*</span> <span>26</span> <span>FROM</span><span> tb a </span><span>27</span> <span>WHERE</span> bb <span>=</span> ( <span>SELECT</span> <span>TOP</span> <span>1</span> <span>28</span> <span> bb </span><span>29</span> <span>FROM</span><span> tb </span><span>30</span> <span>WHERE</span> aa <span>=</span><span> a.aa </span><span>31</span> <span>ORDER</span> <span>BY</span> <span>NEWID</span><span>() </span><span>32</span> )
例子出处:http://www.cnblogs.com/lyhabc/archive/2013/04/19/3029840.html
Window Spool (窗口假脱机)
Window Spool 这个执行计划和OVER() 开窗函数息息相关,因为只有OVER()函数才会使用到Window Spool 这个执行计划
http://msdn.microsoft.com/zh-cn/library/ms189461.aspx
大家可以看一下MSDN中对OVER()开窗函数里ROWS选项和RANGE选项的解释
ROWS | RANGE
通过指定分区中的起点和终点,进一步限制分区中的行数。 这是通过按照逻辑关联或物理关联对当前行指定某一范围的行实现的。物理关联通过使用 ROWS 子句实现。
ROWS 子句通过指定当前行之前或之后的固定数目的行,限制分区中的行数。
此外,RANGE 子句通过指定针对当前行中的值的某一范围的值,从逻辑上限制分区中的行数。
基于 ORDER BY 子句中的顺序对之前和之后的行进行定义。
窗口框架“RANGE … CURRENT ROW …”包括在 ORDER BY 表达式中与当前行具有相同值的所有行。
例如,ROWS BETWEEN 2 PRECEDING AND CURRENT ROW 意味着该函数对其操作的行的窗口在大小上是 3 行,以当前行之前(包括当前行)的 2 行开头。
SQL脚本如下:
<span> 1</span> <span>use</span><span> master </span><span> 2</span> <span>GO</span> <span> 3</span> <span> 4</span> <span>--</span><span>range</span> <span> 5</span> <span>select</span> <span>count</span>(<span>*</span>) <span>over</span> (<span>order</span> <span>by</span> id RANGE <span>between</span> <span>current</span> row <span>and</span><span> unbounded following) </span><span> 6</span> <span>from</span><span> sysobjects </span><span> 7</span> <span>order</span> <span>by</span><span> id </span><span> 8</span> <span> 9</span> <span>--</span><span>rows</span> <span>10</span> <span>select</span> <span>count</span>(<span>*</span>) <span>over</span> (<span>order</span> <span>by</span> type ROWS <span>current</span><span> row ) </span><span>11</span> <span>from</span><span> sysobjects </span><span>12</span> <span>order</span> <span>by</span> id
例子出处:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=182542
对上面这些运算符的解释:
假脱机运算符会取出表中的一部分的数据集,将他们存放在tempdb数据库里的临时结构里
这个临时结构一般就是堆表或者非聚集索引,但是有一个物理运算符例外,临时结构是不存放数据的,
他只存放假脱机里保存的数据的行数,而这个物理运算符就是Row Count spool
Index Spool:索引假脱机只有非聚集索引假脱机,没有聚集索引假脱机,结合我以前写的两篇文章,解释一下原因
SQLSERVER聚集索引与非聚集索引的再次研究(上)
SQLSERVER聚集索引与非聚集索引的再次研究(下)
SQLSERVER当遇到复杂的查询的时候,需要把部分结果集放到tempdb数据库里的非聚集索引页里(说白了就是在tempdb数据库里建立
表的非聚集索引)以加快查找的速度的时候就会用到索引假脱机
例如上面的例子,SQL语句用到了子查询(tb表),SQLSERVER需要把子查询里的结果集(tb表)进行排序然后将结果集放进去
非聚集索引里(对tb表建立非聚集索引),
然后用非聚集索引里的数据和主表(tb a)里的数据进行联接,并输出结果
为什麽不用聚集索引?
SQLSERVER聚集索引与非聚集索引的再次研究(上/下)里说到,非聚集索引和堆表是没有连接在一起的,非聚集索引页面只有指针
指向堆表的数据页,而聚集索引的叶子节点就是数据页,索引页和数据页连接在一起,如果建立聚集索引,就需要将表(tb表)中的数据
放入到tempdb数据库里,这样开销就会很大
或者用下面两张图来描述可能会清楚一点,关键还是要读懂 SQLSERVER聚集索引与非聚集索引的再次研究(上/下)
Table Spool:把表中的数据放进tempdb数据库里
为什麽第一个查询会用到Table Spool?因为CUBE这个数据汇总关键字会将表中的数据进行汇总,汇总的过程比较复杂
把表中的数据放进去tempdb数据库里的工作表(worktable、临时表、堆表)里进行复杂的汇总计算是比较好的
他避免了阻塞,以防止长期锁住表中的数据
关于CUBE关键字可以看一下我这篇文章:SQLSERVER中的ALL、PERCENT、CUBE关键字、ROLLUP关键字和GROUPING函数
Row Count Spool:存放中间结果/表的数据的行数,上面的例子里用于计算表中的数据行数并保存在tempdb数据库的
Row Count Spool里,为后面两表联接选用执行计划提供选择依据
Eager Spool逻辑运算符:一次性将所有数据放入到Spool里
Lazy Spool逻辑运算符:逐次逐次地将数据放入Spool里
在上面的例子里
Tabel Spool Eager Spool
SQLSERVER使用Eager Spool一次性将Sales 表中的数据存放到tempdb数据库的工作表里面,方便快速统计
Row Count Spool
SQLSERVER使用计数器每次读取到一行就加1,这样一次一次地统计表中的行数(这里只是比喻,SQLSERVER内部可能并不是这样统计!)
Window Spools:根据MSDN中的定义,OVER 子句定义查询结果集内的窗口或用户指定的行集。 然后,开窗函数将计算窗口中每一行的值
SQLSERVER将窗口中的结果集放入Spool里,以加快后续操作的速度
对于单独一个窗口来讲:单独一个窗口属于Eager Spool(一次性将结果集放进去窗口里)
对于表中的窗口来讲:属于Lazy Spool ,因为每个窗口把数据存放进去窗口里的速度/顺序不是一致的,逐次逐次地将数据存放进去每个窗口
为什麽需要假脱机?
主要有两个原因:
1:数据需要再次被调用
2:使假脱机数据与源数据保持隔离
第二个原因很容易理解,就像第一个例子中的Tabel Spool那样,需要把表数据放进Tabel Spool里,以方便进行数据汇总,
而不影响原表数据
第一个原因可以再举一个例子
公用表表达式(CTE)
<span> 1</span> <span>USE</span> <span>[</span><span>AdventureWorks</span><span>]</span> <span> 2</span> <span>GO</span> <span> 3</span> <span>WITH</span> managers <span>AS</span><span>( </span><span> 4</span> <span>SELECT</span> <span>[</span><span>EmployeeID</span><span>]</span>,<span>[</span><span>ManagerID</span><span>]</span> <span> 5</span> <span>from</span> <span>[</span><span>HumanResources</span><span>]</span>.<span>[</span><span>Employee</span><span>]</span> <span> 6</span> <span>WHERE</span> <span>[</span><span>ManagerID</span><span>]</span> <span>IS</span> <span>NULL</span> <span> 7</span> <span>UNION</span> <span>ALL</span> <span> 8</span> <span>SELECT</span> e.<span>[</span><span>EmployeeID</span><span>]</span>,e.<span>[</span><span>ManagerID</span><span>]</span> <span> 9</span> <span>from</span> <span>[</span><span>managers</span><span>]</span><span> m </span><span>10</span> <span>JOIN</span> <span>[</span><span>HumanResources</span><span>]</span>.<span>[</span><span>Employee</span><span>]</span><span> e </span><span>11</span> <span>ON</span> e.<span>[</span><span>ManagerID</span><span>]</span><span>=</span>m.<span>[</span><span>EmployeeID</span><span>]</span> <span>12</span> <span>) </span><span>13</span> <span>14</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>managers</span><span>]</span>
索引假脱机运算符负责把数据一条一条地塞进去tempdb的非聚集索引里,并且是Lazy的,为什麽是Lazy的?
因为刚开始的时候只有一行记录,后来慢慢一条一条数据地从最右边的表假脱机里获取数据
我们还是先分析一下整个执行计划以方便理解,我们可以将整个执行计划拆解为三部分
第一部分 执行计划的右上角
<span>1</span> <span>SELECT</span> <span>[</span><span>EmployeeID</span><span>]</span>,<span>[</span><span>ManagerID</span><span>]</span> <span>2</span> <span>from</span> <span>[</span><span>HumanResources</span><span>]</span>.<span>[</span><span>Employee</span><span>]</span> <span>3</span> <span>WHERE</span> <span>[</span><span>ManagerID</span><span>]</span> <span>IS</span> <span>NULL</span>
这部分的执行计划只查找到一条记录
他把这条记录放入索引假脱机里
第二部分 UNION ALL
将第一部分的结果和第三部分的结果合并在一起
第三部分 执行计划的右下角
<span>1</span> <span>SELECT</span> e.<span>[</span><span>EmployeeID</span><span>]</span>,e.<span>[</span><span>ManagerID</span><span>]</span> <span>2</span> <span>from</span> <span>[</span><span>managers</span><span>]</span><span> m </span><span>3</span> <span>JOIN</span> <span>[</span><span>HumanResources</span><span>]</span>.<span>[</span><span>Employee</span><span>]</span><span> e </span><span>4</span> <span>ON</span> e.<span>[</span><span>ManagerID</span><span>]</span><span>=</span>m.<span>[</span><span>EmployeeID</span><span>]</span>
最右边的表假脱机运算符负责把表数据装载入表假脱机里,这个装载过程也是逐条数据装载的
那么,执行计划里的表假脱机和索引假脱机主要有什么用???
表假脱机主要用作公用表表达式里的递归调用
<span> 1</span> <span>WITH</span> managers <span>AS</span><span>( </span><span> 2</span> <span>SELECT</span> <span>[</span><span>EmployeeID</span><span>]</span>,<span>[</span><span>ManagerID</span><span>]</span> <span> 3</span> <span>from</span> <span>[</span><span>HumanResources</span><span>]</span>.<span>[</span><span>Employee</span><span>]</span> <span> 4</span> <span>WHERE</span> <span>[</span><span>ManagerID</span><span>]</span> <span>IS</span> <span>NULL</span> <span> 5</span> <span>UNION</span> <span>ALL</span> <span> 6</span> <span>SELECT</span> e.<span>[</span><span>EmployeeID</span><span>]</span>,e.<span>[</span><span>ManagerID</span><span>]</span> <span> 7</span> <span>from</span> <span>[</span><span>managers</span><span>]</span><span> m </span><span> 8</span> <span>JOIN</span> <span>[</span><span>HumanResources</span><span>]</span>.<span>[</span><span>Employee</span><span>]</span><span> e </span><span> 9</span> <span>ON</span> e.<span>[</span><span>ManagerID</span><span>]</span><span>=</span>m.<span>[</span><span>EmployeeID</span><span>]</span> <span>10</span> )
SELECT e.[EmployeeID],e.[ManagerID]
from [managers] m
JOIN [HumanResources].[Employee] e
ON e.[ManagerID]=m.[EmployeeID]
上面的代码是每次递归的时候都需要调用到的,所以SQLSERVER干脆把表数据放到假脱机里的,不用每次都去查找记录了
而索引假脱机是方便外部代码调用公用表表达式的时候不用每次都去计算公用表表达式的结果,直接把公用表表达式的结果
放进去索引假脱机,当SELECT * FROM managers的时候,直接到索引假脱机里取数据就可以了
<span>1</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>managers</span><span>]</span>
断定运算符在这里的作用是判断是否超过系统循环次数造成死循环,如果我们加上OPTION (MAXRECURSION 0)
断定运算符就会消失
<span> 1</span> <span>USE</span> <span>[</span><span>AdventureWorks</span><span>]</span> <span> 2</span> <span>GO</span> <span> 3</span> <span>WITH</span> managers <span>AS</span><span>( </span><span> 4</span> <span>SELECT</span> <span>[</span><span>EmployeeID</span><span>]</span>,<span>[</span><span>ManagerID</span><span>]</span> <span> 5</span> <span>from</span> <span>[</span><span>HumanResources</span><span>]</span>.<span>[</span><span>Employee</span><span>]</span> <span> 6</span> <span>WHERE</span> <span>[</span><span>ManagerID</span><span>]</span> <span>IS</span> <span>NULL</span> <span> 7</span> <span>UNION</span> <span>ALL</span> <span> 8</span> <span>SELECT</span> e.<span>[</span><span>EmployeeID</span><span>]</span>,e.<span>[</span><span>ManagerID</span><span>]</span> <span> 9</span> <span>from</span> <span>[</span><span>managers</span><span>]</span><span> m </span><span>10</span> <span>JOIN</span> <span>[</span><span>HumanResources</span><span>]</span>.<span>[</span><span>Employee</span><span>]</span><span> e </span><span>11</span> <span>ON</span> e.<span>[</span><span>ManagerID</span><span>]</span><span>=</span>m.<span>[</span><span>EmployeeID</span><span>]</span> <span>12</span> <span>) </span><span>13</span> <span>14</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>managers</span><span>]</span> OPTION (MAXRECURSION 0)
万圣节问题
网上有两篇文章介绍了这个问题
园子里的这篇文章介绍非常不深入,看了之后还是不明白
http://www.cnblogs.com/xwdreamer/archive/2012/05/28/2522404.html
simple-talk网站的文章就介绍得非常清晰
https://www.simple-talk.com/sql/learn-sql-server/operator-of-the-week---spools,-eager-spool/
在介绍之前先来做一个小实验
下面SQL脚本建立一个非聚集索引表,并且非聚集索引的第一个字段是salary 并且按salary升序排序!!!
<span> 1</span> <span>USE</span> <span>[</span><span>Spool</span><span>]</span> <span> 2</span> <span>GO</span> <span> 3</span> <span> 4</span> <span>CREATE</span> <span>TABLE</span> nct(id <span>INT</span> <span>IDENTITY</span>(<span>1</span>,<span>1</span>),NAME <span>VARCHAR</span>(<span>30</span>), salary <span>INT</span><span>); </span><span> 5</span> <span>GO</span> <span> 6</span> <span>--</span><span>建立非聚集索引 切记:非聚集索引的第一个字段是salary 并且按salary升序排序!!!</span> <span> 7</span> <span>CREATE</span> <span>INDEX</span> ix_nct <span>ON</span> nct(salary <span>ASC</span>,<span>[</span><span>ID</span><span>]</span>,<span>[</span><span>NAME</span><span>]</span><span>) </span><span> 8</span> <span>GO</span> <span> 9</span> <span>10</span> <span>--</span><span>插入数据</span> <span>11</span> <span>INSERT</span> <span>INTO</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>nct</span><span>]</span> ( <span>[</span><span>NAME</span><span>]</span>,<span>[</span><span>salary</span><span>]</span><span> ) </span><span>12</span> <span>SELECT</span> <span>'</span><span>小明</span><span>'</span>, <span>1</span> <span>UNION</span> <span>ALL</span> <span>13</span> <span>SELECT</span> <span>'</span><span>小华</span><span>'</span>, <span>2</span> <span>UNION</span> <span>ALL</span> <span>14</span> <span>SELECT</span> <span>'</span><span>小芳</span><span>'</span>, <span>3</span> <span>15</span> <span>GO</span> <span>16</span> <span>17</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>nct</span><span>]</span>
我们看一下非聚集索引页
<span> 1</span> <span>CREATE</span> <span>TABLE</span><span> DBCCResult ( </span><span> 2</span> PageFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 3</span> PagePID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 4</span> IAMFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 5</span> IAMPID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 6</span> ObjectID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 7</span> IndexID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 8</span> PartitionNumber <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 9</span> PartitionID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>10</span> iam_chain_type <span>NVARCHAR</span>(<span>200</span><span>), </span><span>11</span> PageType <span>NVARCHAR</span>(<span>200</span><span>), </span><span>12</span> IndexLevel <span>NVARCHAR</span>(<span>200</span><span>), </span><span>13</span> NextPageFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>14</span> NextPagePID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>15</span> PrevPageFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>16</span> PrevPagePID <span>NVARCHAR</span>(<span>200</span><span>) </span><span>17</span> <span>) </span><span>18</span> <span>19</span> <span>--</span><span>TRUNCATE TABLE [dbo].[DBCCResult]</span> <span>20</span> <span>21</span> <span>INSERT</span> <span>INTO</span> DBCCResult <span>EXEC</span> (<span>'</span><span>DBCC IND(Spool,nct,-1) </span><span>'</span><span>) </span><span>22</span> <span>23</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>DBCCResult</span><span>]</span> <span>ORDER</span> <span>BY</span> <span>[</span><span>PageType</span><span>]</span> <span>DESC</span> <span>24</span> <span>25</span> <span>DBCC</span> TRACEON(<span>3604</span>,<span>-</span><span>1</span><span>) </span><span>26</span> <span>GO</span> <span>27</span> <span>DBCC</span> PAGE(Spool,<span>1</span>,<span>47</span>,<span>3</span><span>) </span><span>28</span> <span>GO</span>
非聚集索引按照Salary字段升序排序
我们用SQL语句update一下小华的Salary
<span>1</span> <span>UPDATE</span> nct <span>SET</span> Salary <span>=</span> <span>4</span> <span>2</span> <span>WHERE</span> <span>[</span><span>NAME</span><span>]</span><span>=</span><span>'</span><span>小华</span><span>'</span>
这里是按照非聚集索引的Range Scan读取出结果的:SQLSERVER中的ALLOCATION SCAN和RANGE SCAN
再看一下非聚集索引页面
我们看一下update前和update后非聚集索引页面的变化
可以看到,update之后非聚集索引马上根据非聚集索引键(Salary字段)重新进行升序排序
--------------------------------------------------------------------------------------------
使用下面SQL脚本建立测试环境
<span> 1</span> <span>USE</span> <span>[</span><span>Spool</span><span>]</span> <span> 2</span> <span>GO</span> <span> 3</span> <span> 4</span> <span> 5</span> <span>--</span><span>建表</span> <span> 6</span> <span>CREATE</span> <span>TABLE</span><span> Halloween </span><span> 7</span> <span>( </span><span> 8</span> ID <span>INT</span> <span>IDENTITY</span>(<span>1</span>, <span>1</span><span>) </span><span> 9</span> <span>PRIMARY</span> <span>KEY</span><span> , </span><span>10</span> Name <span>VARCHAR</span>(<span>30</span><span>) , </span><span>11</span> Salary NUMERIC(<span>18</span>, <span>2</span><span>), </span><span>12</span> Remark <span>NVARCHAR</span>(<span>3000</span><span>) </span><span>13</span> <span>) </span><span>14</span> <span>GO</span> <span>15</span> <span>16</span> <span>--</span><span>插入数据</span> <span>17</span> <span>INSERT</span> <span>INTO</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Halloween</span><span>]</span> ( <span>[</span><span>Name</span><span>]</span>, <span>[</span><span>Salary</span><span>]</span>, <span>[</span><span>Remark</span><span>]</span><span> ) </span><span>18</span> <span>SELECT</span> <span>'</span><span>小明</span><span>'</span>,<span>1</span>,<span>replicate</span>(<span>'</span><span>a</span><span>'</span>, <span>3000</span>) <span>UNION</span> <span>ALL</span> <span>19</span> <span>SELECT</span> <span>'</span><span>小方</span><span>'</span>,<span>2</span>,<span>replicate</span>(<span>'</span><span>a</span><span>'</span>, <span>3000</span><span>) </span><span>20</span> <span>21</span> <span>22</span> <span>23</span> <span>24</span> <span>--</span><span>建立非聚集索引</span> <span>25</span> <span>CREATE</span> <span>NONCLUSTERED</span> <span>INDEX</span> ix_Halloween <span>ON</span> Halloween(Salary <span>ASC</span><span>) </span><span>26</span> <span>GO</span> <span>27</span> <span>28</span> <span>--</span><span>查询</span> <span>29</span> <span>SELECT</span> <span>*</span> <span>FROM</span><span> Halloween </span><span>30</span> <span>GO</span>
我们用下面SQL语句看一下聚集索引页面和非聚集索引页面
<span> 1</span> <span>CREATE</span> <span>TABLE</span><span> DBCCResult ( </span><span> 2</span> PageFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 3</span> PagePID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 4</span> IAMFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 5</span> IAMPID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 6</span> ObjectID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 7</span> IndexID <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 8</span> PartitionNumber <span>NVARCHAR</span>(<span>200</span><span>), </span><span> 9</span> PartitionID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>10</span> iam_chain_type <span>NVARCHAR</span>(<span>200</span><span>), </span><span>11</span> PageType <span>NVARCHAR</span>(<span>200</span><span>), </span><span>12</span> IndexLevel <span>NVARCHAR</span>(<span>200</span><span>), </span><span>13</span> NextPageFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>14</span> NextPagePID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>15</span> PrevPageFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>16</span> PrevPagePID <span>NVARCHAR</span>(<span>200</span><span>) </span><span>17</span> <span>) </span><span>18</span> <span>19</span> <span>--</span><span>TRUNCATE TABLE [dbo].[DBCCResult]</span> <span>20</span> <span>INSERT</span> <span>INTO</span> DBCCResult <span>EXEC</span> (<span>'</span><span>DBCC IND(spool,Halloween,-1) </span><span>'</span><span>) </span><span>21</span> <span>22</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>DBCCResult</span><span>]</span> <span>ORDER</span> <span>BY</span> <span>[</span><span>PageType</span><span>]</span> <span>DESC</span> <span>23</span> <span>24</span> <span>25</span> <span>DBCC</span> TRACEON(<span>3604</span>,<span>-</span><span>1</span><span>) </span><span>26</span> <span>GO</span> <span>27</span> <span>DBCC</span> PAGE(spool,<span>1</span>,<span>184</span>,<span>3</span><span>) </span><span>28</span> <span>GO</span> <span>29</span> <span>DBCC</span> PAGE(spool,<span>1</span>,<span>93</span>,<span>3</span><span>) </span><span>30</span> <span>GO</span>
聚集索引页面
非聚集索引页面
我们update一下Salary等于1的那位员工的工资
<span>1</span> <span>UPDATE</span> Halloween <span>SET</span> Salary <span>=</span> <span>2.5</span> <span>2</span> <span>FROM</span><span> Halloween </span><span>3</span> <span>WHERE</span> Salary <span>=</span><span>1</span>
再看一下聚集索引页面和非聚集索引页面
聚集索引页面
非聚集索引页面
非聚集索引马上按照非聚集索引键(Salary字段)进行重新排序
这里似乎没有什么问题,我们drop掉Halloween表,并重新建立测试环境
<span> 1</span> <span>USE</span> <span>[</span><span>Spool</span><span>]</span> <span> 2</span> <span>GO</span> <span> 3</span> <span> 4</span> <span> 5</span> <span> 6</span> <span>--</span><span>建表</span> <span> 7</span> <span>CREATE</span> <span>TABLE</span><span> Halloween </span><span> 8</span> <span>( </span><span> 9</span> ID <span>INT</span> <span>IDENTITY</span>(<span>1</span>, <span>1</span><span>) </span><span>10</span> <span>PRIMARY</span> <span>KEY</span><span> , </span><span>11</span> Name <span>VARCHAR</span>(<span>30</span><span>) , </span><span>12</span> Salary NUMERIC(<span>18</span>, <span>2</span><span>), </span><span>13</span> Remark <span>NVARCHAR</span>(<span>3000</span><span>) </span><span>14</span> <span>) </span><span>15</span> <span>GO</span> <span>16</span> <span>17</span> <span>--</span><span>插入数据</span> <span>18</span> <span>INSERT</span> <span>INTO</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>Halloween</span><span>]</span> ( <span>[</span><span>Name</span><span>]</span>, <span>[</span><span>Salary</span><span>]</span>, <span>[</span><span>Remark</span><span>]</span><span> ) </span><span>19</span> <span>SELECT</span> <span>'</span><span>小明</span><span>'</span>,<span>1</span>,<span>replicate</span>(<span>'</span><span>a</span><span>'</span>, <span>3000</span>) <span>UNION</span> <span>ALL</span> <span>20</span> <span>SELECT</span> <span>'</span><span>小方</span><span>'</span>,<span>2</span>,<span>replicate</span>(<span>'</span><span>a</span><span>'</span>, <span>3000</span><span>) </span><span>21</span> <span>22</span> <span>23</span> <span>24</span> <span>25</span> <span>--</span><span>建立非聚集索引</span> <span>26</span> <span>CREATE</span> <span>NONCLUSTERED</span> <span>INDEX</span> ix_Halloween <span>ON</span> Halloween(Salary <span>ASC</span><span>) </span><span>27</span> <span>GO</span> <span>28</span> <span>29</span> <span>--</span><span>查询</span> <span>30</span> <span>SELECT</span> <span>*</span> <span>FROM</span><span> Halloween </span><span>31</span> <span>GO</span> <span>32</span> <span>33</span> <span>34</span> <span>35</span> <span>36</span> <span>37</span> <span>CREATE</span> <span>TABLE</span><span> DBCCResult ( </span><span>38</span> PageFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>39</span> PagePID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>40</span> IAMFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>41</span> IAMPID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>42</span> ObjectID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>43</span> IndexID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>44</span> PartitionNumber <span>NVARCHAR</span>(<span>200</span><span>), </span><span>45</span> PartitionID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>46</span> iam_chain_type <span>NVARCHAR</span>(<span>200</span><span>), </span><span>47</span> PageType <span>NVARCHAR</span>(<span>200</span><span>), </span><span>48</span> IndexLevel <span>NVARCHAR</span>(<span>200</span><span>), </span><span>49</span> NextPageFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>50</span> NextPagePID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>51</span> PrevPageFID <span>NVARCHAR</span>(<span>200</span><span>), </span><span>52</span> PrevPagePID <span>NVARCHAR</span>(<span>200</span><span>) </span><span>53</span> <span>) </span><span>54</span> <span>55</span> <span>--</span><span>TRUNCATE TABLE [dbo].[DBCCResult]</span> <span>56</span> <span>INSERT</span> <span>INTO</span> DBCCResult <span>EXEC</span> (<span>'</span><span>DBCC IND(spool,Halloween,-1) </span><span>'</span><span>) </span><span>57</span> <span>58</span> <span>SELECT</span> <span>*</span> <span>FROM</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>DBCCResult</span><span>]</span> <span>ORDER</span> <span>BY</span> <span>[</span><span>PageType</span><span>]</span> <span>DESC</span> <span>59</span> <span>60</span> <span>61</span> <span>DBCC</span> TRACEON(<span>3604</span>,<span>-</span><span>1</span><span>) </span><span>62</span> <span>GO</span> <span>63</span> <span>DBCC</span> PAGE(spool,<span>1</span>,<span>184</span>,<span>3</span><span>) </span><span>64</span> <span>GO</span> <span>65</span> <span>DBCC</span> PAGE(spool,<span>1</span>,<span>93</span>,<span>3</span><span>) </span><span>66</span> <span>GO</span>
这次我们使用下面update语句,记住一定要加WITH(INDEX=ix_Halloween)
<span>1</span> <span>USE</span> <span>[</span><span>Spool</span><span>]</span> <span>2</span> <span>GO</span> <span>3</span> <span>UPDATE</span> Halloween <span>SET</span> Salary <span>=</span> <span>[</span><span>Salary</span><span>]</span><span>*</span><span>2.5</span> <span>4</span> <span>FROM</span> Halloween <span>WITH</span>(<span>INDEX</span><span>=</span><span>ix_Halloween) </span><span>5</span> <span>WHERE</span> Salary <span><span>7</span></span>
如果我们加了WITH(INDEX=ix_Halloween),SQLSERVER就会走非聚集索引查找
如果我们不加WITH(INDEX=ix_Halloween),SQLSERVER就会走聚集索引扫描
这里不讨论加不加WITH(INDEX=ix_Halloween)的问题
关键我们加WITH(INDEX=ix_Halloween)就是为了让SQLSERVER走非聚集索引
update了之后正常的结果应该是这样的
为什麽会这样?
还记得刚才我们说到了非聚集索引更新了之后马上进行排序吗?
用下面的图来表示应该会比较清楚
SQLSERVER使用Table Spool来解决万圣节问题
先将非聚集索引的数据放进去Table Spool(工作表)里,然后逐行逐行扫描工作表,这样就不会遇到非聚集索引更新后马上进行排序的问题了
使用Table Spool后就能够得到正确结果
为什麽不用Index Spool而用Table Spool?
之前我们说过Index Spool在tempdb数据库里建立临时的非聚集索引,把非聚集索引里的数据
放进去非聚集索引里,那不是会继续遇到万圣节问题???
下面这个SQL语句也是使用了Table Spool来避免万圣节问题
<span>1</span> <span>USE</span> <span>[</span><span>AdventureWorks</span><span>]</span> <span>2</span> <span>GO</span> <span>3</span> <span>UPDATE</span><span> s </span><span>4</span> <span>SET</span> <span>[</span><span>Name</span><span>]</span> <span>=</span> <span>'</span><span>Z</span><span>'</span> <span>+</span> <span>[</span><span>Name</span><span>]</span> <span>5</span> <span>FROM</span> Production.ProductSubcategory <span>AS</span> s <span>WITH</span> ( <span>INDEX</span> ( <span>[</span><span>AK_ProductSubcategory_Name</span><span>]</span><span> ) ) </span><span>6</span> <span>WHERE</span> <span>[</span><span>Name</span><span>]</span> <span>>=</span> <span>'</span><span>N</span><span>'</span>
总结一下万圣节问题:
update数据的时候,如果update的是非聚集索引的第一个字段(即非聚集索引键)的时候并且走的是非聚集索引扫描/查找
都有可能引起万圣节问题
SQLSERVER的解决方法是把非聚集索引里的数据全部移到Tabel Spool(Eager)里
防止由于更新非聚集索引的非聚集索引键而引起的非聚集索引重新排序,造成数据更新错误的问题
总结
实际上这些假脱机运算符的本质跟临时表和表变量是一样的,都是以空间换时间,以达到性能上的平衡!
文章最后面附上MSDN里的SQLSERVER所有的执行计划(逻辑运算符和物理运算符)
参考文章:
http://www.scarydba.com/2009/09/09/spools-in-execution-plans/
https://www.simple-talk.com/sql/learn-sql-server/operator-of-the-week---spools,-eager-spool/
http://sqlblog.com/blogs/rob_farley/archive/2013/06/11/spooling-in-sql-execution-plans.aspx
如有不对的地方,欢迎大家拍砖o(∩_∩)o
Showplan 逻辑运算符和物理运算符参考
操作说明
本节介绍了各个逻辑运算符和物理运算符。
图形执行计划图标 |
Showplan 运算符 |
说明 |
---|---|---|
无 |
Aggregate |
Aggregate 运算符计算包含 MIN、MAX、SUM、COUNT 或 AVG 的表达式。 Aggregate 既是一个逻辑运算符,也是一个物理运算符。 |
Arithmetic Expression |
Arithmetic Expression 运算符根据行中的现有值计算新值。 SQL Server 2012 中不使用 Arithmetic Expression, |
|
Assert |
Assert 运算符用于验证条件。 例如,验证引用完整性或确保标量子查询返回一行。 对于每个输入行,Assert 运算符都要计算执行计划的Argument 列中的表达式。 如果此表达式的值为 NULL,则通过 Assert 运算符传递该行,并且查询执行将继续。 如果此表达式的值非 Null,则将产生相应的错误。 Assert 运算符是一个物理运算符。 |
|
Assign |
Assign 运算符将表达式的值或常量分配给变量。 Assign 是一个语言元素。 |
|
无 |
Asnyc Concat |
Asnyc Concat 运算符仅用于远程查询(分布式查询)。 它有 n 个子节点和一个父节点。 通常,某些子节点是参与分布式查询的远程计算机。 Asnyc Concat 同时向所有子节点发出 open() 调用,然后将位图应用于每个子节点。 对于为 1 的每个位,Async Concat 按需向父节点发送输出行。 |
Bitmap |
SQL Server 使用 Bitmap 运算符来实现并行查询计划中的位图筛选。 在将行传递给另一个运算符(如 |