From b07b97ab37c14c1b459ffb4297da5c449d04a8e0 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 29 Nov 2017 12:00:01 +0100 Subject: [inc/Database] Do EXPLAIN SELECT at shutdown so we don't interfere with SQL_CAL_FOUND_ROWS --- inc/database.inc.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'inc') diff --git a/inc/database.inc.php b/inc/database.inc.php index 150f828a..79c945b8 100644 --- a/inc/database.inc.php +++ b/inc/database.inc.php @@ -17,6 +17,7 @@ class Database private static $statements = array(); private static $returnErrors; private static $lastError = false; + private static $explainList = array(); /** * Connect to the DB if not already connected. @@ -37,6 +38,11 @@ class Database return false; Util::traceError('Connecting to the local database failed: ' . $e->getMessage()); } + if (CONFIG_DEBUG) { + register_shutdown_function(function() { + self::examineLoggedQueries(); + }); + } return true; } @@ -115,7 +121,7 @@ class Database { self::init(); if (CONFIG_DEBUG && preg_match('/^\s*SELECT/is', $query)) { - self::explainQuery($query, $args); + self::$explainList[] = [$query, $args]; } // Support passing nested arrays for IN statements, automagically refactor self::handleArrayArgument($query, $args); @@ -141,6 +147,13 @@ class Database return false; } + public static function examineLoggedQueries() + { + foreach (self::$explainList as $e) { + self::explainQuery($e[0], $e[1]); + } + } + private static function explainQuery($query, $args) { $res = self::simpleQuery('EXPLAIN ' . $query, $args, true); @@ -155,7 +168,7 @@ class Database $lens[$key] = strlen($key); } foreach ($rows as $row) { - if (!$log && preg_match('/filesort|temporary/i', $row['Extra'])) { + if (!$log && $row['rows'] > 20 && preg_match('/filesort|temporary/i', $row['Extra'])) { $log = true; } foreach ($row as $key => $col) { -- cgit v1.2.3-55-g7522