Use optimized update statements

This commit is contained in:
Philipp Holzer 2023-05-13 22:14:52 +02:00
parent 557d0e3aeb
commit 5be9c9dbaf
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
8 changed files with 83 additions and 51 deletions

View file

@ -151,8 +151,8 @@ class Cron
// We are acquiring the two locks from the worker to avoid locking problems // 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_PROCESS, 10)) {
if (DI::lock()->acquire(Worker::LOCK_WORKER, 10)) { if (DI::lock()->acquire(Worker::LOCK_WORKER, 10)) {
DBA::e("OPTIMIZE TABLE `workerqueue`"); DBA::optimizeTable('workerqueue');
DBA::e("OPTIMIZE TABLE `process`"); DBA::optimizeTable('process');
DI::lock()->release(Worker::LOCK_WORKER); DI::lock()->release(Worker::LOCK_WORKER);
} }
DI::lock()->release(Worker::LOCK_PROCESS); DI::lock()->release(Worker::LOCK_PROCESS);

View file

@ -821,6 +821,29 @@ class DBA
return DI::dba()->processlist(); 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 * Fetch a database variable
* *

View file

@ -1781,6 +1781,24 @@ class Database
return $this->e("OPTIMIZE TABLE " . DBA::buildTableString([$table])) !== false; 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 * Fetch a database variable
* *

View file

@ -78,7 +78,7 @@ class Delivery
*/ */
public static function incrementFailed(int $uri_id, string $inbox) 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) public static function selectForInbox(string $inbox)

View file

@ -82,27 +82,27 @@ class DeliveryData
*/ */
public static function incrementQueueDone(int $uri_id, int $protocol = 0) public static function incrementQueueDone(int $uri_id, int $protocol = 0)
{ {
$sql = ''; $increments = ["`queue_done` = `queue_done` + 1"];
switch ($protocol) { switch ($protocol) {
case self::ACTIVITYPUB: case self::ACTIVITYPUB:
$sql = ", `activitypub` = `activitypub` + 1"; $increments[] = ["`activitypub` = `activitypub` + 1"];
break; break;
case self::DFRN: case self::DFRN:
$sql = ", `dfrn` = `dfrn` + 1"; $increments[] = ["`dfrn` = `dfrn` + 1"];
break; break;
case self::LEGACY_DFRN: case self::LEGACY_DFRN:
$sql = ", `legacy_dfrn` = `legacy_dfrn` + 1"; $increments[] = ["`legacy_dfrn` = `legacy_dfrn` + 1"];
break; break;
case self::DIASPORA: case self::DIASPORA:
$sql = ", `diaspora` = `diaspora` + 1"; $increments[] = ["`diaspora` = `diaspora` + 1"];
break; break;
case self::OSTATUS: case self::OSTATUS:
$sql = ", `ostatus` = `ostatus` + 1"; $increments[] = ["`ostatus` = `ostatus` + 1"];
break; 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) 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) 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]);
} }
/** /**

View file

@ -312,7 +312,7 @@ class Queue
// Optimizing this table only last seconds // Optimizing this table only last seconds
if (DI::config()->get('system', 'optimize_tables')) { if (DI::config()->get('system', 'optimize_tables')) {
Logger::info('Optimize start'); Logger::info('Optimize start');
DBA::e("OPTIMIZE TABLE `inbox-entry`"); DBA::optimizeTable('inbox-entry');
Logger::info('Optimize end'); Logger::info('Optimize end');
} }
} }

View file

@ -163,15 +163,6 @@ class Cron
{ {
Logger::info('Looking for sleeping processes'); Logger::info('Looking for sleeping processes');
$processes = DBA::p("SHOW FULL PROCESSLIST"); DBA::deleteSleepingProcesses();
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);
} }
} }

View file

@ -40,36 +40,36 @@ class OptimizeTables
Logger::info('Optimize start'); Logger::info('Optimize start');
DBA::e("OPTIMIZE TABLE `cache`"); DBA::optimizeTable('cache');
DBA::e("OPTIMIZE TABLE `locks`"); DBA::optimizeTable('locks');
DBA::e("OPTIMIZE TABLE `oembed`"); DBA::optimizeTable('oembed');
DBA::e("OPTIMIZE TABLE `parsed_url`"); DBA::optimizeTable('parsed_url');
DBA::e("OPTIMIZE TABLE `session`"); DBA::optimizeTable('session');
if (DI::config()->get('system', 'optimize_all_tables')) { if (DI::config()->get('system', 'optimize_all_tables')) {
DBA::e("OPTIMIZE TABLE `apcontact`"); DBA::optimizeTable('apcontact');
DBA::e("OPTIMIZE TABLE `contact`"); DBA::optimizeTable('contact');
DBA::e("OPTIMIZE TABLE `contact-relation`"); DBA::optimizeTable('contact-relation');
DBA::e("OPTIMIZE TABLE `conversation`"); DBA::optimizeTable('conversation');
DBA::e("OPTIMIZE TABLE `diaspora-contact`"); DBA::optimizeTable('diaspora-contact');
DBA::e("OPTIMIZE TABLE `diaspora-interaction`"); DBA::optimizeTable('diaspora-interaction');
DBA::e("OPTIMIZE TABLE `fcontact`"); DBA::optimizeTable('fcontact');
DBA::e("OPTIMIZE TABLE `gserver`"); DBA::optimizeTable('gserver');
DBA::e("OPTIMIZE TABLE `gserver-tag`"); DBA::optimizeTable('gserver-tag');
DBA::e("OPTIMIZE TABLE `inbox-status`"); DBA::optimizeTable('inbox-status');
DBA::e("OPTIMIZE TABLE `item-uri`"); DBA::optimizeTable('item-uri');
DBA::e("OPTIMIZE TABLE `notification`"); DBA::optimizeTable('notification');
DBA::e("OPTIMIZE TABLE `notify`"); DBA::optimizeTable('notify');
DBA::e("OPTIMIZE TABLE `photo`"); DBA::optimizeTable('photo');
DBA::e("OPTIMIZE TABLE `post`"); DBA::optimizeTable('post');
DBA::e("OPTIMIZE TABLE `post-content`"); DBA::optimizeTable('post-content');
DBA::e("OPTIMIZE TABLE `post-delivery-data`"); DBA::optimizeTable('post-delivery-data');
DBA::e("OPTIMIZE TABLE `post-link`"); DBA::optimizeTable('post-link');
DBA::e("OPTIMIZE TABLE `post-thread`"); DBA::optimizeTable('post-thread');
DBA::e("OPTIMIZE TABLE `post-thread-user`"); DBA::optimizeTable('post-thread-user');
DBA::e("OPTIMIZE TABLE `post-user`"); DBA::optimizeTable('post-user');
DBA::e("OPTIMIZE TABLE `storage`"); DBA::optimizeTable('storage');
DBA::e("OPTIMIZE TABLE `tag`"); DBA::optimizeTable('tag');
} }
Logger::info('Optimize end'); Logger::info('Optimize end');