Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

enh(database): optimize partitioning during fresh install #6937

Merged
merged 1 commit into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions www/class/centreon-partition/partEngine.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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()) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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";
Expand Down
5 changes: 4 additions & 1 deletion www/install/steps/process/partitionTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down