sql-server – 如何查找导致tempdb增长的SQL语句?

前端之家收集整理的这篇文章主要介绍了sql-server – 如何查找导致tempdb增长的SQL语句?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
服务器的tempdb(sql Server 2008)每月多次增加到500GB.是否有可能找出导致此问题的sql语句?问题通常不是由create table #temp …引起的;插入#temp …或选择…到#temp …但复杂的连接.

某些tempdb文件的初始大小每次也会自动设置为更大的值.怎么预防呢?

有时缓存的计划会阻止调整/缩小文件.如何找到哪个持有tempdb?

解决方法

您可以使用三个DMV来跟踪tempdb使用情况:

> sys.dm_db_task_space_usage
> sys.dm_db_session_space_usage
> sys.dm_db_file_space_usage

前两个将允许您在查询和跟踪时跟踪分配.会话级别.第三个跟踪版本存储,用户和内部对象的分配.

以下示例查询将为您提供每个会话的分配:

SELECT
  sys.dm_exec_sessions.session_id AS [SESSION ID],DB_NAME(database_id) AS [DATABASE Name],HOST_NAME AS [System Name],program_name AS [Program Name],login_name AS [USER Name],status,cpu_time AS [cpu TIME (in milisec)],total_scheduled_time AS [Total Scheduled TIME (in milisec)],total_elapsed_time AS    [Elapsed TIME (in milisec)],(memory_usage * 8)      AS [Memory USAGE (in KB)],(user_objects_alloc_page_count * 8) AS [SPACE Allocated FOR USER Objects (in KB)],(user_objects_dealloc_page_count * 8) AS [SPACE Deallocated FOR USER Objects (in KB)],(internal_objects_alloc_page_count * 8) AS [SPACE Allocated FOR Internal Objects (in KB)],(internal_objects_dealloc_page_count * 8) AS [SPACE Deallocated FOR Internal Objects (in KB)],CASE is_user_process
             WHEN 1      THEN 'user session'
             WHEN 0      THEN 'system session'
  END         AS [SESSION Type],row_count AS [ROW COUNT]
FROM 
  sys.dm_db_session_space_usage
INNER join
  sys.dm_exec_sessions
ON  sys.dm_db_session_space_usage.session_id = sys.dm_exec_sessions.session_id

如果要跟踪一段时间内的使用情况,请考虑使用sp_whoisactive收集数据,如Kendra Little所示.

猜你在找的MsSQL相关文章