MySQL IN子句全表扫描

sra*_*vis 5 mysql sql

我有一个测试表(table1),其中有6条记录。我想基于列(col1)获取多个值的数据。所以我索引了列。现在,如果我在IN子句中传递多个值以选择具有强制索引的所有column(*),则将获得特定记录而不是全表扫描。如果我对选定的列运行相同的查询,我会看到它进行了全表扫描。

我已经读过,在选择查询中使用全选(*)不好。但是在这里,如果我不使用全选(*),将进行全表扫描。我不明白mysql如何读取查询。请帮助我解决此问题。

+----+--------+---------+
| id | col1   | col2    |
+----+--------+---------+
|  1 | 100000 | E100000 |
|  2 | 100001 | E200001 |
|  3 | 100002 | E300002 |
|  4 | 100003 | E400003 |
|  5 | 100004 | E500004 |
|  6 | 100005 | E600005 |
+----+--------+---------+
Run Code Online (Sandbox Code Playgroud)

指数

+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| table1   |          0 | PRIMARY  |            1 | id          | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
| table1   |          1 | col1     |            1 | col1        | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
Run Code Online (Sandbox Code Playgroud)

解释(使用力索引(col1)并选择所有(*)列)

select * from table1 force index(col1) where col1 in ('100000', '100001');

+----+-------------+----------+-------+---------------+-------+---------+------+------+-------------+
| id | select_type | table    | type  | possible_keys | key   | key_len | ref  | rows | Extra       |
+----+-------------+----------+-------+---------------+-------+---------+------+------+-------------+
|  1 | SIMPLE      | table1   | range | col1          | col1  | 10      | NULL |    2 | Using where |
+----+-------------+----------+-------+---------------+-------+---------+------+------+-------------+
Run Code Online (Sandbox Code Playgroud)

解释(使用力索引(col1)并仅选择1列数据而不是全部(*))

select col1 from table1 force index(col1) where col1 in ('100000', '100001');

+----+-------------+----------+-------+---------------+-------+---------+------+------+--------------------------+
| id | select_type | table    | type  | possible_keys | key   | key_len | ref  | rows | Extra                    |
+----+-------------+----------+-------+---------------+-------+---------+------+------+--------------------------+
|  1 | SIMPLE      | table1   | range | col1          | col1  | 10      | NULL |    6 | Using where; Using index |
+----+-------------+----------+-------+---------------+-------+---------+------+------+--------------------------+
Run Code Online (Sandbox Code Playgroud)

Ale*_*zin 5

  1. MySQL优化器发现表太小,全扫描比先搜索索引然后检索数据更有效。
  2. 当您仅选择一列时,MySQL 优化器会发现该列在索引中,并且无需从表中检索数据 - 读取索引就足够了。

优化器如何确定什么更有效?它尝试预测磁盘读取块操作的数量。

正如之前在评论中提到的,在大表上 EXPLAIN 会有所不同。