sqlserver表变量和临时表

来源:互联网 发布:kindle 推送 知乎周刊 编辑:程序博客网 时间:2024/06/13 15:10

      两者都是常用的存储数据方式 。

   

从性能角度对比两者,可以概括为两点:

         

        --表变量的操作都会在独立的transaction中立即commit,大大减少了锁定资源的需求

        --因为表变量是无统计信息的。  所以存储过程不会因为表变量的数据量变化而重编译  。

实验证明如下:

begin tran

declare @i int

insert into  @i select 2

select * from sys.dm_tran_locks ;
rollback ;
go

 

begin tran

craete table # (id int )
insert into # select 2 
select * from sys.dm_tran_locks ;
rollback ;
go

-------实验结果可见 :表变量大大减少了锁定资源的操作 。

 

 

 

 

 

 

 

原创粉丝点击