Active Sessions: Know Who Is In Your Database Right Now
Connection count is a lagging indicator. By the time an alert fires, you are already in trouble.
The better habit is checking active sessions as part of your routine — before deployments, during slow periods, whenever something feels off. What queries are running? Who has been sitting in a transaction for three minutes? Which application server is holding ten connections open and doing nothing?
This script answers all of that in one pass.
ActiveSessionsandThreads.sql
Loading…
Reading the Results
Active Queries
| Column | What to Check |
|---|---|
user | Which app user or service owns the connection |
host | Source IP or hostname — useful for tracing back to an app server |
db | Which database the session is attached to |
command | Query = actively running; Sleep = idle (filtered out above) |
seconds | How long the current command has been running |
state | Internal execution stage — e.g. Sending data, Waiting for lock |
query | The actual SQL being executed (may be truncated) |
Thread and Connection Metrics
| Variable | What It Tells You |
|---|---|
Threads_connected | Total open connections right now |
Threads_running | Connections actively executing — the one that matters under load |
Threads_cached | Connections held in the thread cache for reuse |
Threads_created | Cumulative thread creates — high values mean cache is too small |
Max_used_connections | Peak concurrent connections since last restart |
max_connections | The configured ceiling — compare against Max_used_connections |
What to Watch For
| Signal | What It Means |
|---|---|
Long-running queries (high seconds) | Slow query, missing index, or a stuck transaction |
state = Waiting for lock | A blocking transaction is holding a lock |
Threads_running close to Threads_connected | Very few idle connections — server is under pressure |
Max_used_connections near max_connections | You are close to the connection ceiling — risk of refused connections |
| Many connections from one host | Application connection pool misconfigured or leaking |
command = Killed | A query was explicitly killed — worth investigating why |
Gareth Winterman