DBAtlas · Database operations atlas

Trace a session / query (enable trace / profiling)

Oracle

-- For a specific session (10046 event tracing):
BEGIN
  DBMS_MONITOR.SESSION_TRACE_ENABLE(session_id => 123, serial_num => 4567, waits => TRUE, binds => TRUE);
END;
/
-- To disable: DBMS_MONITOR.SESSION_TRACE_DISABLE(123, 4567);

DBMS_MONITOR.SESSION_TRACE_ENABLE produces a 10046 trace file for the session; always disable it when done (tracing adds overhead and disk usage).

PostgreSQL

LOAD 'auto_explain';
SET auto_explain.log_min_duration = 0;
SET auto_explain.log_analyze = true;
-- Simple alternative: SET log_min_duration_statement = 0;

auto_explain writes query plans, and log_min_duration_statement writes slow/all queries, to the log file; a value of 0 logs every query, so watch the log volume.

MySQL

-- performance_schema approach (recommended):
UPDATE performance_schema.setup_consumers SET ENABLED='YES' WHERE NAME LIKE 'events_statements%';
-- Legacy/simple method (being deprecated):
SET profiling = 1;
-- run the query, then:
SHOW PROFILES;

Query tracing is done with performance_schema events_statements; the SET profiling method is old (deprecated) but gives a quick per-session measurement.

SQL Server

CREATE EVENT SESSION [trace_slow] ON SERVER
ADD EVENT sqlserver.sql_statement_completed(
  WHERE duration > 1000000)
ADD TARGET package0.event_file(SET filename=N'trace_slow.xel');
GO
ALTER EVENT SESSION [trace_slow] ON SERVER STATE = START;

Extended Events (XEvents) is the modern tracing method; create the session and start it with START, and stop it with STOP/DROP when done.

MongoDB

db.setProfilingLevel(2)
// 1 = slow operations only (slowms threshold), 0 = off
// Results: db.system.profile.find().sort({ ts: -1 }).limit(10)

setProfilingLevel(2) records all operations in the system.profile collection; in production 1 (slow only) is preferred, as level 2 adds overhead.

Redis

MONITOR
# Streams all commands arriving at the server in real time. Exit: Ctrl-C

MONITOR shows all commands live but causes a serious performance drop; use it briefly and carefully in production. Alternative: SLOWLOG GET.

Elasticsearch

GET my-index/_search
{
  "profile": true,
  "query": { "match": { "title": "elastic" } }
}
// Persistent slow query tracing: the index.search.slowlog.threshold.query.warn setting

"profile": true gives the query/aggregation timing breakdown of a single search; for continuous monitoring, configure slowlog thresholds.

Open in DBAtlas →