3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET')) {
25 * Check DB queries for filesorts and such and log em.
27 * @package SQLProfilePlugin
28 * @maintainer Brion Vibber <brion@status.net>
30 class SQLProfilePlugin extends Plugin
32 private $recursionGuard = false;
34 function onPluginVersion(&$versions)
36 $versions[] = array('name' => 'SQLProfile',
37 'version' => STATUSNET_VERSION,
38 'author' => 'Brion Vibber',
39 'homepage' => 'http://status.net/wiki/Plugin:SQLProfile',
41 _m('Debug tool to watch for poorly indexed DB queries.'));
46 function onStartDBQuery($obj, $query, &$result)
48 if (!$this->recursionGuard && preg_match('/\bselect\b/i', $query)) {
49 $this->recursionGuard = true;
51 $explain = $xobj->query('EXPLAIN ' . $query);
52 $this->recursionGuard = false;
54 while ($xobj->fetch()) {
55 $extra = $xobj->Extra;
56 $evil = (strpos($extra, 'Using filesort') !== false) ||
57 (strpos($extra, 'Using temporary') !== false);
59 $xquery = $xobj->sanitizeQuery($query);
60 common_log(LOG_DEBUG, "$extra | $xquery");