From: Brion Vibber Date: Fri, 17 Dec 2010 19:41:40 +0000 (-0800) Subject: SQLProfile: quickie plugin to run DB queries through 'explain' and log ones that... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=66b89de256ee5cf875e621b586381a29f6d418b1;p=quix0rs-gnu-social.git SQLProfile: quickie plugin to run DB queries through 'explain' and log ones that trigger filesort or temporary table --- 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; + } +}