From b5ae50454d459013af1f70d77ec9d9cee72efbcd Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Mon, 20 Mar 2023 19:31:18 +0000 Subject: [PATCH] Optimize getTrackingIcon Signed-off-by: Kamil Tekiela --- .../Database/StructureController.php | 13 +++--- libraries/classes/Tracker.php | 42 +++++++++++++++++++ phpstan-baseline.neon | 10 +++++ psalm-baseline.xml | 6 ++- 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/libraries/classes/Controllers/Database/StructureController.php b/libraries/classes/Controllers/Database/StructureController.php index 59ed4a2a0860..991676d92eed 100644 --- a/libraries/classes/Controllers/Database/StructureController.php +++ b/libraries/classes/Controllers/Database/StructureController.php @@ -242,6 +242,7 @@ protected function displayTableList($replicaInfo): string $hiddenFields = []; $overallApproxRows = false; $structureTableRows = []; + $trackedTables = Tracker::getTrackedTables($GLOBALS['db']); foreach ($this->tables as $currentTable) { // Get valid statistics whatever is the table type @@ -417,7 +418,7 @@ protected function displayTableList($replicaInfo): string ) ) ), - 'tracking_icon' => $this->getTrackingIcon($truename), + 'tracking_icon' => $this->getTrackingIcon($truename, $trackedTables[$truename] ?? null), 'server_replica_status' => $replicaInfo['status'], 'table_url_params' => $tableUrlParams, 'db_is_system_schema' => $this->dbIsSystemSchema, @@ -521,20 +522,20 @@ protected function displayTableList($replicaInfo): string /** * Returns the tracking icon if the table is tracked * - * @param string $table table name + * @param string $table table name + * @param array|null $trackedTable * * @return string HTML for tracking icon */ - protected function getTrackingIcon(string $table): string + protected function getTrackingIcon(string $table, $trackedTable): string { $trackingIcon = ''; if (Tracker::isActive()) { - $isTracked = Tracker::isTracked($this->db, $table); - if ($isTracked || Tracker::getVersion($this->db, $table) > 0) { + if ($trackedTable !== null) { $trackingIcon = $this->template->render('database/structure/tracking_icon', [ 'db' => $this->db, 'table' => $table, - 'is_tracked' => $isTracked, + 'is_tracked' => $trackedTable['active'], ]); } } diff --git a/libraries/classes/Tracker.php b/libraries/classes/Tracker.php index 7eab74b63e43..2c4bed9e7f38 100644 --- a/libraries/classes/Tracker.php +++ b/libraries/classes/Tracker.php @@ -973,4 +973,46 @@ private static function isAnyTrackingInProgress( return $dbi->queryAsControlUser($sqlQuery)->fetchValue() !== false; } + + /** + * THIS IS TEMPORARY FIX for performance issues in QA 5.2. Do not merge into 6.0! + */ + public static function getTrackedTables(string $dbName): array + { + global $dbi; + + $trackingEnabled = Cache::get(self::TRACKER_ENABLED_CACHE_KEY, false); + if (! $trackingEnabled) { + return []; + } + + $relation = new Relation($dbi); + $trackingFeature = $relation->getRelationParameters()->trackingFeature; + if ($trackingFeature === null) { + return []; + } + + $sqlQuery = sprintf( + "SELECT table_name, tracking_active + FROM ( + SELECT table_name, MAX(version) version + FROM %s.%s WHERE db_name = %s AND table_name <> '' + GROUP BY table_name + ) filtered_tables + JOIN %s.%s USING(table_name, version)", + Util::backquote($trackingFeature->database), + Util::backquote($trackingFeature->tracking), + "'" . $dbi->escapeString($dbName, DatabaseInterface::CONNECT_CONTROL) . "'", + Util::backquote($trackingFeature->database), + Util::backquote($trackingFeature->tracking) + ); + + $trackedTables = []; + foreach ($dbi->queryAsControlUser($sqlQuery) as $row) { + $trackedTable = ['name' => (string) $row['table_name'], 'active' => (bool) $row['tracking_active']]; + $trackedTables[$trackedTable['name']] = $trackedTable; + } + + return $trackedTables; + } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index fcc1d843a47a..419f7d09e31d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1115,6 +1115,11 @@ parameters: count: 1 path: libraries/classes/Controllers/Database/StructureController.php + - + message: "#^Method PhpMyAdmin\\\\Controllers\\\\Database\\\\StructureController\\:\\:getTrackingIcon\\(\\) has parameter \\$trackedTable with no value type specified in iterable type array\\.$#" + count: 1 + path: libraries/classes/Controllers/Database/StructureController.php + - message: "#^Method PhpMyAdmin\\\\Controllers\\\\Database\\\\StructureController\\:\\:getValuesForAriaTable\\(\\) has parameter \\$currentTable with no value type specified in iterable type array\\.$#" count: 1 @@ -8555,6 +8560,11 @@ parameters: count: 1 path: libraries/classes/Tracker.php + - + message: "#^Method PhpMyAdmin\\\\Tracker\\:\\:getTrackedTables\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: libraries/classes/Tracker.php + - message: "#^Method PhpMyAdmin\\\\Tracker\\:\\:parseQuery\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 1a69e9392d5d..a7496ec8cc83 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1777,7 +1777,7 @@ $formattedOverhead $formattedSize - + $checkTime $checkTimeAll $createTime @@ -1811,6 +1811,7 @@ $replicaInfo['Wild_Ignore_Table'] $sumSize $tableIsView + $trackedTables[$truename] ?? null $truename $updateTime $updateTimeAll @@ -14038,12 +14039,13 @@ self::$trackingCache[$dbName][$tableName] - + $GLOBALS['db'] $data $result['tablename'] $trackingEnabled $trackingEnabled + $trackingEnabled bool