表变量特点补充,为什么表变量在大数据情况下普遍性能不佳

之前有写一篇文章

http://blog.csdn.net/starseeker7/article/details/8621046

今天看到论坛上在讨论这个东西,顺便更详细的补充一下

--2014/06/07补充:

我们再优化SQL时,经常会发现若对表变量填充大量数据,性能往往会不如使用临时表做相同动作

那么到底是上面的哪一点造成这个情况的呢?

最近看到一篇文章比较详细的测试了一下,发现是上面第六点最具有决定性的影响。

参考地址:http://blogs.msdn.com/b/psssql/archive/2010/08/24/query-performance-and-table-variables.aspx

测试环境搭建

set statistics profile off
go
create table t2 (c2 int)

go
--insert 100,000 rows into the perm table
set nocount on

declare @i int
set @i = 0
while @i < 100000
begin
insert into t2 values (@i)
set @i = @i + 1
end
go
--update stats
update statistics t2


--测试底性能的执行计划

--2.  join permantant table with table variable the table variable gets 100,000 rows inserted then it is joined to t2 @t1 gets 1 rows estimate it ends up with nested loop join
set nocount on
declare @t1 table (c1 int)
begin tran
declare @i int
set @i = 0
while @i < 100000
begin
insert into @t1 values (@i)
set @i = @i + 1
end
commit tran
set statistics profile on

set statistics IO on
select * from @t1 inner join t2 on c1=c2
go

set statistics profile off
set statistics IO off
go






--使用option关键字,进行强制重编译优化后的执行计划

--3. solution use stmt level recompile

declare @t1 table (c1 int)
set nocount on
begin tran
declare @i int
set @i = 0
while @i < 100000
begin
insert into @t1 values (@i)
set @i = @i + 1
end
commit tran
set statistics profile on

set statistics IO on
select * from @t1 inner join t2 on c1=c2 option (recompile)
go

set statistics profile off
set statistics IO off
go




我们可以看到而这性能差异很大,具体导致原因我们通过通过statistics profile获取的执行计划对比得出

他们的esimaterows区别很大,同样是table scan@t1第一次仅识别到1行,而第二个查询中,才能正确世界到10W行数据

这直接导致了他们采取了不同的inner join方式,第一个使用的是nested join而第二个使用的是hash match

而大数据情况下hash match具有非常明显的优势。这也就是为什么使用表值函数在应用到较多数据的情况下性能低下的根本原因。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值