Covering index and prefix indexes

November 24, 2006
Author
Peter Zaitsev
Share this Post:

I already wrote in the blog as well mentioned in presentation there is often a choice you have to make between having prefix index – which can be significantly smaller in size and having index being covering index, which means query can be executed using only data from the index without reading the row itself.

Today I had a chance to do couple of experiments to see when exactly it works or does not work:

Now lets see if index can be used as covering index if it has some key parts which are prefixes:

Great. As you see you actually can use index which has prefix key parts as covering index if you do not touch columns which only have prefixes in the index. Notice “Using Index” in Extra column.

In this case index can’t be used as covering index as we only have portion of “j” in the index. It is interesting I tested it with single character column values which all do fit in the index but MySQL does not look at the actual data in this case it only looks at definitions.

This example is less obvious – one may think why can’t we read only from the index as we only select k column ? The reason is – we’re using column j in where clause. Even if this particular like check can be done only by using index, MySQL is not smart enough to notice it – it simply checks if column is used in the query and if it does, covering index can’t be used.

Note: MySQL is however smart enough to make sure prefix specified is actually prefix, not the full key length. If you would specify key length of 10 in this case it will convert it to the full key instead of prefix key. If you would use length longer than column length, lets say 20, MySQL will refuse to create an index.

So in general handling of prefix keys in MySQL is smart in this respect.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved