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)

  • ALTER DATABASE (Transact-SQL) Compatibility Level ALTER DATABASE(Transact-SQL)兼容性级别
  • Enable plan-affecting SQL Server query optimizer behavior that can be controlled by different trace flags on a specific-query level 启用受计划影响SQL Server查询优化器行为,该行为可由特定查询级别上的不同跟踪标志控制
  • Identifying SQL Server 2014 New Cardinality Estimator issues and Service Pack 1 improvement 识别SQL Server 2014新基数估计器问题和Service Pack 1改进

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

sql判断基数

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

  1. sql limit 子句_SQL Server中的FOR XML PATH子句

    sql limit 子句 As SQL professionals, we often have to deal with XML data in our databases. This articl ...

  2. sql 实现决策树_SQL Server中的Microsoft决策树

    sql 实现决策树 Decision trees, one of the very popular data mining algorithm which is the next topic in o ...

  3. sql数据透视_SQL Server中的数据科学:取消数据透视

    sql数据透视 In this article, in the series, we'll discuss understanding and preparing data by using SQL ...

  4. sql语句截断_SQL Server中SQL截断和SQL删除语句之间的区别

    sql语句截断 We get the requirement to remove the data from the relational SQL table. We can use both SQL ...

  5. oracle 动态sql列转行_SQL Server中动态列转行

    http://www.cnblogs.com/gaizai/p/3753296.html 一.本文所涉及的内容(Contents) 三.实现代码(SQL Codes) (一) 首先我们先创建一个测试表 ...

  6. 对于数据给定范围sql取数_SQL Server中的报表–根据给定日期范围内提取的数据创建图表

    对于数据给定范围sql取数 介绍 ( Introduction ) I recently heard from a lady from overseas who wanted to find a qu ...

  7. server sql 本月最后一天_SQL SERVER中求上月、本月和下月的第一天和最后一天 DATEADD DATEDIFF...

    1.上月的第一天 SELECT CONVERT(CHAR(10),DATEADD(month,-1,DATEADD(dd,-DAY(GETDATE())+1,GETDATE())),111) 2.上月 ...

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

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

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

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

最新文章

  1. boost::core模块实现分配器指针
  2. 活动目录向DNS注册SRV记录不成功的原因
  3. WebView内存泄露的解决方案
  4. 简单而又完整的Makefile-转
  5. php urldecode 加号不显示_php|urldecode urlencode 的加号问题
  6. 判断浏览器是IE的几种方式
  7. 【转】Prewitt 算子
  8. 练习-Java类和对象之包的定义
  9. word图片撑满,word怎么让图片占满整个页面?
  10. Crout分解法 | matlab
  11. ie11启用java时打不开_ie11打不开解决方法
  12. 如何设置Foxmail收取yahoo.com.cn和yahoo.cn以及yahoo.com等邮箱的方法
  13. echarts饼图制作
  14. 叮咚同城小程序问题:点击发布信息按钮自动闪退到首页的解决方法
  15. video.js API 详解
  16. 求大神帮忙解答!!!急!!!
  17. 吃烧烤之命令模式学习笔记[C++版] 1
  18. How Tomcat Works, A Guide to Developing Your Own Java Servlet Container
  19. 将任意一个对象中的所有String类型的成员变量所对应的字符串内容中的“a“改为“b“(封装成一个方法)
  20. C4D团队渲染联机渲染,异地电脑一起渲染,使用RS,OC等渲染器。

热门文章

  1. think in python 第二版_译林版牛津高中英语,模块词组二
  2. xmpp协议详解一:xmpp基本概念
  3. 通过XShell实现windows文件上传到Linux服务器上
  4. js中如何把字符串转化为对象、数组示例代码
  5. 【BZOJ3489】A simple rmq problem(树套树)
  6. Centos系统创建用户oracle后,用该用户登陆系统,页面加载报错GConf error
  7. Xcode启动RN报错“`fsevents` unavailable“
  8. ios-deploy out of date (1.9.4 is required). To upgrade with Brew: brew upgrade ios-deploy
  9. spingMVC拦截器 -单个、多个、设想
  10. 业务线开发流程图(四)