diff --git a/www/class/centreon-partition/partEngine.class.php b/www/class/centreon-partition/partEngine.class.php index 6c5c738d60e..34b37a33b86 100644 --- a/www/class/centreon-partition/partEngine.class.php +++ b/www/class/centreon-partition/partEngine.class.php @@ -177,16 +177,24 @@ private function updateDailyPartitions($db, $tableName, $table, $lastTime) $this->createMaxvaluePartition($db, $tableName, $table); } } - - private function createDailyPartitions($table) + + /** + * Generate query part to build partitions + * + * @param MysqlTable $table The table to partition + * @param bool $createPastPartitions If the past partitions need to be created + * @return string The built partitions query + */ + private function createDailyPartitions($table, $createPastPartitions): string { date_default_timezone_set($table->getTimezone()); $ltime = localtime(); - //$current_time = mktime(0, 0, 0, $ltime[4], $ltime[3], $ltime[5]+1900); $createPart = " PARTITION BY RANGE(".$table->getColumn().") ("; - $num_days = $table->getRetention(); - + + // Create past partitions if needed (not needed in fresh install) + $num_days = ($createPastPartitions === true) ? $table->getRetention() : 0; + $append = ''; while ($num_days >= 0) { $current_time = mktime(0, 0, 0, $ltime[4]+1, $ltime[3]-$num_days, $ltime[5]+1900); @@ -204,6 +212,8 @@ private function createDailyPartitions($table) $num_days--; $append = ','; } + + // Create future partitions $num_days_forward = $table->getRetentionForward(); $count = 1; while ($count <= $num_days_forward) { @@ -222,16 +232,20 @@ private function createDailyPartitions($table) $append = ','; $count++; } - + $createPart .= ");"; return $createPart; } - + /** * Create a new table with partitions + * + * @param MysqlTable $table The table to partition + * @param CentreonDB $db The database connection + * @param bool $createPastPartitions If the past partitions need to be created */ - public function createParts($table, $db) + public function createParts($table, $db, $createPastPartitions): void { $tableName = $table->getSchema().".".$table->getName(); if ($table->exists()) { @@ -240,7 +254,7 @@ public function createParts($table, $db) $partition_part = null; if ($table->getType() == 'date' && $table->getDuration() == 'daily') { - $partition_part = $this->createDailyPartitions($table); + $partition_part = $this->createDailyPartitions($table, $createPastPartitions); } if (is_null($partition_part)) { @@ -381,7 +395,8 @@ public function migrate($table, $db) * creating new table with the initial name */ echo "[".date(DATE_RFC822)."][migrate] Creating parts for new table ".$tableName."\n"; - $this->createParts($table, $db); + // create partitions for past and future + $this->createParts($table, $db, true); // dumping data from existing table echo "[".date(DATE_RFC822)."][migrate] Insert data from ".$tableName."_old to new table\n"; diff --git a/www/install/steps/process/partitionTables.php b/www/install/steps/process/partitionTables.php index 7fdadcd0e25..cfc9b4dffd0 100644 --- a/www/install/steps/process/partitionTables.php +++ b/www/install/steps/process/partitionTables.php @@ -76,7 +76,10 @@ $centreonDb ); $mysqlTable = $config->getTable($table); - $partEngine->createParts($mysqlTable, $database); + + // past partitions do not need to be created + // it optimizes the time for partition process + $partEngine->createParts($mysqlTable, $database, false); } } catch (\Exception $e) { $return['msg'] = preg_replace('/\n/', "", $e->getMessage());