SQL JOIN-Hash Join

1概述

hash join 在特性与merge join相同,都需要一个等值条件。当在连接条上无法命中索引,或大集合的Join, nested join和 merge join可能就无法得到很好的性能,这时我们就需要考虑用hash join.

2基本算法

Hash join 分为两个阶段,build和probe。在build阶段,会将其中一个集合作为build set,然后hash build table在连接条件上的列,并将结果存储在内存中的(命名为build hash table).  在probe阶段(将第二个集合命名为probe set),每一行hash probe set在连接条件上的列,然后与build hash table比较,如果相等,则返回。

伪代码:

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
                return (R1, R2)
    end

3 示例

测试数据

View Code
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

set @i = 0

while @i < 10000

  begin

    insert T2 values (@i * 3, @i * 7, @i)

    set @i = @i + 1

  end

set @i = 0

while @i < 100000

  begin

    insert T3 values (@i * 5, @i * 11, @i)

    set @i = @i + 1

  end

执行SQL:

SET STATISTICS PROFILE ON
select *
from 
    (
        T1 inner join T2 on T1.a = T2.a
    )
    inner join T3 on T1.b = T3.a
option (hash join)

执行结果:

转载于:https://www.cnblogs.com/dataadapter/archive/2012/10/25/2740419.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值