返回行数比例高不走索引

 在MySQL中,查询返回的行数比例超过总行数20%,执行计划将不走索引,下边看一个例子,查询id大于1的信息。

点击(此处)折叠或打开

  1. mysql> explain select * from test06 where id>1;
  2. +----+-------------+--------+------+---------------+------+---------+------+---------+-------------+
  3. | id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows    | Extra       |
  4. +----+-------------+--------+------+---------------+------+---------+------+---------+-------------+
  5. | 1  | SIMPLE      | test06 | ALL  | idx_test03_id | NULL | NULL    | NULL | 2009559 | Using where |
  6. +----+-------------+--------+------+---------------+------+---------+------+---------+-------------+
  当返回行数比例较高时候,优化器认为全表扫描更省时间,不会使用索引。