我的SQL Server达到了并发连接的最大限制。我有很多不同的服务器和服务同时连接到一个SQL Server。
我确实找到了另一个似乎有用的查询:
SELECT DB_NAME(dbid) AS DBName,
COUNT(dbid) AS NumberOfConnections,
loginame AS LoginName,
nt_domain AS NT_Domain,
nt_username AS NT_UserName,
hostname AS HostName
FROM sys.sysprocesses
WHERE dbid > 0
GROUP BY dbid,
hostname,
loginame,
nt_domain,
nt_username
ORDER BY NumberOfConnections DESC;
然而,这给了我很好的连接数。所以我发现另一个查询似乎吐出了正在运行的sql语句等。
SELECT
SPID = er.session_id
,STATUS = ses.STATUS
,[Login] = ses.login_name
,Host = ses.host_name
,BlkBy = er.blocking_session_id
,DBName = DB_Name(er.database_id)
,CommandType = er.command
,SQLStatement = st.text
,ObjectName = OBJECT_NAME(st.objectid)
,ElapsedMS = er.total_elapsed_time
,CPUTime = er.cpu_time
,IOReads = er.logical_reads + er.reads
,IOWrites = er.writes
,LastWaitType = er.last_wait_type
,StartTime = er.start_time
,Protocol = con.net_transport
,ConnectionWrites = con.num_writes
,ConnectionReads = con.num_reads
,ClientAddress = con.client_net_address
,Authentication = con.auth_scheme
FROM sys.dm_exec_requests er
OUTER APPLY sys.dm_exec_sql_text(er.sql_handle) st
LEFT JOIN sys.dm_exec_sessions ses
ON ses.session_id = er.session_id
LEFT JOIN sys.dm_exec_connections con
ON con.session_id = ses.session_id
我如何将这两个查询合并在一起?我相信这两个问题一起会给我我需要的东西。
答案 0 :(得分:1)
不完全确定你想要的输出;你可以通过stright join session_id = spid加入两个查询(没有组)。
SELECT
spr.loginame as LoginName,
spr.nt_domain AS NT_Domain,
spr.nt_username AS NT_UserName,
spr.hostname AS HostName,
STATUS = ses.STATUS
,[Login] = ses.login_name
,Host = ses.host_name
,BlkBy = er.blocking_session_id
,DBName = DB_Name(er.database_id)
,CommandType = er.command
,SQLStatement = st.text
,ObjectName = OBJECT_NAME(st.objectid)
,ElapsedMS = er.total_elapsed_time
,CPUTime = er.cpu_time
,IOReads = er.logical_reads + er.reads
,IOWrites = er.writes
,LastWaitType = er.last_wait_type
,StartTime = er.start_time
,Protocol = con.net_transport
,ConnectionWrites = con.num_writes
,ConnectionReads = con.num_reads
,ClientAddress = con.client_net_address
,Authentication = con.auth_scheme
FROM [sys.dm_exec_requests][1] er
OUTER APPLY sys.dm_exec_sql_text(er.sql_handle) st
LEFT JOIN sys.dm_exec_sessions ses
ON ses.session_id = er.session_id
LEFT JOIN sys.dm_exec_connections con
ON con.session_id = ses.session_id
left outer join sys.sysprocesses spr
on er.session_id = spr.spid
答案 1 :(得分:1)
如果您关注的是连接数,我不确定除了sys.dm_exec_connections之外您还需要知道什么。无论哪种方式,这是我在查看实例上运行的代码时使用的代码,它还会显示哪个作业正在运行该语句(如果它是一个作业):
SELECT
SPID = er.session_id
, Status = ses.status
, [Login] = ses.login_name
, Host = ses.host_name
, BlkBy = er.blocking_session_id
, DBName = DB_Name(er.database_id)
, CommandType = er.command
, SQLStatement =
SUBSTRING
(
qt.text,
er.statement_start_offset/2,
(CASE WHEN er.statement_end_offset = -1
THEN LEN(CONVERT(nvarchar(MAX), qt.text)) * 2
ELSE er.statement_end_offset
END - er.statement_start_offset)/2
)
, sj.name + SUBSTRING(ses.program_name,65,8) JobName
, ObjectName = OBJECT_SCHEMA_NAME(qt.objectid,dbid) + '.' + OBJECT_NAME(qt.objectid, qt.dbid)
, ElapsedMS = er.total_elapsed_time
, CPUTime = er.cpu_time
, IOReads = er.logical_reads + er.reads
, IOWrites = er.writes
, LastWaitType = er.last_wait_type
, StartTime = er.start_time
, Protocol = con.net_transport
, transaction_isolation =
CASE ses.transaction_isolation_level
WHEN 0 THEN 'Unspecified'
WHEN 1 THEN 'Read Uncommitted'
WHEN 2 THEN 'Read Committed'
WHEN 3 THEN 'Repeatable'
WHEN 4 THEN 'Serializable'
WHEN 5 THEN 'Snapshot'
END
, ConnectionWrites = con.num_writes
, ConnectionReads = con.num_reads
, ClientAddress = con.client_net_address
, Authentication = con.auth_scheme
FROM sys.dm_exec_requests er
LEFT OUTER JOIN sys.dm_exec_sessions ses
ON ses.session_id = er.session_id
LEFT OUTER JOIN sys.dm_exec_connections con
ON con.session_id = ses.session_id
LEFT OUTER JOIN msdb..sysjobs sj
ON upper(convert(varchar(34), master.dbo.fn_varbintohexstr(convert(varbinary(16), sj.job_id)))) = LTRIM(RTRIM(SUBSTRING(ses.program_name,29,36)))
OUTER APPLY sys.dm_exec_sql_text(er.sql_handle) as qt
WHERE er.session_id > 50
ORDER BY
er.blocking_session_id DESC
,er.session_id