MySQL-use index、ignore index、force index

MySQL-use index、ignore index、force index

起男 696 2021-02-26

MySQL-use index、ignore index、force index

语法

select ... from 表名 {use|ignore|force} index(索引名) [where ...]

作用

  • use index:建议MySQL使用指定的索引
  • ignore index:禁止查询优化器使用指定的索引
  • force index:强制MySQL使用特定的索引

use和force的区别:

如果使用use index,则优化器可以使用此索引,但如果认为更快,则可以使用表扫描

如果使用force index,那么即使认为表扫描更有效,也会使用此索引,只有没有办法使用索引查找时,才会使用表扫描