# MySQL choosing wrong indexes

**URL:** <https://forums.percona.com/t/mysql-choosing-wrong-indexes/1685>\
**Category:** Other MySQL® Questions\
**Created:** [October 13, 2011, 12:07pm UTC](https://forums.percona.com/t/mysql-choosing-wrong-indexes/1685 "2011-10-13T12:07:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Forbidden](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Forbidden](https://forums.percona.com/u/Forbidden)\
**Post date:** [October 13, 2011, 12:07pm UTC](https://forums.percona.com/t/mysql-choosing-wrong-indexes/1685/1 "2011-10-13T12:07:14Z")

</div>

Hello, I’m hoping someone can point me in the right direction on this. I’ve had trouble searching because I don’t quite know what this sort of problem is officially called. We’re running MySQL 5.5.9 with four slaves and one master. I’ve been running into a problem where one or more of the slaves will choose the wrong indexes on fairly complicated queries.

For instance, with the same query, the first row on the explain will be:

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 1. row \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
id: 1  
select\_type: SIMPLE  
table: wrsemployees  
partitions: NULL  
type: ref  
possible\_keys: PRIMARY,wrsemployees\_practice\_id  
key: wrsemployees\_practice\_id  
key\_len: 5  
ref: const  
rows: 24  
Extra: Using where; Using index; Using temporary; Using filesort

While on another server, it will be:  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 1. row \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
id: 1  
select\_type: SIMPLE  
table: wrsemployeeordersnotesections  
partitions: NULL  
type: ALL  
possible\_keys: PRIMARY,wrsemployeeordersnotesections\_employee\_order\_id,wrse mployeeordersnotesections\_section\_id  
key: NULL  
key\_len: NULL  
ref: NULL  
rows: 3941822  
Extra: Using where; Using temporary; Using filesort

Obviously, the performance on the latter is simply unacceptable.

I’ve tried analyzing the tables. I’ve tried rebuilding the indexes. A force index will solve it, but I’d like to get to the underlying issue of why some servers, with identical configurations, are returning different index paths. What is this sort of problem called? Anyone know of any reference material that can help here?

---

<div class="post-metadata">

**Author:** ![gmouse](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@gmouse](https://forums.percona.com/u/gmouse)\
**Post date:** [October 13, 2011, 12:13pm UTC](https://forums.percona.com/t/mysql-choosing-wrong-indexes/1685/2 "2011-10-13T12:13:54Z")

</div>

It has to do with the cardinality of the indices. They are always computed (after rebuilding, when analyzing, or when starting MySQL) based on a subset of your rows, not all rows.

Maybe it works to add some rows to your index wrsemployees\_practice\_id, but that will depend on your query.

---

<div class="post-metadata">

**Author:** ![xaprb](https://avatars.discourse-cdn.com/v4/letter/x/49beb7/32.png) [@xaprb](https://forums.percona.com/u/xaprb)\
**Post date:** [October 13, 2011, 1:07pm UTC](https://forums.percona.com/t/mysql-choosing-wrong-indexes/1685/3 "2011-10-13T13:07:08Z")

</div>

I agree with gmouse. Note that what’s changing is not just the index, but the join order. I would suggest using STRAIGHT\_JOIN hints instead of FORCE INDEX. Sometimes scanning a big table and doing a “star schema” join is actually much more efficient than what seems to be a bunch of fast index lookups, due to use of sequential I/O instead of random, but in your case I take it this is not desired.
