From bd84f153bbc869aa9b1e06b4d1eec1eb28c0e0c5 Mon Sep 17 00:00:00 2001 From: ildyria Date: Sun, 16 Jan 2022 19:47:28 +0100 Subject: [PATCH 1/2] add migration to enforce optimization of tables: solve slow down due to indexes missing --- .../2022_01_16_181337_optimize_tables.php | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 database/migrations/2022_01_16_181337_optimize_tables.php diff --git a/database/migrations/2022_01_16_181337_optimize_tables.php b/database/migrations/2022_01_16_181337_optimize_tables.php new file mode 100644 index 00000000000..08c39e9633f --- /dev/null +++ b/database/migrations/2022_01_16_181337_optimize_tables.php @@ -0,0 +1,57 @@ +output = new ConsoleOutput(); + $this->msgSection = $this->output->section(); + } + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + $connection = Schema::connection(null)->getConnection(); + $tables = $connection->getDoctrineSchemaManager()->listTableNames(); + + $driverName = $connection->getDriverName(); + $sql = 'ANALYZE '; + if ($driverName == 'mysql') { + $this->msgSection->writeln('Info: MySql/MariaDB detected.'); + $sql = 'ANALYZE TABLE '; + } + + foreach ($tables as $table) { + try { + DB::statement($sql . $table); + $this->msgSection->writeln('Info: ' . $table . ' optimized.'); + } catch (\Throwable $th) { + $this->msgSection->writeln('Error: could not optimize ' . $table . '.'); + $this->msgSection->writeln('Error: ' . $th->getMessage()); + } + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // Nothing do to here. + } +} From 89c9070790ba4a9fbbfebb11aba1135b6a1c8fd4 Mon Sep 17 00:00:00 2001 From: Matthias Nagel Date: Sun, 16 Jan 2022 20:01:07 +0100 Subject: [PATCH 2/2] Made migration more robust wrt. other DBMS. --- .../2022_01_16_181337_optimize_tables.php | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/database/migrations/2022_01_16_181337_optimize_tables.php b/database/migrations/2022_01_16_181337_optimize_tables.php index 08c39e9633f..af05502b3ac 100644 --- a/database/migrations/2022_01_16_181337_optimize_tables.php +++ b/database/migrations/2022_01_16_181337_optimize_tables.php @@ -28,10 +28,24 @@ public function up() $tables = $connection->getDoctrineSchemaManager()->listTableNames(); $driverName = $connection->getDriverName(); - $sql = 'ANALYZE '; - if ($driverName == 'mysql') { - $this->msgSection->writeln('Info: MySql/MariaDB detected.'); - $sql = 'ANALYZE TABLE '; + + switch ($driverName) { + case 'mysql': + $this->msgSection->writeln('Info: MySql/MariaDB detected.'); + $sql = 'ANALYZE TABLE '; + break; + case 'posgresql': + $this->msgSection->writeln('Info: PostgreSQL detected.'); + $sql = 'ANALYZE '; + break; + case 'sqlite': + $this->msgSection->writeln('Info: SQLite detected.'); + $sql = 'ANALYZE '; + break; + default: + $this->msgSection->writeln('Warning: Unknown DBMS; doing nothing.'); + + return; } foreach ($tables as $table) {