gpt4 book ai didi

sql-server-2005 - 限制 SQL Tempdb 增长

转载 作者:行者123 更新时间:2023-12-04 06:58:11 25 4
gpt4 key购买 nike

我的生产服务器面临一个严重的问题,临时数据库呈指数级增长。有什么方法可以在不重启 SQL 服务的情况下恢复临时数据库空间?

干杯坎南。

最佳答案

我会忽略建议您更改恢复模型或限制 tempDB(!) 大小的帖子。

您需要追踪增长的实际原因。

如果您打开了默认跟踪(默认情况下打开,开箱即用),您可以通过运行以下命令追溯找出导致增长的原因:

--check if default trace is enabled
if exists (select 1 from sys.configurations where configuration_id = 1568)
BEGIN

declare @defaultTraceFilepath nvarchar(256)

--get the current trace rollover file
select @defaultTraceFilepath = CONVERT(varchar(256), value) from ::fn_trace_getinfo(0)
where property = 2

SELECT ntusername,loginname, objectname, e.category_id, textdata, starttime,spid,hostname, eventclass,databasename, e.name
FROM ::fn_trace_gettable(@defaultTraceFilepath,0)
inner join sys.trace_events e
on eventclass = trace_event_id
INNER JOIN sys.trace_categories AS cat
ON e.category_id = cat.category_id
where
databasename = 'tempDB' and
cat.category_id = 2 and --database category
e.trace_event_id in (92,93) --db file growth

END

否则,您可以启动 SQL 事件探查器跟踪来捕获这些事件。打开自动增长事件、排序警告和连接警告的捕获,并查找交叉连接、散列连接或丢失的连接条件。

SQL Server 公开了一种通过当前执行的查询使用 DMV 来识别 tempDB 空间分配的方法:

-- 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 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

( Ref .)

关于sql-server-2005 - 限制 SQL Tempdb 增长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1762082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com