我的编程空间,编程开发者的网络收藏夹
学习永远不晚

monitor temp db usage for MSSQL的示例代码

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

monitor temp db usage for MSSQL的示例代码

这篇文章将为大家详细讲解有关monitor temp db usage for MSSQL的示例代码,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

  1. create a SP to record the temp file usage, and record the active session with SQL with requested paged from temp db

  2. USE [DBCenter]

  3. GO


  4. CREATE PROCEDURE [dba].[sys_tmp_monitor_85]

  5. AS

  6. begin

  7. truncate table dbcenter..tmpdbmonitor_SQL

  8. insert into dbcenter..tmpdbmonitor_85

  9. 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,GETDATE() [DATE_TIME]

  10.  from tempdb.dbo.sysfiles where name like 'tempdev%'


  11. insert into dbcenter..tmpdbmonitor_SQL

  12. select replace(a.hostname,' ','') as hostname ,''''+replace(program_name,' ','')+'''' as program_name

  13. , loginame,db_name(a.dbid) AS DBname, j.*,GETDATE() date_time

  14. from (

  15. SELECT t1.session_id, t1.internal_objects_alloc_page_count*8.0 internal_objects_alloc_KB,

  16. t1.user_objects_alloc_page_count*8.0 user_objects_alloc_KB,

  17. t1.internal_objects_dealloc_page_count*8.0 internal_objects_dealloc_KB,

  18. t1.user_objects_dealloc_page_count*8.0 user_objects_dealloc_KB,

  19. st.text

  20. from tempdb.sys.dm_db_session_space_usage as t1,

  21. tempdb.sys.dm_exec_requests as t4

  22. CROSS APPLY tempdb.sys.dm_exec_sql_text(t4.sql_handle) AS st

  23. where t1.session_id = t4.session_id

  24.   and t1.session_id >50

  25.   and (t1.internal_objects_alloc_page_count>0

  26.     or t1.user_objects_alloc_page_count >0

  27.     or t1.internal_objects_dealloc_page_count>0

  28.     or t1.user_objects_dealloc_page_count>0) ) as j

  29. left join

  30.     tempdb.sys.sysprocesses as a with(nolock) on a.spid=j.session_id

  31.     where isnull(a.loginame,'') <>''


  32. insert into dbcenter..tmpdbmonitor_SQL_his

  33. select replace(a.hostname,' ','') as hostname ,''''+replace(program_name,' ','')+'''' as program_name

  34. , loginame,db_name(a.dbid) AS DBname, j.*,GETDATE() date_time

  35. from (

  36. SELECT t1.session_id, t1.internal_objects_alloc_page_count*8.0 internal_objects_alloc_KB,

  37. t1.user_objects_alloc_page_count*8.0 user_objects_alloc_KB,

  38. t1.internal_objects_dealloc_page_count*8.0 internal_objects_dealloc_KB,

  39. t1.user_objects_dealloc_page_count*8.0 user_objects_dealloc_KB,

  40. st.text

  41. from tempdb.sys.dm_db_session_space_usage as t1,

  42. tempdb.sys.dm_exec_requests as t4

  43. CROSS APPLY tempdb.sys.dm_exec_sql_text(t4.sql_handle) AS st

  44. where t1.session_id = t4.session_id

  45.   and t1.session_id >50

  46.   and (t1.internal_objects_alloc_page_count>0

  47.     or t1.user_objects_alloc_page_count >0

  48.     or t1.internal_objects_dealloc_page_count>0

  49.     or t1.user_objects_dealloc_page_count>0) ) as j

  50. left join

  51.     tempdb.sys.sysprocesses as a with(nolock) on a.spid=j.session_id

  52.     where isnull(a.loginame,'') <>''


  53. end

2. create a job which will get the tmpdb usage info
   if usage percent >=85 sent out mail 

  1. declare

  2. @used_per int,

  3. @sql varchar(8000),

  4. @sbj varchar(1000)


  5. begin

  6. select @used_per=(sum(size)*1.0/sum(maxsize))*100 from tempdb.dbo.sysfiles where name like 'tempdev%'

  7. print @used_per

  8. if @used_per>=85

  9. begin

  10.     exec [DBCenter].[dba].[sys_tmp_monitor_85]

  11.     set @sql='SELECT [hostname]

  12.           ,[program_name]

  13.           ,[loginame]

  14.           ,[DBname]

  15.           ,[session_id]

  16.           ,[text]

  17.           ,[date_time]

  18.       FROM [DBCenter].[dbo].[tmpdbmonitor_SQL]'

  19.           select @sbj=@@SERVERNAME

  20.         set @sbj=@sbj+'tmpdb usage over 85 %'

  21.      --print @sql

  22.     EXEC msdb.dbo.sp_send_dbmail

  23.         @profile_name = 'monitor',

  24.         @recipients = '1234567@qq.com',

  25.         @query = @sql ,

  26.         @subject = @sbj,

  27.         @attach_query_result_as_file = 1 ;

  28. end

  29. end

关于“monitor temp db usage for MSSQL的示例代码”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

monitor temp db usage for MSSQL的示例代码

下载Word文档到电脑,方便收藏和打印~

下载Word文档

编程热搜

目录