[翻譯]TempDB剩餘空間監視與錯誤修正

來源:互聯網
上載者:User

Q:是否有辦法監視TempDB剩餘空間?是否可以控制不同的應用程式被分配多少TempDB空間,或者建立多個TempDB?

A:
不幸的是SQL Server不提供以上的功能。但SQL Server可以使用DMV來檢查當前查詢TempDB空間分配。如果您發現TempDB剩餘的空間相當少了,您可以找出消耗最大的查詢,決定終止一個或幾個查詢來回收TempDB的空間。

您可以使用如下DMV查詢:

 

Code
-- This DMV query shows currently executing tasks and 
-- tempdb space usage
-- Once you have isolated the task(s) that are generating lots 
-- of internal object allocations, 
-- you can even find out which TSQL statement and its query plan 
-- for detailed analysis

select top 10
t1.session_id, 
t1.request_id, 
t1.task_alloc,
     t1.task_dealloc,  
    (SELECT SUBSTRING(text, t2.statement_start_offset/2 + 1,
          (CASE WHEN statement_end_offset = -1 
              THEN LEN(CONVERT(nvarchar(max),text)) * 2 
                   ELSE statement_end_offset 
              END - t2.statement_start_offset)/2)
     FROM sys.dm_exec_sql_text(sql_handle)) AS query_text,
 (SELECT query_plan from sys.dm_exec_query_plan(t2.plan_handle)) as query_plan

from      (Select session_id, request_id,
sum(internal_objects_alloc_page_count +   user_objects_alloc_page_count) as task_alloc,
sum (internal_objects_dealloc_page_count + user_objects_dealloc_page_count) as task_dealloc
       from sys.dm_db_task_space_usage 
       group by session_id, request_id) as t1, 
       sys.dm_exec_requests as t2
where t1.session_id = t2.session_id and 
(t1.request_id = t2.request_id) and 
      t1.session_id > 50
order by t1.task_alloc DESC

該查詢可以顯示TempDB中消耗最高的查詢。您也可以進一步查看查詢計劃來看查詢在TempDB中申請的空間所派的用處。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.