查看temp db
use tempdb
-- 每隔1秒鐘運行一次,直到用戶手工終止腳本運行
while 1=1
begin
select getdate()
-- 從文件級看tempdb使用情況
dbcc showfilestats
-- Query 1
-- 返回所有做過空間申請的session信息
Select 'Tempdb' as DB, getdate() as Time,
SUM (user_object_reserved_page_count)*8 as user_objects_kb,
SUM (internal_object_reserved_page_count)*8 as internal_objects_kb,
SUM (version_store_reserved_page_count)*8 as version_store_kb,
SUM (unallocated_extent_page_count)*8 as freespace_kb
From sys.dm_db_file_space_usage
Where database_id = 2
-- Query 2
-- 這個管理視圖能夠反映當(dāng)時tempdb空間的總體分配
SELECT t1.session_id,
t1.internal_objects_alloc_page_count, t1.user_objects_alloc_page_count,
t1.internal_objects_dealloc_page_count , t1.user_objects_dealloc_page_count,
t3.*
from sys.dm_db_session_space_usage t1 ,
-- 反映每個session累計空間申請
sys.dm_exec_sessions as t3
-- 每個session的信息
where
t1.session_id = t3.session_id
and (t1.internal_objects_alloc_page_count>0
or t1.user_objects_alloc_page_count >0
or t1.internal_objects_dealloc_page_count>0
or t1.user_objects_dealloc_page_count>0)
-- Query 3
-- 返回正在運行并且做過空間申請的session正在運行的語句
SELECT t1.session_id,
st.text
from sys.dm_db_session_space_usage as t1,
sys.dm_exec_requests as t4
CROSS APPLY sys.dm_exec_sql_text(t4.sql_handle) AS st
where t1.session_id = t4.session_id
and t1.session_id >50
and (t1.internal_objects_alloc_page_count>0
or t1.user_objects_alloc_page_count >0
or t1.internal_objects_dealloc_page_count>0
or t1.user_objects_dealloc_page_count>0)
waitfor delay '0:0:1'
end
-
select replace(a.hostname,' ','') as hostname ,''''+replace(program_name,' ','')+'''' as program_name
-
, loginame, j.*
-
from (
-
SELECT t1.session_id, t1.internal_objects_alloc_page_count*8.0 internal_objects_alloc_KB,
-
t1.user_objects_alloc_page_count*8.0 user_objects_alloc_KB,
-
t1.internal_objects_dealloc_page_count*8.0 internal_objects_dealloc_KB,
-
t1.user_objects_dealloc_page_count*8.0 user_objects_dealloc_KB,
-
st.text
-
from sys.dm_db_session_space_usage as t1,
-
sys.dm_exec_requests as t4
-
CROSS APPLY sys.dm_exec_sql_text(t4.sql_handle) AS st
-
where t1.session_id = t4.session_id
-
and t1.session_id >50
-
and (t1.internal_objects_alloc_page_count>0
-
or t1.user_objects_alloc_page_count >0
-
or t1.internal_objects_dealloc_page_count>0
-
or t1.user_objects_dealloc_page_count>0) ) as j
-
left join
-
sys.sysprocesses as a with(nolock) on a.spid=j.session_id
-
where isnull(a.loginame,'') <>''
-
select j.session_id,replace(s.hostname,' ','') as hostname ,''''+replace(s.program_name,' ','')+'''' as program_name
-
, s.loginame, db_name(s.dbid) AS DBname,j.text from
-
-
( SELECT t1.session_id,st.text
-
from sys.dm_db_session_space_usage as t1,
-
sys.dm_exec_requests as t4
-
CROSS APPLY sys.dm_exec_sql_text(t4.sql_handle) AS st
-
-
where t1.session_id = t4.session_id
-
and t1.session_id >50
-
and (t1.internal_objects_alloc_page_count>0
-
or t1.user_objects_alloc_page_count >0
-
or t1.internal_objects_dealloc_page_count>0
-
or t1.user_objects_dealloc_page_count>0)) as j
-
-
left join
-
sys.sysprocesses as s with(nolock) on s.spid=j.session_id
-
where isnull(s.loginame,'') <>''
-
select sum(convert(float,size) * (8192/1024)/1024/1024) TMPFILE_GB, sum(convert(float,maxsize) * (8192/1024)/1024/1024) MAX_TMPFILE_GB,(sum(size)*1.0/sum(maxsize))*100 USED_PER from tempdb.dbo.sysfiles
-
where name like 'tempdev%'
-
select * from tempdb.dbo.sysfiles
-
use tempdb
-
-
-- 每隔1秒鐘運行一次,直到用戶手工終止腳本運行
-
-
while 1=1
-
-
begin
-
-
select getdate()
-
-
-- 從文件級看tempdb使用情況
-
-
dbcc showfilestats
-
-
-- Query 1
-
-
-- 返回所有做過空間申請的session信息
-
-
Select 'Tempdb' as DB, getdate() as Time,
-
-
SUM (user_object_reserved_page_count)*8 as user_objects_kb,
-
-
SUM (internal_object_reserved_page_count)*8 as internal_objects_kb,
-
-
SUM (version_store_reserved_page_count)*8 as version_store_kb,
-
-
SUM (unallocated_extent_page_count)*8 as freespace_kb
-
-
From sys.dm_db_file_space_usage
-
-
Where database_id = 2
-
-
-- Query 2
-
-
-- 這個管理視圖能夠反映當(dāng)時tempdb空間的總體分配
-
-
SELECT t1.session_id,
-
-
t1.internal_objects_alloc_page_count, t1.user_objects_alloc_page_count,
-
-
t1.internal_objects_dealloc_page_count , t1.user_objects_dealloc_page_count,
-
-
t3.*
-
-
from sys.dm_db_session_space_usage t1 ,
-
-
-- 反映每個session累計空間申請
-
-
sys.dm_exec_sessions as t3
-
-
-- 每個session的信息
-
-
where
-
-
t1.session_id = t3.session_id
-
-
and (t1.internal_objects_alloc_page_count>0
-
-
or t1.user_objects_alloc_page_count >0
-
-
or t1.internal_objects_dealloc_page_count>0
-
-
or t1.user_objects_dealloc_page_count>0)
-
-
-- Query 3
-
-
-- 返回正在運行并且做過空間申請的session正在運行的語句
-
-
SELECT t1.session_id,
-
-
st.text
-
-
from sys.dm_db_session_space_usage as t1,
-
-
sys.dm_exec_requests as t4
-
-
CROSS APPLY sys.dm_exec_sql_text(t4.sql_handle) AS st
-
-
where t1.session_id = t4.session_id
-
-
and t1.session_id >50
-
-
and (t1.internal_objects_alloc_page_count>0
-
-
or t1.user_objects_alloc_page_count >0
-
-
or t1.internal_objects_dealloc_page_count>0
-
-
or t1.user_objects_dealloc_page_count>0)
-
-
waitfor delay '0:0:1'
-
-
end