易截截图软件、单文件、免安装、纯绿色、仅160KB

MS SQL 查询联接运算系列--哈希联接(Hash Join)

哈希联接是第三种物理联接运算符,当说到哈希联接时,它是三种联接运算中性能最好的.嵌套循环联接适用于相对较小的数据集,而合并联接适用于中等规模的数据集,而哈希联接则适用于大规模联接的数据集.
哈希联接算法采用"构建"和"探测"两步来执行.在"构建"阶段,它首先从第一个输入中读取所有行(常叫做左或构建输入)在相等联接键上对这些行进行哈
希,然后在内存中创建哈希表,在"探测"阶段,从第二个输入中读取所有行(常叫做右中探测输入)在相的等值联接键上对这些行进行哈希,查找可探测该哈希表
中匹配的行.
下面是伪代码描述:
for each row R1 in the build table
begin
calculate hash value on R1 join key(s)
insert R1 into the appropriate hash bucket
end
for each row R2 in the probe table
begin
calculate hash value on R2 join key(s)
for each row R1 in the corresponding hash bucket
if R1 joins with R2
output (R1, R2)
end
示例:
创建表对象架构:
create table T1 (a int, b int, x char(200))
create table T2 (a int, b int, x char(200))
create table T3 (a int, b int, x char(200))
set nocount on
declare @i int
set @i = 0
while @i < 1000
begin
insert T1 values (@i * 2, @i * 5, @i)
set @i = @i + 1
end
GO
declare @i int
set @i = 0
while @i < 10000
begin
insert T2 values (@i * 3, @i * 7, @i)
set @i = @i + 1
end
GO
declare @i int
set @i = 0
while @i < 100000
begin
insert T3 values (@i * 5, @i * 11, @i)
set @i = @i + 1
end
GO
现在执行以下查询:
set statistics profile on
select *
from T1 join T2 on T1.a = T2.a
set statistics profile off
执行上述查询得到如下查询计划:
[img=550,189 alt=]http://www.haixiait.com/attachments/month_0804/v200841125633.JPG[/img]

从输出信息了解到,T2表是T1表的10倍,而查询优化器则选择T1表作为"构建"表,T2表作为"探测"表.
下面来看一下三个表的联接:
set statistics profile on
select *
from (T1 join T2 on T1.a = T2.a)
join T3 on T1.b = T3.a
set statistics profile off
执行上述查询得到如下计划:  
[img=550,191 alt=]http://www.haixiait.com/attachm


相关文档:

[SQL Server] 存储过程事务

在存储过程中使用事务,以下为模板:
CREATE PROCEDURE testPro
AS
/**//* ------- 事务开始---------- */
BEGIN TRANSACTION tran_test
/**//* -------- 保存事务----------*/
SAVE TRANSACTION tran_test
/**//* -------- 数据操作---------*/
INSERT [table1] ( [content] ) VALUES ( '43332' )
/**//*---- ......

sql中 in 、not in 、exists、not exists 用法和差别

exists (sql 返回结果集为真)
not exists (sql 不返回结果集为真)
如下:
表A
ID NAME
1    A1
2    A2
3  A3
表B
ID AID NAME
1    1 B1
2    2 B2
3    2 B3
表A和表B是1对多的关系 A.ID => B.AID
......

SQL Server字符串分割

一、按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果。
CREATE function Get_StrArrayLength
(
  @str varchar(1024),  --要分割的字符串
  @split varchar(10)  --分隔符号
) ......

精妙SQL语句收集

                                                    精妙SQL语句收集 
1、说明:创建数据库CREATE DATABASE database-name 2、说明:删除数据库drop database dbn ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号