This post opens a series of blog posts dedicated to my observations of the new cardinality estimator in SQL Server 2014. But, before we jump to the new features, I’d like to provide some background, to make the next posts clearer.

这篇文章打开了一系列博客文章,这些文章专门针对我对SQL Server 2014中新基数估计器的观察。但是,在我们跳转至新功能之前,我想提供一些背景知识,以使下一篇文章更加清晰。

We’ll start by discussing the role of Cardinality Estimation in SQL Server, trying to answer – what is it and why it is needed.

我们将首先讨论基数估计在SQL Server中的作用,然后尝试回答–它是什么以及为什么需要它。

SQL Server has the Cost Based Optimizer. The optimizer chooses the cheapest plan, where cost represents an estimate of the resource consumption.

SQL Server具有基于成本的优化器。 优化器选择最便宜的计划,其中成本表示对资源消耗的估计。

Cardinality estimation is the crucial mechanism in SQL Server, because, it is a main argument in the costing function. Cardinality estimation influences a lot of things, here are some of them:

基数估计是SQL Server中的关键机制,因为它是成本核算函数中的主要论据。 基数估计会影响很多因素,其中包括:

  • Access Method Strategy 访问方法策略
  • Join Order Choise 加入订单选择
  • Join Type Choise 加入类型选择
  • Memory grants 内存授予
  • Optimization Efforts Spent 优化工作
  • Whole Plan Shape 整体平面图

A lot of things are not included in this general list, however, they are also influenced by cardinality.

此总列表中未包括很多内容,但是它们也受基数的影响。

Let’s create a simple synthetic database “opt”, it will be used in this and future blog posts for the simple demos. It contains three plain tables of 1000 rows each with primary keys, two of them have a foreign key relationship.

让我们创建一个简单的综合数据库“ opt”,它将在本博客以及以后的博客文章中进行简单演示。 它包含三个具有1000行的普通表,每个普通表都有主键,其中两个具有外键关系。

use master;
go
if db_id('opt') is not null drop database opt;
go
create database opt;
go
use opt;
go
create table t1(a int not null, b int not null, c int check (c between 1 and 50), constraint pk_a primary key(a));
create table t2(b int not null, c int, d char(10), constraint pk_b primary key(b));
create table t3(c int not null, constraint pk_c primary key(c));
go
insert into t1(a,b,c) select number, number%100+1, number%50+1 from master..spt_values where type = 'p' and number between 1 and 1000;
insert into t2(b,c) select number, number%100+1 from master..spt_values where type = 'p' and number between 1 and 1000;
insert into t3(c) select number from master..spt_values where type = 'p' and number between 1 and 1000;
go
alter table t1 add constraint fk_t2_b foreign key (b) references t2(b);
go
create statistics s_b on t1(b);
create statistics s_c on t1(c);
create statistics s_c on t2(c);
create statistics s_d on t2(c);
go

Now, imagine simple queries that joins two tables and filter rows.

现在,想象一下连接两个表和过滤器行的简单查询。

set showplan_xml on
go
select * from t1 join t2 on t1.a = t2.c where t1.a <= 10
select * from t1 join t2 on t1.a = t2.c where t1.a <= 100
go
set showplan_xml off
go

Depending on the estimated number of rows (estimated cardinality), we get different plans, even in such a simple case!

根据估计的行数(估计的基数),即使在这种简单情况下,我们也会获得不同的计划!

The only thing that is different, is the estimated number of rows. These different estimates lead to, at least, three item changes, according to the list mentioned above:

唯一不同的是估计的行数。 根据上述清单,这些不同的估计至少会导致三个项目更改:

  • Access Method Strategy (t2 Seek vs. t2 Scan) 访问方法策略(t2搜寻与t2扫描)
  • Join Order Choise (t1 Join t2 vs. t2 Join t1) 选择加入顺序(t1加入t2与t2加入t1)
  • Join Type Choise (Nested Loops Join vs. Merge Join) 选择联接类型(嵌套循环联接与合并联接)

Now we may see, how sensitive is the Query Optimizer to the cardinality estimation. Wrong estimates may lead to a very inefficient plan, that’s why it is so important component of SQL Server, and that’s why I think it is worth to spend time learning how these estimates are made.

现在我们可以看到,查询优化器对基数估计有多敏感。 错误的估计值可能会导致计划效率非常低下,这就是为什么它是SQL Server如此重要的组成部分,这就是为什么我认为值得花时间学习如何进行这些估计。

Next we’ll discuss a place of Cardinality Estimation in the whole optimization process.

接下来,我们将讨论基数估计在整个优化过程中的位置。

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

  • Cardinality Estimation (SQL Server) 基数估计(SQL Server)
  • Troubleshooting Poor Query Performance: Cardinality Estimation 解决查询性能差的问题:基数估计
  • Query Tuning Fundamentals: Density, Predicates, Selectivity, and Cardinality 查询调优基础知识:密度,谓词,选择性和基数

翻译自: https://www.sqlshack.com/cardinality-estimation-role/

SQL Server中的基数估计角色相关推荐

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

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

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

    sql判断基数 This is a small post about how you may control the cardinality estimator version and determi ...

  3. SQL Server中出现用户或角色在当前数据库已经存在的问题的解决

    SQL Server中出现用户或角色在当前数据库已经存在的问题的解决 参考文章: (1)SQL Server中出现用户或角色在当前数据库已经存在的问题的解决 (2)https://www.cnblog ...

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

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

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

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

  6. SQL Server中的MTVF和CE模型变化

    This is a note about multi-statement table valued functions (MTVF) and how their cardinality is esti ...

  7. sql server定义_在SQL Server中查看定义权限

    sql server定义 We have various database objects such as view, stored procedures, triggers, functions a ...

  8. SQL Server中通用数据库角色权限的处理详解

    SQL Server中通用数据库角色权限的处理详解 前言 安全性是所有数据库管理系统的一个重要特征.理解安全性问题是理解数据库管理系统安全性机制的前提. 最近和同事在做数据库权限清理的事情,主要是删除 ...

  9. sql server登录名、服务器角色、数据库用户、数据库角色、架构区别联系

    原创链接:https://www.cnblogs.com/lxf1117/p/6762315.html sql server登录名.服务器角色.数据库用户.数据库角色.架构区别联系 1.一个数据库用户 ...

最新文章

  1. 星星模型 维度_用模型“想象”出来的target来训练,可以提高分类的效果!
  2. python的mysql模块_python使用MySQLdb模块连接MySQL
  3. 【已解决】R read.table()报错:incomplete final line found by readTableHeader
  4. php判断表单提交是否为空,JS判断提交表单不能为空代码 多种方案
  5. ActiveMQ的签收方式
  6. JSONObject中optString和getString等的区别
  7. 在物理内存中观察CLR托管内存及GC行为
  8. 1000道Python题库系列分享四(40道)
  9. [物理题+枚举] hdu 4445 Crazy Tank
  10. 航空三字代码表_航空公司二字及三字代码表
  11. window驱动备份与安装
  12. CC攻击防御策略详解,效果显著
  13. 用U盘安装ubuntu系统
  14. fastq文件转化成bam文件
  15. LSL-- Events
  16. pythonocc 坐标系平移
  17. 爱分析访谈阿博茨CEO杨永智:从感知到认知,阿博茨用AI解放金融民工
  18. 纺织再生产品申请grs注意标准
  19. 前端菜鸟模仿新浪导航
  20. 很强,3万字把华为HCIA知识点全部总结了

热门文章

  1. Codeforces 1163A - Eating Soup
  2. 4章 关键的“构建”决策
  3. 根据自己平时的经验写的针对SQLSERVER操作的通用类库
  4. LeetCode(2)——两数相加(JavaScript)
  5. 微信里文件小程序导不出来_懒得打开电脑传文件?这四类小程序把这事解决了-小程序视频怎么发在电脑上...
  6. “人,年轻时一定要去大公司、大城市奋斗”你怎么看?
  7. 自动挡跑高速用S挡还是D挡? 回答
  8. 湿气重的人,脸上会有哪些信号?
  9. 你缺钱只有一个原因就是你每天做赚钱的动作太少了
  10. 遇到事情不计较就是善待自己