This is a note about multi-statement table valued functions (MTVF) and how their cardinality is estimated in the new CE framework.

这是有关多语句表值函数(MTVF)以及如何在新的CE框架中估计其基数的注释。

In the old CE framework the MTVF had fixed estimate of one row, in the new one the estimate is still fixed, however, now it is 100 rows. That’s the whole story. =)

在旧的CE框架中,MTVF具有固定的一行估计,在新的CE框架中,估计仍然是固定的,但是现在是100行。 这就是整个故事。 =)

For the optimizer, the MTVF (as long as the scalar UDF) is a black box, from the estimation perspective. Its often considered that inline-TVF is better, because it’s text is embedded into the query and optimized as a whole. However, I saw the examples where MTVF performed better than inline – it depends, as it used to say. In general and most of the cases inline functions are really a better choice.

对于优化器,从估计的角度来看,MTVF(只要标量UDF)是一个黑匣子。 人们通常认为内联TVF更好,因为它的文本被嵌入到查询中并进行了整体优化。 但是,我看到了MTVF的性能优于内联的示例–正如过去所说,这取决于。 通常,在大多数情况下,内联函数确实是一个更好的选择。

If you turn on the diagnostic output TF 2363 you will see that the optimizer uses the term “black-box” literally. You may also notice, that a fixed join calculator is used to estimate the join selectivity with the fixed 100 row estimate.

如果打开诊断输出TF 2363,您将看到优化器按字面意义使用术语“黑匣子”。 您可能还会注意到,使用固定的连接计算器以固定的100行估算值估算连接选择性。

Now let’s move to the short example, this is an artificial example, just to demonstrate the possible positive effect of estimating more than 1 row.

现在让我们转到简短的示例,这是一个人工示例,只是为了演示估算多于1行的可能的积极效果。

At first, let’s create a Numbers table and a simple MTVF.

首先,让我们创建一个Numbers表和一个简单的MTVF。

use AdventureWorks2012;
go------------------------------------------------
if object_id ('dbo.mtvfGetNums') is not null drop function dbo.mtvfGetNums;
if object_id('dbo.Numbers') is not null drop table dbo.Numbers;
create table dbo.Numbers(n int primary key)
insert dbo.Numbers(n) select top(1000000) rn = row_number() over(order by (select null)) from sys.columns c1,sys.columns c2,sys.columns c3;
go------------------------------------------------
create function dbo.mtvfGetNums(@max int)
returns @res table(id int identity primary key, n int)
with schemabinding
as
begininsert @res(n) select n from dbo.Numbers where n <= @max;return;
end;
go

Now, let’s run two identical synthetic queries, to demonstrate the difference (don’t look for the sense in them), the first one uses the old CE, the second one the new CE:

现在,让我们运行两个相同的综合查询,以证明它们之间的区别(不要在其中寻找意义),第一个使用旧的CE,第二个使用新的CE:

-- Old
set statistics time, xml on
declare @a int, @b int, @c int;
select @a = p.BusinessEntityID,@b = be.BusinessEntityID,@c = f.n
fromPerson.Person pcross apply dbo.mtvfGetNums((BusinessEntityID+1)%1000) fleft join Person.BusinessEntity be on f.id = be.BusinessEntityID
wherep.BusinessEntityID <= 1000
option(querytraceon 9481)
set statistics time, xml off
go
--New:
set statistics time, xml on
declare @a int, @b int, @c int;
select @a = p.BusinessEntityID,@b = be.BusinessEntityID,@c = f.n
fromPerson.Person pcross apply dbo.mtvfGetNums((BusinessEntityID+1)%1000) fleft join Person.BusinessEntity be on f.id = be.BusinessEntityID
wherep.BusinessEntityID <= 1000
set statistics time, xml off
go

On average the first query runs 50% slower (1950 ms old vs. 1250 ms new). Let’s look at the plans.

平均而言,第一个查询的运行速度降低了50%(旧查询为1950毫秒,新查询为1250毫秒)。 让我们看看计划。

In the first case, the MTVF was estimated as one row, multiplied by the number of executions 646. The actual number of rows is much higher, about 270 000. That lower estimate, lead to selecting a merge join that demands sorted inputs, and so the sort is present. The Sort demands some memory amount, this amount is based on the cardinality also, that was underestimated and so the spill at the level two occurred.

在第一种情况下,MTVF估计为一行,乘以执行次数646。实际的行数高得多,大约为270000。该较低的估计值导致选择需要排序输入的合并联接,并且因此存在排序。 排序需要一些内存量,该内存量也是基于基数的,因此被低估了,因此发生了第二级溢出。

The new CE estimated in a correct way and is closer to the reality of 270 000 rows, however, not very close, but this was enough, to choose another join type and avoid sorting and spilling.

新的CE以正确的方式估算,并且接近270 000行的实际值,但是距离不是很接近,但这足以选择另一种连接类型并避免排序和溢出。

You may invent the opposite situation when 1 row estimate wins, or you may invent the example where there is no difference. That is possible because 100 rows estimate is still a guess and you may vary the data to make the guess closer or farther to the reality.

当赢得1行估算值时,您可以发明相反的情况,也可以发明没有差异的示例。 这是可能的,因为100行估算仍然是一个猜测,您可以更改数据以使猜测更接近或更远。

模型变化 (The Model Variation)

You may use the new CE, but turn off this particular estimate of 100 rows for MTVF using the model variation, that can be enabled by TF 9488 (it is checked in the function CCardFrameworkSQL12::CardEstimateTVF internally).

您可以使用新的CE,但是可以使用TF 9488启用的模型变体来关闭MTVF的100行特定估计(可以在函数CCardFrameworkSQL12 :: CardEstimateTVF内部进行检查)。

If you run the query:

如果运行查询:

set statistics time, xml on
declare @a int, @b int, @c int;
select @a = p.BusinessEntityID,@b = be.BusinessEntityID,@c = f.n
fromPerson.Person pcross apply dbo.mtvfGetNums((BusinessEntityID+1)%1000) fleft join Person.BusinessEntity be on f.id = be.BusinessEntityID
wherep.BusinessEntityID <= 1000
option(querytraceon 9488)
set statistics time, xml off
go

You will see that the estimate now is 1 row per execution:

您将看到现在的估算值是每次执行1行:

Interesting, that even the MTVF is estimated as in the old CE framework, we have a different plan. That is because the Joins are present in our queries, and the Join estimation was also changed in many ways. However, that is a topic for another blog post.

有趣的是,即使MTVF也是按照旧的CE框架进行估算的,我们也有不同的计划。 这是因为联接存在于我们的查询中,并且联接估计也以许多方式进行了更改。 但是,这是另一篇博客文章的主题。

That’s all for that post, happy estimations! =)

这是该帖子的全部内容,估计很高兴! =)

目录 (Table of contents)

Cardinality Estimation Role in SQL Server
Cardinality Estimation Place in the Optimization Process in SQL Server
Cardinality Estimation Concepts in SQL Server
Cardinality Estimation Process in SQL Server
Cardinality Estimation Framework Version Control in SQL Server
Filtered Stats and CE Model Variation in SQL Server
Join Containment Assumption and CE Model Variation in SQL Server
Overpopulated Primary Key and CE Model Variation in SQL Server
Ascending Key and CE Model Variation in SQL Server
MTVF and CE Model Variation in SQL Server
SQL Server中的基数估计角色
基数估计在SQL Server优化过程中的位置
SQL Server中的基数估计概念
SQL Server中的基数估计过程
SQL Server中的基数估计框架版本控制
SQL Server中的筛选后的统计信息和CE模型变化
在SQL Server中加入包含假设和CE模型变化
SQL Server中人口过多的主键和CE模型的变化
SQL Server中的升序密钥和CE模型变化
SQL Server中的MTVF和CE模型变化

参考资料 (References)

  • Optimizing Your Query Plans with the SQL Server 2014 Cardinality Estimator 使用SQL Server 2014基数估计器优化查询计划
  • Query Performance and multi-statement table valued functions 查询性能和多语句表值函数
  • ALTER DATABASE (Transact-SQL) Compatibility Level ALTER DATABASE(Transact-SQL)兼容性级别

翻译自: https://www.sqlshack.com/mtvf-and-ce-model-variation/

SQL Server中的MTVF和CE模型变化相关推荐

  1. SQL Server中的筛选后的统计信息和CE模型变化

    In this blog post, we are going to view some interesting model variation, that I've found while expl ...

  2. 在SQL Server中加入包含假设和CE模型变化

    In this post we are going to talk about one of the model assumptions, that was changed in the new ca ...

  3. sql server 密钥_SQL Server中的升序密钥和CE模型变化

    sql server 密钥 In this note, I'm going to discuss one of the most useful and helpful cardinality esti ...

  4. SQL Server中的基数估计角色

    This post opens a series of blog posts dedicated to my observations of the new cardinality estimator ...

  5. SQL Server中的基数估计过程

    In this post, we are going to take a deeper look at the cardinality estimation process. We will use ...

  6. 到T-SQL DML 三级的阶梯:在SQL server中实现关系模型

    作者: Gregory Larsen, 2017/08/02 (第一次出版: 2011/11/09) 翻译:谢雪妮,许雅莉,赖慧芳,刘琼滨 译文: 系列 该文章是阶梯系列的一部分:T-SQL DML的 ...

  7. SQL Server备份的三个恢复模型

    在SQL Server 2000中,有无数种备份数据库的方法.无论你的数据库有多大.改变是否频繁,都有满足你的要求的备份策略.让我们看看几种可以在不同环境下工作的基本备份策略. 本文假定你有备份数据库 ...

  8. sql server中对xml进行操作

    一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和参数.为了更好地支持 XM ...

  9. 事物日志恢复 mysql_浅谈SQL Server中的事务日志(五)----日志在高可用和灾难恢复中的作用...

    本篇文章是系列文章中的第五篇,是对前一个日志系列的补充篇.如果您对日志的基本概念还没有一个比较系统的了解,可以参看本系列之前的文章: 浅谈SQL Server中的事务日志(一)----事务日志的物理和 ...

最新文章

  1. pandas中set_option的常用设置:显示所有行、显示所有列、控制浮点型精度、每个数据元素的显示字符数、对齐形式等
  2. [布局] bootstrap基本标签总结
  3. 科学家从脑电图中解读大脑的运动意图
  4. dyld: Library not loaded: @rpath/MySDK.framework/MySDK 错误解决
  5. 程序员面试题精选100题(57)-O(n)时间的排序[算法]
  6. Fedora 17 meld 显示行号以及语法高亮
  7. IT项目管理总结:第十章 项目沟通管理
  8. 我的世界java和pe版_《我的世界》pe版不一样的方块世界
  9. Python数据可视化案例一:自定义曲线频率、颜色与线型
  10. LaTeX报告用模板
  11. Podfile文件用法详解
  12. 第十三届“认证杯”数学中国数学建模比赛赛后体会
  13. Linux的网络基础
  14. 激光导航AGV的停车精度受环境变化的影响有多大?
  15. 微信扫一扫二维码直接打开手机外部浏览器
  16. 【OpenGrok代码搜索引擎】二、Windows10下基于Linux子系统搭建Opengrok代码搜索引擎
  17. Fabric Block区块结构解析
  18. FQQ兵法,适用于各种版本以及种族
  19. 华为云 CDN金秋特惠活动,这价可以
  20. JS把加号当成连接符

热门文章

  1. 用Maven创建第一个web项目Struts2项目
  2. 跨server传输数据注意事项
  3. Codevs 3342 绿色通道
  4. MySQL主从复制的原理及配置方法(比较详细)
  5. 8皇后问题--回溯法 (循环递归)
  6. 算法---回溯法--模板解法
  7. 【Vue2.0】—Vue与Component的关系(十二)
  8. 数据库原理—常用的DBS产品简介(六)
  9. 二套房贷款首付比例?
  10. 中小学生应不应该学英语?