The latest release of openark kit introduces oak-hook-general-log, a handy tool which allows for some analysis of executing queries.
Initially I just intended for the tool to be able to dump the general log to standard output, from any machine capable to connect to MySQL. Quick enough, I realized the power it brings.
With this tool, one can dump to standard output all queries using temporary tables; or using a specific index; or doing a full index scan; or just follow up on connections; or… For example, the following execution will only log queries which make for filesort:
oak-hook-general-log --user=root --host=localhost --password=123456 --filter-explain-filesort
The problem with using the standard logs
So you have the general log, which you don’t often enable, since it tends to grow huge within moments. You then have the slow log. Slow log is great, and is among the top tools for MySQL diagnosis.
The slow log allows for log-queries-not-using-indexes, which is yet another nice feature. Not only should you log any query running for over X seconds, but also log any query which does not use an index.
Wait. This logs all single-row tables (no single row table will use an index), as well as very small tables (a common 20 rows lookup table will most often be scanned). These are OK scans. This makes for some noise in the slow log.
And how about queries which do use an index, but do so poorly? They use an index, but retrieve some 12,500,000 rows, using temporary table & filesort?
What oak-hook-general-log does for you
This tool streams out the general log, and filters out queries based on their role or on their execution plan.
To work at all, it must enable the general log. Moreover, it directs the general log to log table. Mind that this makes for a performance impact, which is why the tool auto-terminates and restores original log settings (default is 1 minute, configurable). It’s really not a tool you should keep running for days. But during the few moments it runs, it will:
- Routinely rotate the mysql.general_log table so that it doesn’t fill up
- Examine entries found in the general log
- Cross reference entries to the PROCESSLIST so as to deduce database context (bug #52554)
- If required and appropriate, evaluate a query’s execution plan
- Decide whether to dump each entry based on filtering rules
Filtering rules
Filtering rules are passed as command line options. At current, only one filtering rule applies (if more than one specified only one is used, so no point in passing more than one). Some of the rules are: Continue reading » “oak-hook-general-log: your poor man’s Query Analyzer”