Temporary Tables vs. Table Variables and Their Effect on SQL Server Performance

本文通过实验对比了SQL Server中临时表与表变量在不同场景下的性能表现,包括事务处理、预编译及索引创建等方面,并针对SQL Server 2005进行了特别说明。

 

Temporary Tables vs. Table Variables and Their Effect on SQL Server Performance
September 13th, 2007

Original http://www.sqlsolutions.com/articles/articles/Temporary_Tables_vs._Table_Variables_and_Their_Effect_on_SQL_Server_Performance.htm 

 

 

There are 3 major theoretical differences between temporary tables:

create table #T (...)

and table-variables

declare @T table (...)

The first one is that transaction logs are not recorded for the table-variables. Hence, they are out of scope of the transaction mechanism, as is clearly visible from this example:

create table #T (s varchar(128))
declare @T table (s varchar(128))
insert into #T select 'old value #'
insert into @T select 'old value @'
begin transaction
  update #T set s='new value #'
  update @T set s='new value @'
rollback transaction
select * from #T
select * from @T

s              
--------------- 
old value #

s                 
--------------- 
new value @

After declaring our temporary table #T and our table-variable @T, we assign each one with the same 'old value' string. Then, we begin a transaction that updates their contents. At this point, both will now contain the same 'new value' string. But when we rollback the transaction, as you can see, the table-variable @T retained its value instead of reverting back to the 'old value' string. This happened because, even though the table-variable was updated within the transaction, it is not a part of the transaction itself.

The second major difference is that any procedure with a temporary table cannot be pre-compiled, while an execution plan of procedures with table-variables can be statically compiled in advance. Pre-compiling a script gives a major advantage to its speed of execution. This advantage can be dramatic for long procedures, where recompilation can be too pricy.

Finally, table-variables exist only in the same scope as variables. Contrary to the temporary tables, they are not visible in inner stored procedures and in exec(string) statements. Also, they cannot be used in insert/exec statement.

But let's compare both in terms of performance.

At first, we prepare a test table with 1 million records:

create table NUM (n int primary key, s varchar(128))
GO
set nocount on
declare @n int
set @n=1000000
while @n>0 begin 
  insert into NUM 
    select @n,'Value: '+convert(varchar,@n)
  set @n=@n-1
  end
GO

Now we prepare our test procedure T1:

create procedure T1
  @total int
as
  create table #T (n int, s varchar(128))
  insert into #T select n,s from NUM 
    where n%100>0 and n<=@total
  declare @res varchar(128)
  select @res=max(s) from NUM
    where n<=@total and
      not exists(select * from #T 
      where #T.n=NUM.n)
GO

Called with a parameter, which we will vary from 10, 100, 1000, 10'000, 100'000 up to 1'000'000, it copies the given number of records into a temporary table (with some exceptions, it skips records where n is divisible by 100), and then finds a max(s) of such missing records. Of course, the more records we give, the longer the execution is.

To measure the execution time precisely, I use the code:

declare @t1 datetime, @n int
set @t1=getdate()
set @n=100-- (**)
while @n>0 begin
  exec T1 1000 -- (*)
  set @n=@n-1 end
select datediff(ms,@t1,getdate())
GO

(*) is a parameter to our procedure, it is varied from 10 to 1'000'000
(**) if an execution time is too short, I repeat the same loop 10 or 100 times.
I run the code several times to get a result of a 'warm' execution.

The results can be found in Table 1 (see below).

Now let's try to improve our stored procedure by adding a primary key to the temporary table:

create procedure T2
  @total int
as
  create table #T (n int primary key, s varchar(128))
  insert into #T select n,s from NUM 
    where n%100>0 and n<=@total
  declare @res varchar(128)
  select @res=max(s) from NUM
    where n<=@total and
      not exists(select * from #T 
      where #T.n=NUM.n)
GO

Then, lets create a third one. With a clustered index, it works much better. But let's create the index AFTER we insert data into the temporary table - usually, it is better:

create procedure T3
  @total int
as
  create table #T (n int, s varchar(128))
  insert into #T select n,s from NUM 
    where n%100>0 and n<=@total
  create clustered index Tind on #T (n)
  declare @res varchar(128)
  select @res=max(s) from NUM
    where n<=@total and
      not exists(select * from #T 
      where #T.n=NUM.n)
GO

Surprise! It not only takes longer for the big amounts of data; merely adding 10 records take an additional 13 milliseconds. The problem is that 'create index' statements force SQL server to recompile stored procedures, and slows down the execution significantly.

Now let's try the same using table-variables:

create procedure V1
  @total int
as
  declare @V table (n int, s varchar(128))
  insert into @V select n,s from NUM 
    where n%100>0 and n<=@total
  declare @res varchar(128)
  select @res=max(s) from NUM
    where n<=@total and
      not exists(select * from @V V 
      where V.n=NUM.n)
GO

To our surprise, this version is not significantly faster than the version with the temporary table. This is a result of a special optimization SQL server has for the create table #T statements in the very beginning of a stored procedure. For the whole range of values, V1 works better or the same as T1.

Now let's try the same with a primary key:

create procedure V2
  @total int
as
  declare @V table (n int primary key, s varchar(128))
  insert into @V select n,s from NUM 
    where n%100>0 and n<=@total
  declare @res varchar(128)
  select @res=max(s) from NUM
    where n<=@total and
      not exists(select * from @V V 
      where V.n=NUM.n)
GO

The result is much better, but T2 outruns this version.

 

 

 

In some cases, SQL 2005 was much faster then SQL 2000 (marked with green). But in many cases, especially with huge amounts of data, procedures that used table variables took much longer (highlighted with red). In 4 cases, I even gave up waiting.

Conclusion:

1. There is no universal rule of when and where to use temporary tables or table variables. Try them both and experiment.

2. In your tests, verify both sides of the spectrum - small amount/number of records and the huge data sets.

3. Be careful with migrating to SQL 2005 when you use complicated logic in your stored procedures. The same code can run 10-100 times slower on SQL server 2005!

基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制方法。通过结合数据驱动技术与Koopman算子理论,将非线性系统动态近似为高维线性系统,进而利用递归神经网络(RNN)建模并实现系统行为的精确预测。文中详细阐述了模型构建流程、线性化策略及在预测控制中的集成应用,并提供了完整的Matlab代码实现,便于科研人员复现实验、优化算法并拓展至其他精密控制系统。该方法有效提升了纳米级定位系统的控制精度与动态响应性能。; 适合人群:具备自动控制、机器学习或信号处理背景,熟悉Matlab编程,从事精密仪器控制、智能制造或先进控制算法研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①实现非线性动态系统的数据驱动线性化建模;②提升纳米定位平台的轨迹跟踪与预测控制性能;③为高精度控制系统提供可复现的Koopman-RNN融合解决方案; 阅读建议:建议结合Matlab代码逐段理解算法实现细节,重点关注Koopman观测矩阵构造、RNN训练流程与模型预测控制器(MPC)的集成方式,鼓励在实际硬件平台上验证并调整参数以适应具体应用场景。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值