It seems that you are trying to load 250_000 records into memory? No index will fix that. Indexes are to help in faster where, but you should limit the amount of data that you get
I also don't see any indexes on price ?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to master index columns in data base. But, in my opinion, nothing works for me.
I create a table specifically for the test:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name', 50);
$table->string('color')->index();
$table->string('gender');
$table->unsignedBigInteger('old_price')->index();
$table->unsignedBigInteger('price');
$table->timestamps();
});
}
create a table specifically for the test:
public function definition()
{
return [
'name' => $this->faker->text(50),
'color' => $this->faker->randomElement(['white', 'black']),
'gender' => $this->faker->randomElement(['man', 'woman', 'boy']),
'old_price' => $this->faker->numberBetween(11,14),
'price' => $this->faker->numberBetween(15,18),
];
}
Launching the seeder:
public function run()
{
Product::factory(500000)->create();
}
After that, I alternately try to find some results. But I don't notice the time difference. Columns that are in the index are loaded the same as without the index.
$products = Produst::select()
->where('color', 'white')
//->where('gender', 'woman')
//->where('price', '15')
//->where('old_price', '18')
->get();
dump($products );
$loadingTime = round(microtime(true) - LARAVEL_START, 3);
echo $loadingTime ;
dd();
This data is loaded in about 8 seconds. What am I doing wrong? What? I do not understand? Thank you in advance!
Please or to participate in this conversation.