Answers:
是否可以查看在执行查询期间获取的锁以及类型?
是的,为了确定锁,
您可以使用beta_lockinfoErland Sommarskog
beta_lockinfo
是一个存储过程,提供有关进程及其持有的锁以及活动事务的信息。beta_lockinfo
旨在收集有关阻塞情况的尽可能多的信息,以便您可以立即找到罪魁祸首,并在情况紧急的情况下终止阻塞过程。然后,您可以坐下来分析输出,beta_lockinfo
以了解阻塞情况是如何产生的,并确定采取什么措施来防止再次发生该情况。的输出beta_lockinfo
显示所有带锁的活动进程和被动进程,它们锁定了哪些对象,它们上次提交了什么命令以及正在执行哪些语句。您还将获得当前语句的查询计划。通常情况下,您运行beta_lockinfo
直接查看输出,但是还有一个存档模式,其中数据保存到表中。这不是最没有用的,如果您希望某人将您beta_lockinfo
无法访问的站点的输出发送给您。
另一种方法是sp_whoIsActive由Adam Machanic与@get_locks = 1
EXEC sp_WhoIsActive
@filter = '',
@filter_type = 'session',
@not_filter = '',
@not_filter_type = 'session',
@show_own_spid = 0,
@show_system_spids = 0,
@show_sleeping_spids = 1,
@get_full_inner_text = 0,
@get_plans = 1,
@get_outer_command = 1,
@get_transaction_info = 0,
@get_task_info = 1,
@get_locks = 1, ----------> 1 = ON (get lock info); 0 = OFF
@get_avg_time = 0,
@get_additional_info = 0,
@find_block_leaders = 0,
@delta_interval = 0,
@output_column_list = '[dd%][session_id][sql_text][sql_command][login_name][wait_info][tasks][tran_log%][cpu%][temp%][block%][reads%][writes%][context%][physical%][query_plan][locks][%]',
@sort_order = '[start_time] ASC',
@format_output = 1,
@destination_table = '',
@return_schema = 0,
@schema = NULL,
@help = 0
这是我按进程/表/锁类型查看锁的方式:
SELECT
HostName,
"OS UserName",
Login,
spid,
"Database",
TableID,
"Table Name_________",
IndID,
-- [Index Name],
"Lock Type",
"Lock Mode",
Status,
-- Resource,
Count(*) AS "Lock Count"
FROM (
SELECT
Convert(VarChar(30), RTRIM(P.HostName)) AS HostName,
Convert(VarChar(30), RTRIM(P.nt_UserName)) AS "OS UserName",
Convert(VarChar(30), Suser_SName(p.sid)) AS Login,
Convert(SmallInt, req_spid) AS spid,
Convert(VarChar(30), DB_Name(rsc_dbid)) AS "Database",
rsc_objid AS TableID,
Convert(VarChar(30), Object_Name(rsc_objid, rsc_dbid))
AS [Table Name_________],
rsc_indid AS IndID,
CASE SubString (lock_type.name, 1, 4)
When '' Then 'None'
When 'DB' Then 'Database'
When 'FIL' Then 'File'
When 'IDX' Then 'Index'
When 'TAB' Then 'Table'
When 'PAG' Then 'Page'
When 'KEY' Then 'Key'
When 'EXT' Then 'Extent'
When 'RID' Then 'Row ID'
When 'APP' Then 'Application'
Else SubString (lock_type.name, 1, 4)
END AS "Lock Type",
Case SubString (lock_mode.name, 1, 12)
When NULL Then 'N/A'
When 'Sch-S' Then 'SCHEMA (Stability)'--'SCHEMA stability lock'
When 'Sch-M' Then 'SCHEMA (Modification)'--'SCHEMA modification lock'
When 'S' Then 'SHARED'--'SHARED Lock acquisition'
When 'U' Then 'UPDATE'--'UPDATE lock acquisition'
When 'X' Then 'EXCLUSIVE'--'EXCLUSIVE lock granted'
When 'IS' Then 'SHARED (Intent)'--'INTENT for SHARED lock'
When 'IU' Then 'UPDATE (Intent)'--'INTENT for UPDATE lock'
When 'IX' Then 'EXCLUSIVE (Intent)'--'INTENT for EXCLUSIVE lock'
When 'SIU' Then 'SHARED (Intent UPDATE)'--'SHARED lock with INTENT for UPDATE'
When 'SIX' Then 'SHARED (Intent EXCLUSIVE)'--'SHARED lock with INTENT for EXCLUSIVE'
When 'UIX' Then 'UPDATE'--'UPDATE lock with INTENT for EXCLUSIVE'
When 'BU' Then 'UPDATE (BULK)'--'BULK UPDATE lock'
Else SubString (lock_mode.name, 1, 12)
END AS "Lock Mode",
SubString(lock_status.name, 1, 5) AS Status,
SubString (rsc_text, 1, 16) AS Resource
FROM
Master..SysLockInfo S
JOIN Master..spt_values lock_type on S.rsc_type = lock_type.number
JOIN Master..spt_values lock_status on S.req_status = lock_status.number
JOIN Master..spt_values lock_mode on S.req_mode = lock_mode.number -1
JOIN Master..SysProcesses P on S.req_spid = P.spid
WHERE
lock_type.type = 'LR'
AND lock_status.type = 'LS'
AND lock_mode.type = 'L'
AND DB_Name(rsc_dbid) NOT IN ('master', 'msdb', 'model')
) AS X
WHERE TableID > 0
GROUP BY
HostName,
"OS UserName",
Login,
spid,
"Database",
TableID,
"Table Name_________",
IndID,
"Lock Type",
"Lock Mode",
Status
ORDER BY
spid, "Database", "Table Name_________", "Lock Type", Login