SELECT status, COUNT(*) AS sessionsFROM sys.dm_exec_sessionsWHERE is_user_process = 1GROUP BY statusORDER BY sessions DESCThis query is part of the "SQL Server health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.
What the query shows
This query counts the user sessions on the server, grouped by their status, using sys.dm_exec_sessions. The filter is_user_process = 1 leaves system sessions out. The result is a handful of rows, which makes it a good cheap health check.
Run it when the application reports connection errors, when you suspect a connection leak or a pool that is too large, or to get a feel for how busy the server is.
How to read each column
- status: running means the session is executing one or more requests. sleeping means it is connected and has no request running. dormant means the session was reset by connection pooling and is waiting in a pre-login state. preconnect means it is in the Resource Governor classifier.
- sessions: how many sessions have that status right now.
Healthy and worrying results
Most sessions sleeping is normal. An application that uses a connection pool keeps connections open and idle between requests. Running should be a small number, close to the number of queries really executing at that moment.
Worry about trends, not about one reading. As a rule of thumb, a sleeping count that climbs for hours and never comes down suggests a connection leak. A running count that stays above the number of CPU cores for long suggests the server is saturated. Compare with the maximum allowed.
SELECT @@MAX_CONNECTIONS AS max_connections;What to do about it
To find who owns the sessions, group by login, host and program.
SELECT login_name, host_name, program_name, status, COUNT(*) AS sessionsFROM sys.dm_exec_sessionsWHERE is_user_process = 1GROUP BY login_name, host_name, program_name, statusORDER BY sessions DESC;A sleeping session that holds an open transaction is the dangerous kind, because its locks block others. This query finds them.
SELECT s.session_id, s.login_name, s.program_name, s.last_request_end_timeFROM sys.dm_exec_sessions sWHERE s.is_user_process = 1 AND s.status = 'sleeping' AND s.open_transaction_count > 0;If one application owns thousands of sleeping sessions, lower the maximum pool size in its connection string and make sure the code closes connections. Tuning the pool is almost always better than raising the server limit.
Limits to know
- It counts sessions, not requests. One running session may be one query or a batch of several statements.
- It is a snapshot. A burst of connections that opens and closes between runs does not show.
- Seeing sessions from other users needs VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE on SQL Server 2022 and later). Without it you see only your own session.
- On Azure SQL Database it needs VIEW DATABASE STATE and shows only the connections to that database.
- The status value is the one reported by the server. Do not compare it with the text of a request status in sys.dm_exec_requests, which has other values.
Run it on a schedule in DataRunner
Schedule this query every hour or so in DataRunner and send it by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets. The run history of the schedule gives you the series you need to tell a pool that is large and stable from a leak that grows.