Skip to content

Commit

Permalink
Increase the batch size of rows during the database backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 committed Sep 10, 2024
1 parent 255e551 commit bce9fd1
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions classes/Task/Upgrade/BackupDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class BackupDb extends AbstractTask
{
const TASK_TYPE = 'upgrade';

const BATCH_SIZE = 10000;

/**
* @throws Exception
*/
Expand Down Expand Up @@ -75,6 +77,8 @@ public function run(): int
_DB_PREFIX_ . 'guest',
_DB_PREFIX_ . 'statssearch', ];

$db = $this->container->getDb();

// INIT LOOP
if (!$this->container->getFileConfigurationStorage()->exists(UpgradeFileNames::DB_TABLES_TO_BACKUP_LIST)) {
$this->container->getState()->setProgressPercentage(
Expand All @@ -85,7 +89,7 @@ public function run(): int
mkdir($this->container->getProperty(UpgradeContainer::BACKUP_PATH) . DIRECTORY_SEPARATOR . $this->container->getState()->getBackupName());
}
$this->container->getState()->setDbStep(0);
$listOfTables = $this->container->getDb()->executeS('SHOW TABLES LIKE "' . _DB_PREFIX_ . '%"', true, false);
$listOfTables = $db->executeS('SHOW TABLES LIKE "' . _DB_PREFIX_ . '%"', true, false);

$tablesToBackup = new Backlog($listOfTables, count($listOfTables));
} else {
Expand Down Expand Up @@ -169,7 +173,7 @@ public function run(): int
// start schema : drop & create table only
if (null === $this->container->getState()->getBackupTable()) {
// Export the table schema
$schema = $this->container->getDb()->executeS('SHOW CREATE TABLE `' . $table . '`', true, false);
$schema = $db->executeS('SHOW CREATE TABLE `' . $table . '`', true, false);

if (count($schema) != 1 ||
!(isset($schema[0]['Table'], $schema[0]['Create Table'])
Expand Down Expand Up @@ -219,21 +223,21 @@ public function run(): int
if (!in_array($table, $ignore_stats_table)) {
do {
$backup_loop_limit = $this->container->getState()->getBackupLoopLimit();
$data = $this->container->getDb()->executeS('SELECT * FROM `' . $table . '` LIMIT ' . (int) $backup_loop_limit . ',200', false, false);
$this->container->getState()->setBackupLoopLimit($this->container->getState()->getBackupLoopLimit() + 200);
$sizeof = $this->container->getDb()->numRows();
$data = $db->executeS('SELECT * FROM `' . $table . '` LIMIT ' . (int) $backup_loop_limit . ','. self::BATCH_SIZE, false, false);
$this->container->getState()->setBackupLoopLimit($this->container->getState()->getBackupLoopLimit() + self::BATCH_SIZE);
$sizeof = $db->numRows();
if ($data && ($sizeof > 0)) {
// Export the table data
$written += fwrite($fp, 'INSERT INTO `' . $table . "` VALUES\n");
$i = 1;
while ($row = $this->container->getDb()->nextRow($data)) {
while ($row = $db->nextRow($data)) {
// this starts a row
$s = '(';
foreach ($row as $field => $value) {
if ($value === null) {
$s .= 'NULL,';
} else {
$s .= "'" . $this->container->getDb()->escape($value, true) . "',";
$s .= "'" . $db->escape($value, true) . "',";
}
}
$s = rtrim($s, ',');
Expand Down Expand Up @@ -301,7 +305,7 @@ public function run(): int
$this->container->getState()->setDbStep(0);

$this->logger->info($this->translator->trans('Database backup done in filename %s. Now upgrading files...', [$this->container->getState()->getBackupName()]));
$this->next = 'upgradeFiles';
$this->next = 'upgradeComplete';

return ExitCode::SUCCESS;
}
Expand Down

0 comments on commit bce9fd1

Please sign in to comment.