Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rediska's avatar

How to correctly add an index to mysql database columns?

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!

0 likes
9 replies
Sinnbeck's avatar

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 ?

1 like
Sinnbeck's avatar

Also in this case I would consider a compound index. But use explain to check

1 like
Rediska's avatar

@Sinnbeck Everything is clear, but nothing is clear =)) Then how do I use the index? And why doesn't it work? Colleagues from this forum gave me good advice - to store product attributes in related tables and access them through relationships. I will definitely do it. But first, I want to understand how the database search works. Therefore, using the test method, I check the download speed of various requests. But at the moment my head is boiling and I don't understand))

1 like
Sinnbeck's avatar

@Rediska indexes can be a bit tricky to predict as the database will try to find the best match based on the query. This is why it's a good idea to use explain to understand what is doing

Check this keynote by Aaron Francis about indexes https://youtu.be/f4QShF42c6E it starts at 6 hours or so

3 likes
Tray2's avatar

@Rediska Indexes are one of the most complicated things to learn. It is easy to create one but it's hard to master how and when an index should be used.

In your case where you have roughly 250K records with the color white and 250K with the color black, the index doesn't really add any benefit. If you on the other hand would have had 200 colors with with 2500 records each, the index would be a huge improvement in performance.

I suggest you read this post, the index part anyways https://tray2.se/posts/database-design-part-2

This one also handles the most important index, the foreign key index, so give that one a read too. https://tray2.se/posts/properly-formed-foreign-keys-are-your-best-friends

4 likes
jeffallen's avatar

You probably need to paginate your data.

2 likes

Please or to participate in this conversation.