I’ve worked with MySQL for years, but I realised there were still parts of database indexing that I understood more in theory than in practice.

So I decided to spend some time experimenting with MySQL indexes.

Rather than starting with a large dataset, I created a small database with 10,000 customer records and used EXPLAIN and EXPLAIN ANALYZE to observe how MySQL executed different queries.

Here’s what I learned.

Starting with a table of 10,000 customers

I created a simple customers table with five columns:

  • id – primary key

  • name

  • email

  • city

  • created_at

The table used InnoDB, and I populated it with 10,000 records.

Initially, the table only had its primary key index.

I started with a simple query:

EXPLAIN
SELECT *
FROM customers
WHERE email = 'customer5000@example.com';

The execution plan showed:

Field

Result

type

ALL

key

NULL

rows

10,096

MySQL was performing a full table scan. It had to examine the records to find the matching email address.

Adding a single-column index

I added an index on the email column:

CREATE INDEX idx_customers_email
ON customers(email);

Running the same query again produced a different execution plan:

Field

Result

type

ref

key

idx_customers_email

rows

1

Instead of scanning the entire table, MySQL could use the index to locate the matching record.

This was my first practical reminder of what an index does: it gives the database a more efficient way to find data without examining every row.

Understanding covering indexes

Next, I changed the query slightly:

EXPLAIN
SELECT email
FROM customers
WHERE email = 'customer5000@example.com';

This time, the Extra column showed:

Using index

This is a covering index.

The query only requested the email column, which was already available in the secondary index. MySQL didn't need to retrieve the complete row from the table.

In InnoDB, secondary indexes also contain the primary key value. This means a secondary index can sometimes cover queries that request both the indexed column and the primary key.

For example:

SELECT id, city
FROM customers
WHERE city = 'Kuala Lumpur';

I later confirmed this behaviour with EXPLAIN ANALYZE.

Experimenting with composite indexes

Single-column indexes are straightforward. But what happens when a query filters using more than one column?

I tested this query:

SELECT *
FROM customers
WHERE city = 'Kuala Lumpur'
AND created_at >= '2025-01-01';

Without a suitable index, MySQL performed a full table scan.

I then created a composite index:

CREATE INDEX idx_customers_city_created
ON customers(city, created_at);

After adding it, MySQL used the index with a range scan.

The execution plan showed approximately 2,000 matching rows.

This made sense: MySQL could first use the city value and then narrow the results using the created_at range.

The order of columns in a composite index matters.

An index on (city, created_at) can support queries using:

  • city

  • city and created_at

But it isn't automatically the right index for queries filtering only by created_at.

This is known as the leftmost-prefix principle.

Why does MySQL sometimes ignore an index?

This was one of the more interesting parts of the experiment.

I created another composite index with the column order reversed:

CREATE INDEX idx_customers_created_city
ON customers(created_at, city);

Now I had indexes in both orders:

  • (city, created_at)

  • (created_at, city)

I expected MySQL to use the second index for a query filtering only by created_at.

However, the execution plan still showed a full table scan.

Interestingly, the possible_keys column listed the index, but the key column was NULL.

That meant MySQL had considered the index but decided not to use it.

I checked the query with EXPLAIN ANALYZE and found that all 10,000 records matched the date condition.

Since the query was retrieving every row, scanning the table was a reasonable choice.

This was a useful distinction: an index being available doesn't mean MySQL will always use it. The optimizer chooses the execution plan it estimates will be cheaper.

Do we need a separate index for every column?

I also wanted to understand whether a standalone index on city was necessary when a composite index on (city, created_at) already existed.

Because city is the leftmost column, the composite index can support queries filtering by city alone.

I created a standalone index:

CREATE INDEX idx_customers_city
ON customers(city);

Then I ran:

EXPLAIN ANALYZE
SELECT city
FROM customers
WHERE city = 'Kuala Lumpur';

MySQL used the standalone index as a covering index.

I also tried selecting id and city:

EXPLAIN ANALYZE
SELECT id, city
FROM customers
WHERE city = 'Kuala Lumpur';

It was still a covering index lookup.

This works because InnoDB stores the primary key alongside secondary index entries.

A standalone index isn't necessarily redundant just because a composite index starts with the same column. A narrower index can be useful for certain queries. Whether it's worth keeping depends on the actual workload and the additional storage and write costs.

What I learned

After working through these experiments, a few things became clearer to me:

  1. An index can significantly reduce the number of rows MySQL needs to examine.

  2. A covering index can avoid retrieving the complete row from the clustered index.

  3. Column order matters when designing composite indexes.

  4. MySQL can use the leftmost columns of a composite index, but the optimizer still decides whether using it makes sense.

  5. More indexes aren't necessarily better. They also consume storage and add work to inserts, updates and deletes.

I also found it useful to distinguish between EXPLAIN and EXPLAIN ANALYZE.

EXPLAIN shows the optimizer's estimated execution plan. EXPLAIN ANALYZE actually executes the query and reports what happened, including actual row counts and execution time.

Seeing the estimates alongside the actual results made the behaviour much easier to understand.

Final thoughts

I've used indexes throughout my career, but spending time examining execution plans gave me a better understanding of what MySQL was doing behind the scenes.

The biggest takeaway for me is that index design shouldn't be based on assumptions. It should start with the queries an application actually runs, followed by examining how the database executes them.

Sometimes the best way to understand a database is simply to create a small experiment, run a query, change one thing, and see what happens.