服务器的 tempdb (SQL Server 2008) 每月数次增加到 500GB+。是否可以找出导致此问题的 SQL 语句?问题通常不是由复杂连接引起的,create table #temp...; insert into #temp...而是由select ... into #temp...复杂连接引起的。
create table #temp...; insert into #temp...
select ... into #temp...
一些 tempdb 文件的初始大小也每次都会自动设置为更大的值。如何预防?
有时缓存的计划会阻止调整文件大小/缩小文件。如何找到哪个持有 tempdb?
您可以使用三个 DMV 来跟踪 tempdb 的使用情况:
前两个将允许您在查询和会话级别跟踪分配。第三个跟踪跨版本存储、用户和内部对象的分配。
以下示例查询将为您提供每个会话的分配:
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所示。