Mysql - “Select like” not using index

岁酱吖の 提交于 2019-12-05 02:08:17

MySql can ignore index if it index incurs access to more than 30% of table rows. You could try FORCE INDEX [index_name], it will use index in any case.

The value of sysvar_max_seeks_for_key also affects whether the index is used or not:

Search for similar requests on SO.

Diego Buzzalino

Based on Ubik comment, and data changes, I found that: The Index IS used in these cases:

- explain select * from testTable force index jeje where startDate like 'aaaaaaadsfadsfadsfasafsafsasfsadsfa%';
- explain select * from testTable force index jeje where startDate like 'aaaaaaadsfadsfadsfasafsafsasfsadsfa%';
- explain select * from testTable force index jeje where startDate like 'aaa';

But the index is NOT being used when I use this query:

- explain select * from testTable force index jeje where startDate like 'aaaaaaaaa';

Based on the fact that in startDate column all the values have the same length (9 characters), when I use a query using a LIKE command and a 9 characters constant, PERHAPS MySQL prefer to not use the reason because of some performance algorithm, and goes to the table.

My concern was to see if I was making some kind of mistake on my original tests, but now I think that the index and tests are correct, and that MySQL in some cases decides to not use the index... and I will relay on this.

For me, this is a closed task. If somebody want to add something to the thread, you are welcome.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!