From 66b89de256ee5cf875e621b586381a29f6d418b1 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Fri, 17 Dec 2010 11:41:40 -0800 Subject: [PATCH] SQLProfile: quickie plugin to run DB queries through 'explain' and log ones that trigger filesort or temporary table --- plugins/SQLProfile/SQLProfilePlugin.php | 66 +++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 plugins/SQLProfile/SQLProfilePlugin.php diff --git a/plugins/SQLProfile/SQLProfilePlugin.php b/plugins/SQLProfile/SQLProfilePlugin.php new file mode 100644 index 0000000000..035c56c280 --- /dev/null +++ b/plugins/SQLProfile/SQLProfilePlugin.php @@ -0,0 +1,66 @@ +. + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Check DB queries for filesorts and such and log em. + * + * @package SQLProfilePlugin + * @maintainer Brion Vibber + */ +class SQLProfilePlugin extends Plugin +{ + private $recursionGuard = false; + + function onPluginVersion(&$versions) + { + $versions[] = array('name' => 'SQLProfile', + 'version' => STATUSNET_VERSION, + 'author' => 'Brion Vibber', + 'homepage' => 'http://status.net/wiki/Plugin:SQLProfile', + 'rawdescription' => + _m('Debug tool to watch for poorly indexed DB queries')); + + return true; + } + + function onStartDBQuery($obj, $query, &$result) + { + if (!$this->recursionGuard) { + $this->recursionGuard = true; + $xobj = clone($obj); + $explain = $xobj->query('EXPLAIN ' . $query); + $this->recursionGuard = false; + + while ($xobj->fetch()) { + $extra = $xobj->Extra; + $evil = (strpos($extra, 'Using filesort') !== false) || + (strpos($extra, 'Using temporary') !== false); + if ($evil) { + $xquery = $xobj->sanitizeQuery($query); + common_log(LOG_DEBUG, "$extra | $xquery"); + } + } + } + return true; + } +} -- 2.39.5