博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql判断基数_SQL Server中的基数估计框架版本控制
阅读量:2514 次
发布时间:2019-05-11

本文共 4381 字,大约阅读时间需要 14 分钟。

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 Framework Version Control in SQL Server
SQL Server中的基数估计框架版本控制

参考资料 (References)

翻译自:

sql判断基数

转载地址:http://yyiwd.baihongyu.com/

你可能感兴趣的文章
JDK工具(一)–Java编译器javac
查看>>
深入.NET框架与面向对象的回顾
查看>>
改变label中的某字体颜色
查看>>
[转]SQL SERVER 的排序规则
查看>>
C语言函数的可变参数列表
查看>>
七牛云存储之应用视频上传系统开心得
查看>>
struts2日期类型转换
查看>>
Spark2-数据探索
查看>>
大数据初入门
查看>>
Java学习笔记-类型初始化
查看>>
鱼那么信任水,水却煮了鱼
查看>>
Http和Socket连接区别
查看>>
Arrays基本使用
查看>>
Angular2,Springboot,Zuul,Shiro跨域CORS请求踩坑实录
查看>>
C语言中操作符的优先级大全
查看>>
pgpool-II - 介绍
查看>>
[转载+原创]Emgu CV on C# (四) —— Emgu CV on 全局固定阈值二值化
查看>>
#leetcode刷题之路29- 两数相除
查看>>
Alpha冲刺(10/10)——2019.5.2
查看>>
图书管理系统用例
查看>>