]> git.mxchange.org Git - friendica.git/commitdiff
Use optimized update statements
authorPhilipp <admin@philipp.info>
Sat, 13 May 2023 20:14:52 +0000 (22:14 +0200)
committerPhilipp <admin@philipp.info>
Sat, 13 May 2023 20:14:52 +0000 (22:14 +0200)
src/Core/Worker/Cron.php
src/Database/DBA.php
src/Database/Database.php
src/Model/Post/Delivery.php
src/Model/Post/DeliveryData.php
src/Protocol/ActivityPub/Queue.php
src/Worker/Cron.php
src/Worker/OptimizeTables.php

index e9b77229bc624a8b83edad27c2c0abc8ebf9323b..d0b915f87aa36c70be2d5b5efb1938b9faf6beb2 100644 (file)
@@ -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);
index d609f108ebf0626506b28d251b99dabfb2de7706..e730b298d593885eb3ce0974a155d9ef29bc4ab9 100644 (file)
@@ -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
         *
index f4959bf7e9028ea8818c569ac2f379f5a8e7fcfd..f931169ceb4ae417bcee2932c4dfe73145e7c24b 100644 (file)
@@ -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
         *
index 0e343e87182e1c354688133250e34a47eb89ad33..c53014fa516ff8ee6fbcfaeacedf428c72db4fed 100644 (file)
@@ -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)
index e87bb0e0157febce69dfdc5a5dbc7dd8205b03f6..327e6f912194155ae9e1074fb4ffee8f491e382d 100644 (file)
@@ -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]);
        }
 
        /**
index d77785fe67b779d978d8fa16d687e92bf8e6202b..beb8817bfbdfaa65f15ee15fe6dc299b07a5e865 100644 (file)
@@ -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');
                }
        }
index 8c9ea58a10983abadad585935f37087db1e0c335..73d158070ee7469860b54970e321a87582091448 100644 (file)
@@ -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();
        }
 }
index bb4cc9e482258be8585fd3e5bd146d58c159951d..784c72fde5c1cf9295ba95fa42147d83f51ce273 100644 (file)
@@ -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');