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
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);

View 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
*

View 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
*

View 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)

View 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]);
}
/**

View 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');
}
}

View 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();
}
}

View 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');