加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

Sql Server Tempdb原理:日志机制解析实践

发布时间:2016-01-12 10:28:15 所属栏目:MsSql教程 来源:网络整理
导读:笔者曾经在面试DBA时的一句tempdb为什么比其他数据库快?使得95%以上的应试者都一脸茫然.Tempdb作为Sqlserver的重要特征,一直以来大家对它可能即熟悉又陌生.熟悉
笔者曾经在面试DBA时的一句”tempdb为什么比其他数据库快?”使得95%以上的应试者都一脸茫然.Tempdb作为Sqlserver的重要特征,一直以来大家对它可能即熟悉又陌生.熟悉是我们时时刻刻都在用,陌生可能是很少有人关注它的运行机制.这次我将通过实例给大家介绍下tempdb的日志机制.

测试用例

我们分别在用户数据库(testpage),tempdb中创建相似对象t1,#t1,并在tempdb中创建创建非临时表,然后执行相应的insert脚本(用以产生日志),并记录执行时间用以比较用以比较说明tempdb”快”

Code

用户数据库testpage

use testpage
go
create table t1
(
id int identity(1,1) not null,
str1 char(8000)
)
     
declare @t datetime2=sysutcdatetime()
declare @i int
set @i=1
while (@i<100000)
begin
insert into t1 select @i,'aa'
select @i=@i+1
end
select [extime]=DATEDIFF(S,@t,sysutcdatetime())

tempdb

use tempdb
go
create table #t1
(
id int not null,
str1 char(8000)
)
     
declare @t datetime2=sysutcdatetime()
declare @i int
set @i=1
while (@i<100000)
begin
insert into #t1 select @i,'aa'
select @i=@i+1
end
select [extime]=DATEDIFF(S,@t,sysutcdatetime())

非临时表在tempdb中执行

use tempdb
go
create table t1
(
id int not null,
str1 char(8000)
)
     
declare @t datetime2=sysutcdatetime()
declare @i int
set @i=1
while (@i<100000)
begin
insert into t1 select @i,'aa'
select @i=@i+1
end
select [extime]=DATEDIFF(S,@t,sysutcdatetime())

由图1-1中我们可以看出,在普通表中执行一分钟的脚本,tempdb只需执行22s.而普通表在tempdb中也只需27s均大大优于普通表中执行情况.

感兴趣的朋友亦可在执行过程中观察日志相关的性能技术器的运行情况如(Log Bytes Flusged sec 等)

Sql Server Tempdb原理:日志机制解析实践

图1-1

由此测试我们可以看出本文开始提到的”tempdb比其他数据库快”.

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读