A Load Test Against an Empty Database Is a Lie
The query planner picks a plan from the number of rows it expects to touch. On a few dozen seed rows a full table scan is cheaper than an index lookup, so the planner may skip the index. On four million production rows it chooses differently. A load test against the small dataset measures the fast plan and says nothing about the one your users hit.
Don't do this:
<?php
declare(strict_types=1);
namespace Database\Seeders;
use App\Models\Article;
use App\Models\User;
use Illuminate\Database\Seeder;
final class LoadTestSeeder extends Seeder
{
public function run(): void
{
User::factory()
->count(10)
->has(Article::factory()->count(3))
->create();
}
}Do this instead: Seed production-shaped volume with batched inserts, because creating four million rows one model at a time takes far too long:
<?php
declare(strict_types=1);
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
final class LoadTestSeeder extends Seeder
{
private const ARTICLES = 4_000_000;
private const BATCH_SIZE = 5_000;
public function run(): void
{
$userIds = User::factory()->count(1_000)->create()->modelKeys();
for ($inserted = 0; $inserted < self::ARTICLES; $inserted += self::BATCH_SIZE) {
$rows = [];
for ($i = 0; $i < self::BATCH_SIZE; $i++) {
$rows[] = [
'user_id' => $userIds[array_rand($userIds)],
'title' => 'Article ' . ($inserted + $i),
'created_at' => now(),
'updated_at' => now(),
];
}
DB::table('articles')->insert($rows);
}
}
}Match the row counts production carries, and the distribution too. A table where one customer owns half the rows behaves differently from one where rows spread evenly, and this seeder spreads them evenly, so skew user_id if production is skewed. Run ANALYZE on the table afterwards (ANALYZE TABLE in MySQL) so the planner's statistics describe the new volume.