Indexes in MySQL

June 2, 2006
Author
Vadim Tkachenko
Share this Post:

MySQL does not always make a right decision about indexes usage.
Condsider a simple table:

 

;
250001 (V1)

;
83036 (V2)
(execution time = 110 ms)

That is index selectivity by condition (ID1=1) is V2/V1 = 0.3321 or 33.21%

It is said (e.g. book “SQL Tuning”) if selectivity over 20% then a full table scan is preferable than an index access.
As far as I know Oracle alway chooses a full table scan if selectivity over 25%.

What with MySQL:

That is MySQL will use index for this query.

Let’s compare the execution time with index access and with table scan:

– 410 ms

– 200 ms

As you see the table scan is faster by 2 times.

Consider more extremal case: selectivity ~95%:

0.9492 = 94.92%

Explain still claims MySQL will use index.

Execution time:

– 1200 ms

– 260 ms

That is table scan is faster by 4.6 times.

Why does MySQL choose index access?
MySQL doesn’t calculate index selectivity, just estimates count of logical input/output operations, and for
our case count of Logical I/O for index access is less than for table scan.

So be careful with indexes, they help in not all cases.

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