Skip to content
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
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ FROM base AS production
USER www-data

# Create the SQLite database, migrate the tables, and seed the data
RUN php -r "file_exists('database/database.sqlite') || touch('database/database.sqlite');"
RUN php -r "file_exists('database/database.sqlite') || touch('database/database.sqlite');" \
&& php artisan db:tune-sqlite-reads
11 changes: 11 additions & 0 deletions app/Console/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function handle()

$this->ensureDatabaseExists();

$this->configureDatabase();

$this->copyEnvExample();

$this->createAppKey();
Expand All @@ -56,6 +58,15 @@ protected function ensureDatabaseExists(): void
file_exists(database_path('database.sqlite')) || touch(database_path('database.sqlite'));
}

protected function configureDatabase(): void
{
try {
$this->call('db:tune-sqlite-reads');
} catch (\Throwable $th) {
$this->fail('❌ There was an issue tuning SQLite reads, check the logs.');
}
}

/**
* Copy the .env.example file to .env.
*/
Expand Down
93 changes: 93 additions & 0 deletions app/Console/Commands/TuneSqliteForReads.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class TuneSqliteForReads extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:tune-sqlite-reads';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Tune SQLite database for improved read performance';

/**
* Execute the console command.
*/
public function handle()
{
if (config('database.default') !== 'sqlite') {
$this->error('This command is only available for SQLite databases.');

return 1;
}

$this->info('Tuning SQLite database for read performance...');

try {
// Enable WAL mode for better read performance
DB::statement('PRAGMA journal_mode=WAL');
$this->info('✓ Journal mode set to WAL (Write-Ahead Logging)');

// Set cache size to 10,000 pages (approximately 40MB with default page size of 4KB)
DB::statement('PRAGMA cache_size=10000');
$this->info('✓ Cache size set to 10,000 pages');

// Additional read optimizations
DB::statement('PRAGMA synchronous=NORMAL');
$this->info('✓ Synchronous mode set to NORMAL');

DB::statement('PRAGMA temp_store=MEMORY');
$this->info('✓ Temporary tables stored in memory');

DB::statement('PRAGMA mmap_size=268435456'); // 256MB
$this->info('✓ Memory-mapped I/O enabled (256MB)');

// Verify the settings
$this->newLine();
$this->info('Current SQLite configuration:');

$journalMode = DB::selectOne('PRAGMA journal_mode');
$this->line("Journal mode: {$journalMode->journal_mode}");

$cacheSize = DB::selectOne('PRAGMA cache_size');
$this->line("Cache size: {$cacheSize->cache_size} pages");

$synchronous = DB::selectOne('PRAGMA synchronous');
$this->line("Synchronous: {$synchronous->synchronous}");

$tempStore = DB::selectOne('PRAGMA temp_store');
$this->line("Temp store: {$tempStore->temp_store}");

$mmapSize = DB::selectOne('PRAGMA mmap_size');
$this->line('Memory-mapped size: '.number_format($mmapSize->mmap_size / 1024 / 1024, 1).'MB');

$this->newLine();
$this->info('SQLite database has been successfully tuned for read performance!');
$this->comment('Note: These settings are applied immediately. For persistent configuration across');
$this->comment('application restarts, set the following in your .env file:');
$this->comment('DB_JOURNAL_MODE=WAL');
$this->comment('DB_CACHE_SIZE=10000');
$this->comment('DB_SYNCHRONOUS=NORMAL');
$this->comment('DB_TEMP_STORE=MEMORY');
$this->comment('DB_MMAP_SIZE=268435456');

} catch (\Exception $e) {
$this->error('Failed to tune SQLite database: '.$e->getMessage());

return 1;
}

return 0;
}
}
7 changes: 5 additions & 2 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
'busy_timeout' => null,
'journal_mode' => null,
'synchronous' => null,
'journal_mode' => env('DB_JOURNAL_MODE', 'WAL'),
'synchronous' => env('DB_SYNCHRONOUS', 'NORMAL'),
'cache_size' => env('DB_CACHE_SIZE', 10000),
'temp_store' => env('DB_TEMP_STORE', 'MEMORY'),
'mmap_size' => env('DB_MMAP_SIZE', 268435456), // 256MB
],

'mysql' => [
Expand Down