DBA Spy Tools: Detecting Source of Queries
As a MySQL DBA I already know the data changes that happen on my system. I have logs for that.
However, it’s a common problem that several years into the life of an application, the current developers won’t know where in the codebase queries come from. It’s often hard for them to find the location in the code if queries are formed dynamically; the pattern I show them to optimize doesn’t match anything in the code.
I stumbled on a trick a couple years ago that has been invaluable in tracking down these problematic queries: query comments.
Here’s an example:
When a query generally shows up in a slow query log, it might look something like this:
# Time: 150217 10:26:01 # User@Host: comments[comments] @ localhost  Id: 13 # Query_time: 0.000231 Lock_time: 0.000108 Rows_sent: 3 Rows_examined: 3 SET timestamp=1424186761; select * from cars;
That logging shows me who executed the query (the comments user), the server it was executed from (localhost in this case), and what time it was executed (who, where, when).
What this doesn’t tell me is where in my codebase this query is. For example, if I want to change that select * to a more targeted select column1, column2, we may not know where to find it.
This is where comments help.
Comments can be any of three styles:
select * from cars; # comment to end of line here select * from cars; -- comment to end of line here select * from /* inline comment here */ cars;
When you add a comment to your query, you’ll capture the comment. Here’s an example.
On the command line:
mysql> select * from cars; # valerie - command line - testing comments
In the slow log:
# Time: 150217 11:25:24 # User@Host: comments[comments] @ localhost  Id: 3 # Query_time: 0.000246 Lock_time: 0.000119 Rows_sent: 3 Rows_examined: 3 SET timestamp=1424190324; select * from cars # valerie - command line - testing comments;
This is especially useful if you are forming queries dynamically and can add the name of the piece of code, function, user, etc. to the comment. Those comments would look something like this in the slow log:
# Time: 150217 11:32:04 # User@Host: comments[comments] @ localhost  Id: 3 # Query_time: 0.000225 Lock_time: 0.000104 Rows_sent: 3 Rows_examined: 3 SET timestamp=1424190724; select * from cars # userid 7695424 - index.php?search=true - display function;