Home > Database > Mysql Tutorial > body text

mysql索引失效的五种情况分析

WBOY
Release: 2016-06-07 16:28:26
Original
1175 people have browsed it

索引并不是时时都会生效的,比如以下几种情况,将导致索引失效: 如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因) 注意:要想使用or,又想让索引生效,只能将or条件中的每个列都加上索引 2.对于多列索引,不是使用的第一部分

索引并不是时时都会生效的,比如以下几种情况,将导致索引失效:

如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因)

  

  注意:要想使用or,又想让索引生效,只能将or条件中的每个列都加上索引

  2.对于多列索引,不是使用的第一部分,则不会使用索引

  3.like查询是以%开头

  4.如果列类型是字符串,那一定要在条件中将数据使用引号引用起来,否则不使用索引

  

  5.如果mysql估计使用全表扫描要比使用索引快,则不使用索引

此外,查看索引的使用情况

show status like ‘Handler_read%';
大家可以注意:
handler_read_key:这个值越高越好,越高表示使用索引查询到的次数 handler_read_rnd_next:这个值越高,说明查询低效

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!