select * from t1;
但是,如果查询的列对象,全部是某一个索引的一部分,则MySQL会自动识别,查询执行计划也会把全表扫描转变为索引扫描.
如:
mysql> show create table t1;
+-------+-----------------------------
| Table | Create Table
+-------+-----------------------------
| t1 | CREATE TABLE `t1` (
`a` int(11) NOT NULL,
PRIMARY KEY (`a`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
+-------+-----------------------------
INSERT INTO t1 VALUES (1), (2), (3);
查看执行计划:
mysql> explain select * from t1;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | t1 | NULL | index | NULL | PRIMARY | 4 | NULL | 4 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
MySQL使用索引扫描快速获取数据,这是MySQL把列对象和索引配对识别后的一种优化手段.
内部实现时,依赖了TABLE结构体上的key_read和no_keyread:
struct TABLE
{
...
my_bool key_read;
my_bool no_keyread;
...
}
相关代码可以参考:void JOIN::adjust_access_methods()