// SQL snippet

Find the sessions doing the most I/O in Oracle

Which session is doing the most I/O? v$sess_io ranks every connected session by its I/O since it logged on.

Main statement
Oracle
Couvre également
MySQL

The task

When storage is saturated, the first question is which session is responsible. v$sess_io accumulates logical reads (block_gets plus consistent_gets) and physical_reads per session since logon. Joined to v$session it gains the username, machine, module, status and current sql_id, which is enough to find the owner and the statement.

The snippet

Written for Oracle. Versions for MySQL are under Variations.

Oracle
SELECT s.sid, s.serial#, s.username, s.status, s.machine, s.module,
       s.sql_id, s.last_call_et AS seconds_in_status,
       io.block_gets, io.consistent_gets, io.physical_reads
FROM v$session s
JOIN v$sess_io io ON io.sid = s.sid
WHERE s.username IS NOT NULL
  AND s.username NOT IN ('SYS', 'SYSTEM')
ORDER BY io.physical_reads DESC
FETCH FIRST 20 ROWS ONLY;

Fonctionnement

consistent_gets are block reads in consistent (read-consistent) mode and block_gets are reads in current mode, the kind used to modify a block; together they are the session's logical reads. physical_reads are blocks read from disk, the number that matters when I/O is the bottleneck. last_call_et is the seconds since the session's status last changed: for an ACTIVE session, how long the current call has been running, and for an INACTIVE one, how long it has been idle. An ACTIVE session with a large last_call_et and climbing physical_reads is running a long, I/O-heavy statement right now. SYS and SYSTEM are filtered out to reduce noise; the variant resolves the session's current statement through sql_id. Querying v$ views as a normal user needs SELECT_CATALOG_ROLE, SELECT ANY DICTIONARY or a direct grant on the underlying v_$ views.

Variations

Resolve the current SQL for one session

SELECT s.sid, s.username, sq.sql_text, sq.executions,
       sq.disk_reads, sq.buffer_gets
FROM v$session s
JOIN v$sqlarea sq ON sq.sql_id = s.sql_id
WHERE s.sid = 123;  -- the sid from the main query

MySQL: file I/O latency per thread from the sys schema

MySQL
SELECT user, processlist_id, total AS io_events,
       total_latency, avg_latency, max_latency
FROM sys.io_by_thread_by_latency
ORDER BY total DESC
LIMIT 20;

Questions that come up

Physical reads are high but the buffer cache is large. What does that mean?

Either the data the session reads does not fit in the cache, or it is reading with direct path reads, which bypass the buffer cache entirely. Oracle uses direct path reads for parallel query and often for serial full scans of large tables, so a big full scan shows up as physical reads however large the cache is. Look at the statement: a full scan of a large table with a selective WHERE clause is the classic case an index fixes.

The counters are cumulative since logon. How do I see recent activity?

Snapshot v$sess_io twice a few minutes apart and compare, because a pooled connection that has been logged on for a week has a week of totals. For recent statement-level history, v$active_session_history samples active sessions every second, but it requires the Diagnostics Pack license on Enterprise Edition.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.