67 lines
1.9 KiB
PHP
67 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Services\QueryOptimizationService;
|
|
use Illuminate\Console\Attributes\Description;
|
|
use Illuminate\Console\Attributes\Signature;
|
|
use Illuminate\Console\Command;
|
|
|
|
#[Signature('db:optimize')]
|
|
#[Description('Optimize database tables and analyze statistics')]
|
|
class OptimizeDatabaseCommand extends Command
|
|
{
|
|
public function __construct(
|
|
private QueryOptimizationService $optimizationService
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle(): int
|
|
{
|
|
$this->info('Starting database optimization...');
|
|
|
|
$tables = [
|
|
'jobs',
|
|
'job_categories',
|
|
'job_sources',
|
|
'job_locations',
|
|
'api_logs',
|
|
'job_fetch_logs',
|
|
'analytics_events',
|
|
];
|
|
|
|
foreach ($tables as $table) {
|
|
$this->info("Optimizing table: {$table}");
|
|
|
|
try {
|
|
// Optimize table (defragment and rebuild indexes)
|
|
$this->optimizationService->optimizeTable($table);
|
|
$this->line(" ✓ Optimized");
|
|
|
|
// Analyze table statistics
|
|
$this->optimizationService->analyzeTable($table);
|
|
$this->line(" ✓ Analyzed");
|
|
|
|
// Get table stats
|
|
$stats = $this->optimizationService->getTableStats($table);
|
|
if ($stats) {
|
|
$this->line(" Size: {$stats['size_mb']} MB (Data: {$stats['data_size_mb']} MB, Indexes: {$stats['index_size_mb']} MB)");
|
|
$this->line(" Rows: " . number_format($stats['table_rows']));
|
|
}
|
|
|
|
$this->newLine();
|
|
} catch (\Exception $e) {
|
|
$this->error(" ✗ Failed: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
$this->info('Database optimization completed!');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|