SQL Server Join

SQL Server Join

执行顺序

ON 定义表连接字段(数据量大的时候添加索引也可以查询速度进行优化

On.. And.. : And 限定的条件是在 Join之前对目标表的数据进行限定

Where 对连接后的数据进行过滤筛选

SELECT * FROM EMPLOY  E LEFT JOIN DEPARTMENT D ON E.DEPTNO=D.DEPTNO AND D.DEPTNO=40-- EMPLOY 表不添加限制,所有连接之前获取所有EMPLOY表的数据 相当于Select * From Employ tmp_Employ-- DEPARTMENT 表 ,获取DEPTNO 为40的 数据,Select * from Department where Deptno = 40 tmp_Department-- 最后结果为 tmp_Department td * tmp_Employ te on te.deptno = td.deptno??SELECT * FROM EMPLOY  E LEFT JOIN DEPARTMENT D ON E.DEPTNO=D.DEPTNO Where D.DEPTNO=40-- 将 And 换成Where 则变成了两个全表数据关联之后-- 再对数据进行筛选 Where Deptno = 40 的数据

MSSQL : 执行顺序 From -> Join -> On -> And ->Left(Right) -> Where -> Select

Join的逻辑层

详情博文 : SQL SERVER – Better Performance – LEFT JOIN or NOT IN?.

个人认为 Outer Join 实际上 是在 Inner join 的结果后再对目标集进行筛选

Join 的物理层

在写查询语句的时候遇到一种情况,两张表关联的时候,对其中的一张小表加上Where条件反而导致了查询速度更慢的问题,最后排查到是优化器,选择了错误的表数据的关联方式

没加Where之前

加了Where之后

初看感觉所有的消耗都在对表查询,实际而次情况的Neseted Loop(嵌套循环) 是性能消耗最高的地方;

Hash Map
Nested Map
Merge Map

底层太难了,懒得写,引用下别人的解释

A nested loop query plan is usually optimal when there are small numbers of rows in one table (think 10s to perhaps 100s in most cases) that can be probed into another table that is either very small or has an index that allows a seek for each input. One thing to watch out for here is when the optimizer THINKS there will be few rows (check the estimated rows in the popup for the estimated query plan graphic) but in reality there are LOTS of rows (thousands or even millions). This is one of the worst things that can happen to a query plan and is usually the result of either out-of-date statistics, a cached query plan or data that is unevenly distributed. Each of these can be addressed to minimize the likelyhood of the problem

.A hash-match plan is optimal when there is a relatively few rows joined into a relatively large number of rows. Gail‘s post explains the basics of the mechanism. It can get REALLY slow on machines with insufficient buffer RAM to contain the hash tables in memory because they will have to be laid down to disk at a HUGE relative cost.

原文地址:https://www.cnblogs.com/Gilfoyle/p/12005143.html

时间: 2024-08-07 01:28:16

SQL Server Join的相关文章

SQL Server Join方式

1.测试数据准备 参考:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek 这篇博客中的实验数据准备.这两篇博客使用了相同的实验数据. 2.SQL Server中的三种Join方式 在Sql Server中,每一个join命令,在内部执行时,都会采用三种更具体的join方式来运行.这三种join的方法是:nested loops join.merge join和hash join.这三种方法,没有哪一种是永远最好的,但是都有其最适合的上下文.S

sql server join ,inner join ,left join ,right join 的使用

测试数据脚本 CREATE TABLE Atable ( S# INT, Sname nvarchar(32), Sage INT, Sfrom nvarchar(8) ) insert into Atable select 1,N'李四',18,N'A' union all select 2,N'tom',19, N'A' union all select 3,N'刘一',17,N'A' union all select 4,N'jack',18,N'A' CREATE TABLE Btabl

SQL Server的三种物理连接之Hash Join(三)

简介 在 SQL Server 2012 在一些特殊的例子下会看到下面的图标: Hash Join分为两个阶段,分别为生成和探测阶段. 首先是生成阶段,将输入源中的每一个条目经过散列函数的计算都放到不同的Hash Bucket中,其中Hash Function的选择和Hash Bucket的数量都是黑盒,通常来讲,查询优化器都会使用连接两端中比较小的哪个输入集来作为第一阶段的输入源. 接下来是探测阶段,对于另一个输入集合,同样针对每一行进行散列函数,确定其所应在的Hash Bucket,在针对这

INNER JOIN与LEFT JOIN在SQL Server的性能

我创建了INNER JOIN 9桌,反正需要很长的(超过五分钟).所以,我的民歌改变INNER JOIN来LEFT JOIN LEFT JOIN的性能较好,在首次尽管我所知道的.之后我变了,查询的速度显著提高. 我想知道为什么LEFT JOIN的速度比INNER JOIN? 我的样子如下:SELECT * FROM A INNER JOIN B ON ... INNER JOIN C ON ... INNER JOIN D因此没有 更新: 这是我的简单架构的. FROM sidisaleshdr

Sql Server 与 MySql 在使用 update inner join 时的区别

Sql Server update tb_User set pass = ''-- 此处pass前不要加 tb_User 别名usr from tb_User usr inner join tb_Address addr on usr.nAddressFK = addr.nAddressID where usr.id=123 MySql UPDATE mem_world AS mw1 INNER JOIN mem_world AS mw2 ON mw1.parentid = mw2.wid SE

SQL SERVER中 外联接即(left join)on 和 where 的区别

使用内联接,无论在JOIN 子句还是 WHERE 子句中,条件具有相同的结果,但使用外联接时并非如此. 当条件在JOIN子句时,SQL SERVER包括外表的所有行,然后使用条件包括第二个表中的行. 当限制置于WHERE子句时,先执行联接,然后将where子句应用于联接行. 上述表述不是很明白 ,以下的解释更清楚. 即:ON 条件(“A LEFT JOIN B ON 条件表达式”中的ON)用来决定如何从 B 表中检索数据行. 如果 B 表中没有任何一行数据匹配 ON 的条件,将会额外生成一行所有

SQL Server nested loop join 效率试验

从很多网页上都看到,SQL Server有三种Join的算法, nested loop join, merge join, hash join. 其中最常用的就是nested loop join. 在介绍nested loop join的很多文章里,都提到如果两个表做nested loop join,取行数较小的表作为外循环表,行数较多的表作为内循环表, join的效率会比较高. 其中之一的原因是如果内循环表做join的列上有合适的索引的话,那么外循环的每一条输入数据可以做索引的seek,这样就

【Transact-SQL】SQL Server自动把left join自动转化为inner join、以及关联时的数据重复问题

原文:[Transact-SQL]SQL Server自动把left join自动转化为inner join.以及关联时的数据重复问题 1.SQL Server自动把left join自动转化为inner join的问题: 下面的两个语句都是left join的,但是一个却转化成了 inner join drop table a,B go create table a(id int) insert into a select 1 union all select 2 create table b

SQL Server 2012 案例教程(贾祥素)——学习笔记

第2章 SQL Server 2012概述 1.SQL(Structed Query Language),结构化查询语言. 2.SSMS(SQL Server Mangement Studio),SQL Server 2012的操作环境. 3.连接SQL Server之前应先启动SQL Server服务,即SQL Server(MSSQLSERVER): 方法1 开始--所有程序--Microsoft SQL Server 2012--配置工具--SQL Server配置管理器. 方法2 控制面