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
相关文档:
1.SQL并行查询
alter session enable parallel dml execute immediate 'alter session enable parallel dml'; --修改会话并行DML select /*+parallel(a,4)*/ * from table_name a select /*+parallel(a,8)*/ * from table_name a ......
要想成功访问 SQL Server 数据库中的数据,我们需要两个方面的授权:一、获得准许连接 SQL Server 服务器的权利;二、获得访问特定数据库中数据的权利(select, update, delete, create table ...)。假设,我们准备建立一个 dba 数据库帐户,用来管理数据库 mydb。
1. 首先在 SQL Server 服务器级别,创建登陆帐户(creat ......
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语句:
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 ......