sql判断基数_SQL Server中的基数估计框架版本控制

sql判断基数

This is a small post about how you may control the cardinality estimator version and determine which version was used to build a plan.

这是一篇有关如何控制基数估计器版本以及确定用于构建计划的版本的小文章。

The version of the cardinality framework is determined by the query database context, where the database has a specific compatibility level.

基数框架的版本由查询数据库上下文确定,其中数据库具有特定的兼容性级别。

When you create a database in SQL Server 2014 it has the latest compatibility level equals 120 by default. If you issue a query in that database context, the new cardinality version will be used. You may verify this by inspecting the plan property “CardinalityEstimationModelVersion” of the root language element (the one with the green icon), SELECT, for example.

在SQL Server 2014中创建数据库时,默认情况下其最新兼容级别等于120。 如果您在该数据库上下文中发出查询,则将使用新的基数版本。 您可以通过检查根语言元素(带有绿色图标的元素)(例如SELECT)的计划属性“ CardinalityEstimationModelVersion”来验证这一点。

If you issue a query in a database context that is less than 120 level compatibility, the old version of the cardinality estimation framework will be used.

如果在数据库上下文中发布的查询的兼容性低于120级,则将使用基数估计框架的旧版本。

If the new cardinality estimation version was used you’ll see the value 120 (you may remember it as 12.0 – the SQL Server 2014 version number) or 70 (also the server version number 7.0, because the old one was first introduced in early 1998 for the SQL Server 7.0 and then was evolved without major concept changes).

如果使用了新的基数估计版本,则将看到值120(您可能会记得它是12.0 – SQL Server 2014版本号)或70(也是服务器版本号7.0),因为旧版本是在1998年初首次引入的用于SQL Server 7.0,然后在没有重大概念更改的情况下进行了开发)。

Here is how it looks like:

看起来是这样的:

Note: Please, make sure, when searching the CardinalityEstimationModeVersion property, that you are using the SQL Server Management Studio 2014. If you use SSMS 2012, for example, it will silently throw it out, even without telling you, and you won’t see it!

注意:请确保在搜索CardinalityEstimationModeVersion属性时使用的是SQL Server Management Studio2014。例如,如果使用SSMS 2012,即使没有告诉您,它也会将其静默丢弃,并且您不会看见!

When Microsoft developed the new cardinality estimation framework, there was a goal to improve the quality of plans, however, there wasn’t a goal to avoid any regressions. That means that you should be prepared to regressions and know how to make an easy fix. Also, you should test your workload before moving on to the new cardinality mechanism.

当微软开发新的基数估计框架时,有一个提高计划质量的目标,但是,没有避免任何回归的目标。 这意味着您应该准备回归并知道如何进行简单修复。 另外,在继续使用新的基数机制之前,您应该测试您的工作负载。

Exactly for that purpose, Microsoft issued two new trace flags, that are officially documented. TF 2312 to force the new (120 and later) cardinality estimation behavior, the exact CE will depend on the database compatibility level, and TF 9481 to force the old one, also regardless of the compatibility level. You may use these flags on various levels, globally, session or query.

正是出于这个目的,Microsoft发布了两个新的跟踪标志,并已正式记录在案。 TF 2312强制执行新的(120及更高版本)基数估计行为,确切的CE将取决于数据库兼容性级别,而TF 9481强制执行旧的基数估计行为,而与兼容性级别无关。 您可以在全局,会话或查询的各个级别上使用这些标志。

That opens a lot of possible scenarios, i’ll enlist some of them:

这打开了很多可能的场景,我将列举其中一些:

  • You want to use the new features, like parallel SELECT INTO, but don’t want to enable the new CE – enable TF 9481 globally.

    您想使用新功能,例如并行SELECT INTO,但不想启用新的CE –全局启用TF 9481。
  • You want to try the new CE, but you are afraid of regressions – leave the compatibility level lower than 120 and enable the new CE in a particular query with the TF 2312, using QUERYTRACEON option, to test it.

    您想尝试使用新的CE,但是担心退化–将兼容性级别保持在120以下,并使用QUERYTRACEON选项使用TF 2312在特定查询中启用新的CE进行测试。
  • You enabled the new CE and everything is fine, except a few queries that had a better plan with the old CE- use TF 9481 and the option QUERYTRACEON to leverage these particular queries.

    您启用了新的CE,一切都很好,除了一些使用旧的CE具有更好计划的查询,使用TF 9481和选项QUERYTRACEON来利用这些特定查询。

You may imagine the scenario you want, what is needed to remember, that you have a full control over what’s happening – if you don’t like it – simply turn it off.

您可能会想像您想要的场景,需要记住的事情,即可以完全控制发生的事情-如果您不喜欢它,则只需将其关闭即可。

The next question naturally is, what if both TFs are enabled. In this case, they neutralize each other and the behavior is if no TF were enabled.

下一个问题自然是,如果两个TF都启用,该怎么办。 在这种情况下,它们彼此抵消,并且行为是如果未启用TF。

Now we are ready to move on to the changes of the new cardinality estimation framework!

现在我们准备继续进行新基数估计框架的更改!

目录 (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)

翻译自: https://www.sqlshack.com/cardinality-estimation-framework-version-control/

sql判断基数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值