TEMPDB不支持自动压缩,且它的数据库和文件压缩能力是有限制的;
如不得不压缩TEMPDB的大小,建议使用离线功能,如下操作:
1.停止SQL SERVER服务;
2.找到sqlservr.exe在磁盘中的位置;
3.使用sqlservr.exe输入命令sqlservr -c -f,这样启动SQL SERVER时,TEMPDB的大小能够保持最小;
4.使用TSQL命令:ALTER DATABASE TEMPDB MODIFY FILE(),这样就可以改变TEMPDB的大小;
5.重启SQL SERVER服务;
TEMPDB空间有三种对象--用户对象、内部对象以及版本存储对象;
用户对象:
用户定义的本地临时表中临时使用的对象;
例:select * from sys.all_objects
内部对象:
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
declare @i int
set @i = 0
create table #myTempTable (keycolumn int, mybigCharColumn char (1000) )
while (@i < 1000000)
begin
insert into #myTempTable values(@i,'x' )
set @i = @i + 1
end
select * from #myTempTable
order by keycolumn
declare @i int
set @i = 0
create table #myTempTable (keycolumn int, mybigCharColumn char (1000) )
while (@i < 1000000)
begin
insert into #myTempTable values(@i,'x' )
set @i = @i + 1
end
select * from #myTempTable
order by keycolumn
最后一个查询有一个ORDER BY ,由于没有建立索引,帮它只能对结果集进行排序;
在查询中,用于排序的空间是由一个SQL SERVER排序工作表对象内部管理;这个对象在sys.all_objects中是可见的;
内部对象通常用于许多其它的目的:
散列联接和散列聚合;
查询池,用于多步查询处理的数据存储;
键集游标,用于存储键值;
静态游标,用来存储查询结果;
SERVICE BROKER,用于在传递中保消息;
XML消息;
LOB变量,包括TEXT、IMAGE、NTEXT、VARCHAR(MAX)和VARBINARY(MAX);
DBCC CHECK;
查询和事件通知;
可以通过动态视图:
sys.dm_db_file_space_usage
sys.dm_db_task_space_usage
sys.dm_db_session_space_usage
版本存储对象:
系统会根据需要创建版本存储对象;
确定每个服务器进程所使用的tempdb空间;
+ expand sourceview plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
select tsu.session_id,
tsu.request_id,
tsu.alloc_count,
er.sql_handle,
er.statement_start_offset,
er.statement_end_offset,
er.plan_handle
from (
select
session_id,
request_id,
sum(internal_objects_alloc_page_count) as alloc_count,
sum(internal_objects_dealloc_page_count) as dealloc_count from sys.dm_db_task_space_usage
group by session_id,request_id
) as tsu,sys.dm_exec_requests as er
where tsu.session_id = er.session_id
and tsu.request_id = er.request_id
order by tsu.alloc_count desc
select tsu.session_id,
tsu.request_id,
tsu.alloc_count,
er.sql_handle,
er.statement_start_offset,
er.statement_end_offset,
er.plan_handle
from (
select
session_id,
request_id,
sum(internal_objects_alloc_page_count) as alloc_count,
sum(internal_objects_dealloc_page_count) as dealloc_count from sys.dm_db_task_space_usage
group by session_id,request_id
) as tsu,sys.dm_exec_requests as er
where tsu.session_id = er.session_id
and tsu.request_id = er.request_id
order by tsu.alloc_count desc
或使用SQLDiag 服务,对生产系统进行不间断的监控,并在空间不足之间采取行动;
采样方法:
Select * from sys.dm_db_session_space_usage
Select * from sys.dm_dm_file_space_usage
Dbcc inputbuffer(51)
Dbcc inputbuffer(52)
Dbcc inputbuffer(53)
页面闩锁竞争
常见的阻塞源头是元数据页、PFS、GAM和SGAM页的闩锁竞争;
可以使用下列伪脚本:
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150