Monitoring and profiling in MySQL - Time & Space Complexity
Monitoring and profiling help us see how long database queries take and where time is spent.
We want to know how the time to run queries changes as data grows.
Analyze the time complexity of the following MySQL query with profiling enabled.
SET profiling = 1;
SELECT * FROM orders WHERE customer_id = 123;
SHOW PROFILE FOR QUERY 1;
This code turns on profiling, runs a query to get orders for one customer, then shows the time spent on each step.
Look at what repeats when the query runs.
- Primary operation: Scanning the orders table rows to find matching customer_id.
- How many times: Once per row in the orders table (depends on table size).
As the number of orders grows, the time to scan grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 row checks |
| 100 | 100 row checks |
| 1000 | 1000 row checks |
Pattern observation: The time grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the query time grows linearly as the number of rows increases.
[X] Wrong: "Profiling shows constant time no matter the data size."
[OK] Correct: Profiling measures actual time, which grows with data size if no index is used.
Knowing how query time grows helps you explain performance issues and improvements clearly.
"What if we add an index on customer_id? How would the time complexity change?"
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
