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
相关文档:
系统环境:Windows 7
软件环境:Visual C++ 2008 SP1 +SQL Server 2005
本次目的:编写一个航空管理系统
这是数据库课程设计的成果,虽然成绩不佳,但是作为我用VC++ 以来编写的最大程序还是传到网上,以供参考。用VC++ 做数据库设计并不容易,但也不是不可能。以下是我的程序界面,后面 ......
问题的提出:
现有表A,内容如下:
编码 仓库 数量
01 A 6
01 B 7
02 A & ......
一、按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果。
CREATE function Get_StrArrayLength
(
@str varchar(1024), --要分割的字符串
@split varchar(10) --分隔符号
)
......
数据库备份 作业中的Sql语句:
DECLARE @strPath NVARCHAR(200)
set @strPath = convert(NVARCHAR(19),getdate(),120)
set @strPath = REPLACE(@strPath, ':' , '_')
set @strPath = REPLACE(@strPath, '-' , '_')
set @strPath = REPLACE(@strPath, ' ' , '_')
set @strPath = 'F:\数据库备份\' + myData_'+@s ......
SQL Server根据查询结果,生成XML文件
来源:不详 作者:佚名 时间:2009-3-6 22:15:58 发布:黑客软件园
/*
'bcp' 不是内部或外部命令,也不是可运行的程序?
看看在C:\Program Files\Microsoft SQL Server\80\Tools\Binn里面有没有bcp.exe这个文件
然后看看path里面加C:\Program Files\Microso ......