Monitoring and profiling help you see how your database works and find slow parts. This makes your database faster and more reliable.
Monitoring and profiling in MySQL
SHOW STATUS; SHOW PROCESSLIST; SHOW PROFILE; SHOW PROFILES;
SHOW STATUS shows server status variables.
SHOW PROCESSLIST shows current running queries.
SHOW PROFILE shows detailed info about a query's execution.
SHOW STATUS LIKE 'Threads_connected';
SHOW PROCESSLIST;
SET profiling = 1; SELECT * FROM users WHERE id = 1; SHOW PROFILES;
This example turns on profiling, runs a query that waits 1 second, then shows the profile with execution time.
SET profiling = 1; SELECT SLEEP(1); SHOW PROFILES;
Profiling is off by default; you must enable it with SET profiling = 1;.
Profiling works per session, so it only shows queries run in the current connection.
Use SHOW PROCESSLIST to find queries that are stuck or running too long.
Monitoring shows the current state of your database server.
Profiling helps measure how long queries take to run.
Use these tools to find and fix slow or problematic queries.
Practice
SHOW PROCESSLIST in MySQL monitoring?Solution
Step 1: Understand the command purpose
SHOW PROCESSLISTlists all active threads and queries running on the MySQL server.Step 2: Identify what it displays
It shows query IDs, user, host, command, time, and status of each running query.Final Answer:
To display currently running queries and their status -> Option AQuick Check:
SHOW PROCESSLIST = Running queries info [OK]
- Confusing it with user listing commands
- Thinking it shows database structure
- Assuming it performs backups
Solution
Step 1: Recall the correct syntax for enabling slow query log
MySQL uses numeric values 0 (off) and 1 (on) for boolean system variables.Step 2: Check each option
SET GLOBAL slow_query_log = 1; usesSET GLOBAL slow_query_log = 1;which is correct syntax to enable the slow query log.Final Answer:
SET GLOBAL slow_query_log = 1; -> Option BQuick Check:
Enable slow query log with 1, not ON [OK]
- Using ON instead of 1
- Using ENABLE or START commands which don't exist
- Confusing session and global variables
SHOW STATUS LIKE 'Slow_queries';
What does the output number represent?
Solution
Step 1: Understand the status variable 'Slow_queries'
This variable counts how many queries exceeded the slow query time limit since the server started.Step 2: Interpret the output meaning
The number is cumulative, showing total slow queries, not current or max time.Final Answer:
The number of queries that took longer than the slow query time threshold -> Option DQuick Check:
Slow_queries = count of slow queries [OK]
- Thinking it shows current running slow queries
- Confusing it with total queries count
- Assuming it shows max query time
SHOW PROFILE; but get an error saying profiling is disabled. What should you do to fix this?Solution
Step 1: Understand profiling status
Profiling is off by default and must be enabled per session withSET profiling = 1;.Step 2: Identify correct enabling method
RunSET profiling = 1;before running queries correctly enables profiling for the current session before running queries.Final Answer:
Run SET profiling = 1; before running queries -> Option AQuick Check:
Enable profiling with SET profiling = 1 [OK]
- Trying to enable profiling globally or via restart
- Using ENABLE PROFILING command which doesn't exist
- Assuming profiling is permanently on
Solution
Step 1: Enable slow query log and log to table
Setslow_query_log = 1andlog_output = 'TABLE'to log slow queries intomysql.slow_logtable.Step 2: Query the slow_log table for top slow queries
RunSELECT * FROM mysql.slow_log ORDER BY query_time DESC LIMIT 3;to get the slowest queries.Final Answer:
Enable slow query log, then query the mysql.slow_log table ordering by query_time DESC LIMIT 3 -> Option CQuick Check:
Slow query log table + ORDER BY query_time DESC = top slow queries [OK]
- Using SHOW PROCESSLIST which shows only current queries
- Using EXPLAIN which analyzes query plan, not runtime
- Restarting server unnecessarily for logging
Ready to prove it?
Pass the test and this pattern moves unknown
