我有一个仓库服务器从旧系统24/7获取数据/同步,我注意到我的一些报告/ sql作业性能不确定,而且我从DBA团队那里听到我的查询阻塞的大部分时间到其他同步过程。
从DBA团队开始,我开始知道命令,即EXEC SP_WHO2,我可以通过查看列BlkBy来识别导致阻塞的查询spid。
请建议我如何避免阻止以及检查 SQL Server
中阻止的其他方法答案 0 :(得分:4)
除了Sp_Who2之外,您还可以使用以下查询来识别SQL中的阻塞。
SELECT
db.name DBName,
tl.request_session_id,
wt.blocking_session_id,
OBJECT_NAME(p.OBJECT_ID) BlockedObjectName,
tl.resource_type,
h1.TEXT AS RequestingText,
h2.TEXT AS BlockingTest,
tl.request_mode
FROM sys.dm_tran_locks AS tl
INNER JOIN sys.databases db ON db.database_id = tl.resource_database_id
INNER JOIN sys.dm_os_waiting_tasks AS wt ON tl.lock_owner_address = wt.resource_address
INNER JOIN sys.partitions AS p ON p.hobt_id = tl.resource_associated_entity_id
INNER JOIN sys.dm_exec_connections ec1 ON ec1.session_id = tl.request_session_id
INNER JOIN sys.dm_exec_connections ec2 ON ec2.session_id = wt.blocking_session_id
CROSS APPLY sys.dm_exec_sql_text(ec1.most_recent_sql_handle) AS h1
CROSS APPLY sys.dm_exec_sql_text(ec2.most_recent_sql_handle) AS h2
GO
也可以使用以下命令检查特定SPID的详细信息。
DBCC INPUTBUFFER(56) — Will give you the Event Info.
KILL 56 -- Will kill the session of this id.
答案 1 :(得分:3)
This是一本非常全面的指南。一些基本指导方针:
SELECT ... INTO #temp
模式,而是首先创建一个表并使用INSERT INTO #Temp SELECT...
WITH (NOLOCK)
WHERE
条款中使用sargable谓词READ_COMMITTED_SNAPSHOT
隔离级别答案 2 :(得分:1)
还有另一个有用的查询。该查询将为您提供有关阻塞会话ID,时间(从阻塞会话运行时开始),执行查询,正在执行此阻塞会话的用户帐户的重要详细信息。当您在生产环境中有许多用户正在运行他们的事务时,该查询非常有用,您应该找出阻塞的会话和用户并采取必要的措施。
SELECT
[s_tst].[session_id],
[s_es].[login_name] AS [Login Name],
DB_NAME (s_tdt.database_id) AS [Database],
[s_tdt].[database_transaction_begin_time] AS [Begin Time],
[s_tdt].[database_transaction_log_bytes_used] AS [Log Bytes],
[s_tdt].[database_transaction_log_bytes_reserved] AS [Log Rsvd],
[s_est].text AS [Last T-SQL Text],
[s_eqp].[query_plan] AS [Last Plan]
FROM
sys.dm_tran_database_transactions [s_tdt]
JOIN
sys.dm_tran_session_transactions [s_tst]
ON
[s_tst].[transaction_id] = [s_tdt].[transaction_id]
JOIN
sys.[dm_exec_sessions] [s_es]
ON
[s_es].[session_id] = [s_tst].[session_id]
JOIN
sys.dm_exec_connections [s_ec]
ON
[s_ec].[session_id] = [s_tst].[session_id]
LEFT OUTER JOIN
sys.dm_exec_requests [s_er]
ON
[s_er].[session_id] = [s_tst].[session_id]
CROSS APPLY
sys.dm_exec_sql_text ([s_ec].[most_recent_sql_handle]) AS [s_est]
OUTER APPLY
sys.dm_exec_query_plan ([s_er].[plan_handle]) AS [s_eqp]
ORDER BY
[Begin Time] ASC;
GO