PHP code example of heyosseus / vacuum

1. Go to this page and download the library: Download heyosseus/vacuum library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

heyosseus / vacuum example snippets


// config/vacuum.php
'learn' => [
    'enabled' => env('VACUUM_LEARN_ENABLED', true),
],

->plugin(\Heyosseus\Vacuum\Filament\VacuumPlugin::make())

use Heyosseus\Vacuum\Vacuum;

Vacuum::auth(fn (Request $request) => $request->user()?->isAdmin() === true);

'middleware' => ['web', 'auth'],

use Illuminate\Support\Facades\Schedule;

Schedule::command('vacuum:snapshot')->hourly();

'thresholds' => [
    'dead_tuple_ratio' => 0.20,
    'dead_tuple_minimum' => 1_000,
    'cache_hit_ratio' => 0.99,
    'cache_hit_minimum_blocks' => 100_000,
    'wraparound_xid_age' => 200_000_000,          // match your autovacuum_freeze_max_age
    'wraparound_xid_age_critical' => 1_000_000_000,
    'wraparound_mxid_age' => 400_000_000,         // match your autovacuum_multixact_freeze_max_age
    'wraparound_mxid_age_critical' => 1_000_000_000,
    'stale_statistics_ratio' => 0.20,             // autoanalyze fires at 0.10
    'stale_statistics_minimum' => 10_000,
    'stale_statistics_minimum_rows' => 1_000,
    'bloat_bytes' => 100 * 1024 * 1024,
    'unused_index_min_size' => 1024 * 1024,
    'long_running_query_seconds' => 60,
    'idle_in_transaction_seconds' => 300,
    'slow_query_milliseconds' => 500,
],

use Heyosseus\Vacuum\Advisor\{Finding, Severity, TableRule};
use Heyosseus\Vacuum\Values\TableStatistic;

final readonly class NeverAnalyzed implements TableRule
{
    public function inspect(TableStatistic $table): ?Finding
    {
        if ($table->lastAnalyzedAt() !== null) {
            return null;
        }

        return new Finding(
            rule: 'never-analyzed',
            subject: $table->qualifiedName(),
            severity: Severity::Warning,
            summary: 'Nothing has ever analyzed this table, so the planner is guessing.',
            impact: 'Without statistics the planner cannot estimate row counts, and it will choose a bad plan confidently.',
            remediation: "ANALYZE {$table->qualifiedName()};",
        );
    }
}

use Heyosseus\Vacuum\VacuumServiceProvider;

$this->app->tag([NeverAnalyzed::class], VacuumServiceProvider::TABLE_RULES);
yaml
- run: php artisan vacuum:lint --format=github
bash
php artisan vacuum:check
bash
php artisan vacuum:check --fail-on=warning   # critical, warning, info, or never
php artisan vacuum:check --format=json       # score, grade, deductions, findings
bash
php artisan vacuum:lint
bash
php artisan vacuum:lint --fail-on=critical   # critical, warning, info, or never
php artisan vacuum:lint --format=json        # score, grade, deductions, findings
bash
php artisan vacuum:lint --generate-baseline
yaml
- run: php artisan vacuum:lint --format=github
bash
php artisan vendor:publish --tag=vacuum-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=vacuum-views