From: Philipp Date: Sat, 13 May 2023 20:14:52 +0000 (+0200) Subject: Use optimized update statements X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5be9c9dbaf7f6e606372b4e18656631719ee8107;p=friendica.git Use optimized update statements --- diff --git a/src/Core/Worker/Cron.php b/src/Core/Worker/Cron.php index e9b77229bc..d0b915f87a 100644 --- a/src/Core/Worker/Cron.php +++ b/src/Core/Worker/Cron.php @@ -151,8 +151,8 @@ class Cron // We are acquiring the two locks from the worker to avoid locking problems if (DI::lock()->acquire(Worker::LOCK_PROCESS, 10)) { if (DI::lock()->acquire(Worker::LOCK_WORKER, 10)) { - DBA::e("OPTIMIZE TABLE `workerqueue`"); - DBA::e("OPTIMIZE TABLE `process`"); + DBA::optimizeTable('workerqueue'); + DBA::optimizeTable('process'); DI::lock()->release(Worker::LOCK_WORKER); } DI::lock()->release(Worker::LOCK_PROCESS); diff --git a/src/Database/DBA.php b/src/Database/DBA.php index d609f108eb..e730b298d5 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -821,6 +821,29 @@ class DBA return DI::dba()->processlist(); } + /** + * Optimizes tables + * + * @param string $table a given table + * + * @return bool True, if successfully optimized, otherwise false + * @throws \Exception + */ + public static function optimizeTable(string $table): bool + { + return DI::dba()->optimizeTable($table); + } + + /** + * Kill sleeping database processes + * + * @return void + */ + public static function deleteSleepingProcesses() + { + return DI::dba()->delete(); + } + /** * Fetch a database variable * diff --git a/src/Database/Database.php b/src/Database/Database.php index f4959bf7e9..f931169ceb 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1781,6 +1781,24 @@ class Database return $this->e("OPTIMIZE TABLE " . DBA::buildTableString([$table])) !== false; } + /** + * Kill sleeping database processes + * + * @return void + */ + public function deleteSleepingProcesses() + { + $processes = $this->p("SHOW FULL PROCESSLIST"); + while ($process = $this->fetch($processes)) { + if (($process['Command'] != 'Sleep') || ($process['Time'] < 300) || ($process['db'] != $this->databaseName())) { + continue; + } + + $this->e("KILL ?", $process['Id']); + } + $this->close($processes); + } + /** * Fetch a database variable * diff --git a/src/Model/Post/Delivery.php b/src/Model/Post/Delivery.php index 0e343e8718..c53014fa51 100644 --- a/src/Model/Post/Delivery.php +++ b/src/Model/Post/Delivery.php @@ -78,7 +78,7 @@ class Delivery */ public static function incrementFailed(int $uri_id, string $inbox) { - return DBA::e('UPDATE `post-delivery` SET `failed` = `failed` + 1 WHERE `uri-id` = ? AND `inbox-id` = ?', $uri_id, ItemURI::getIdByURI($inbox)); + return DBA::update('post-delivery', ["`failed` = `failed` + 1"], ['uri-id' => $uri_id, 'inbox-id' => ItemURI::getIdByURI($inbox)]); } public static function selectForInbox(string $inbox) diff --git a/src/Model/Post/DeliveryData.php b/src/Model/Post/DeliveryData.php index e87bb0e015..327e6f9121 100644 --- a/src/Model/Post/DeliveryData.php +++ b/src/Model/Post/DeliveryData.php @@ -82,27 +82,27 @@ class DeliveryData */ public static function incrementQueueDone(int $uri_id, int $protocol = 0) { - $sql = ''; + $increments = ["`queue_done` = `queue_done` + 1"]; switch ($protocol) { case self::ACTIVITYPUB: - $sql = ", `activitypub` = `activitypub` + 1"; + $increments[] = ["`activitypub` = `activitypub` + 1"]; break; case self::DFRN: - $sql = ", `dfrn` = `dfrn` + 1"; + $increments[] = ["`dfrn` = `dfrn` + 1"]; break; case self::LEGACY_DFRN: - $sql = ", `legacy_dfrn` = `legacy_dfrn` + 1"; + $increments[] = ["`legacy_dfrn` = `legacy_dfrn` + 1"]; break; case self::DIASPORA: - $sql = ", `diaspora` = `diaspora` + 1"; + $increments[] = ["`diaspora` = `diaspora` + 1"]; break; case self::OSTATUS: - $sql = ", `ostatus` = `ostatus` + 1"; + $increments[] = ["`ostatus` = `ostatus` + 1"]; break; } - return DBA::e('UPDATE `post-delivery-data` SET `queue_done` = `queue_done` + 1' . $sql . ' WHERE `uri-id` = ?', $uri_id); + return DBA::update('post-delivery-data', $increments, ['uri-id' => $uri_id]); } /** @@ -116,7 +116,7 @@ class DeliveryData */ public static function incrementQueueFailed(int $uri_id) { - return DBA::e('UPDATE `post-delivery-data` SET `queue_failed` = `queue_failed` + 1 WHERE `uri-id` = ?', $uri_id); + return DBA::update('post-delivery-data', ["`queue_failed` = `queue_failed` + 1"], ['uri-id' => $uri_id]); } /** @@ -129,7 +129,7 @@ class DeliveryData */ public static function incrementQueueCount(int $uri_id, int $increment = 1) { - return DBA::e('UPDATE `post-delivery-data` SET `queue_count` = `queue_count` + ? WHERE `uri-id` = ?', $increment, $uri_id); + return DBA::update('post-delivery-data', ["`queue_count` = `queue_count` + $increment"], ['uri-id' => $uri_id]); } /** diff --git a/src/Protocol/ActivityPub/Queue.php b/src/Protocol/ActivityPub/Queue.php index d77785fe67..beb8817bfb 100644 --- a/src/Protocol/ActivityPub/Queue.php +++ b/src/Protocol/ActivityPub/Queue.php @@ -312,7 +312,7 @@ class Queue // Optimizing this table only last seconds if (DI::config()->get('system', 'optimize_tables')) { Logger::info('Optimize start'); - DBA::e("OPTIMIZE TABLE `inbox-entry`"); + DBA::optimizeTable('inbox-entry'); Logger::info('Optimize end'); } } diff --git a/src/Worker/Cron.php b/src/Worker/Cron.php index 8c9ea58a10..73d158070e 100644 --- a/src/Worker/Cron.php +++ b/src/Worker/Cron.php @@ -163,15 +163,6 @@ class Cron { Logger::info('Looking for sleeping processes'); - $processes = DBA::p("SHOW FULL PROCESSLIST"); - while ($process = DBA::fetch($processes)) { - if (($process['Command'] != 'Sleep') || ($process['Time'] < 300) || ($process['db'] != DBA::databaseName())) { - continue; - } - - DBA::e("KILL ?", $process['Id']); - Logger::notice('Killed sleeping process', ['id' => $process['Id']]); - } - DBA::close($processes); + DBA::deleteSleepingProcesses(); } } diff --git a/src/Worker/OptimizeTables.php b/src/Worker/OptimizeTables.php index bb4cc9e482..784c72fde5 100644 --- a/src/Worker/OptimizeTables.php +++ b/src/Worker/OptimizeTables.php @@ -40,36 +40,36 @@ class OptimizeTables Logger::info('Optimize start'); - DBA::e("OPTIMIZE TABLE `cache`"); - DBA::e("OPTIMIZE TABLE `locks`"); - DBA::e("OPTIMIZE TABLE `oembed`"); - DBA::e("OPTIMIZE TABLE `parsed_url`"); - DBA::e("OPTIMIZE TABLE `session`"); + DBA::optimizeTable('cache'); + DBA::optimizeTable('locks'); + DBA::optimizeTable('oembed'); + DBA::optimizeTable('parsed_url'); + DBA::optimizeTable('session'); if (DI::config()->get('system', 'optimize_all_tables')) { - DBA::e("OPTIMIZE TABLE `apcontact`"); - DBA::e("OPTIMIZE TABLE `contact`"); - DBA::e("OPTIMIZE TABLE `contact-relation`"); - DBA::e("OPTIMIZE TABLE `conversation`"); - DBA::e("OPTIMIZE TABLE `diaspora-contact`"); - DBA::e("OPTIMIZE TABLE `diaspora-interaction`"); - DBA::e("OPTIMIZE TABLE `fcontact`"); - DBA::e("OPTIMIZE TABLE `gserver`"); - DBA::e("OPTIMIZE TABLE `gserver-tag`"); - DBA::e("OPTIMIZE TABLE `inbox-status`"); - DBA::e("OPTIMIZE TABLE `item-uri`"); - DBA::e("OPTIMIZE TABLE `notification`"); - DBA::e("OPTIMIZE TABLE `notify`"); - DBA::e("OPTIMIZE TABLE `photo`"); - DBA::e("OPTIMIZE TABLE `post`"); - DBA::e("OPTIMIZE TABLE `post-content`"); - DBA::e("OPTIMIZE TABLE `post-delivery-data`"); - DBA::e("OPTIMIZE TABLE `post-link`"); - DBA::e("OPTIMIZE TABLE `post-thread`"); - DBA::e("OPTIMIZE TABLE `post-thread-user`"); - DBA::e("OPTIMIZE TABLE `post-user`"); - DBA::e("OPTIMIZE TABLE `storage`"); - DBA::e("OPTIMIZE TABLE `tag`"); + DBA::optimizeTable('apcontact'); + DBA::optimizeTable('contact'); + DBA::optimizeTable('contact-relation'); + DBA::optimizeTable('conversation'); + DBA::optimizeTable('diaspora-contact'); + DBA::optimizeTable('diaspora-interaction'); + DBA::optimizeTable('fcontact'); + DBA::optimizeTable('gserver'); + DBA::optimizeTable('gserver-tag'); + DBA::optimizeTable('inbox-status'); + DBA::optimizeTable('item-uri'); + DBA::optimizeTable('notification'); + DBA::optimizeTable('notify'); + DBA::optimizeTable('photo'); + DBA::optimizeTable('post'); + DBA::optimizeTable('post-content'); + DBA::optimizeTable('post-delivery-data'); + DBA::optimizeTable('post-link'); + DBA::optimizeTable('post-thread'); + DBA::optimizeTable('post-thread-user'); + DBA::optimizeTable('post-user'); + DBA::optimizeTable('storage'); + DBA::optimizeTable('tag'); } Logger::info('Optimize end');