当表中只有一条记录并且该表使用的存储引擎的统计数据是精确的,比如 MyISAM、Memory,那么对该表的访问方法就是 system。
在连接查询时,如果被驱动表是通过主键或者唯一二级索引列等值匹配的方式进行访问的(如果该主键或者唯一二级索引是联合索引的话,所有的索引列都必须进行等值比较),则对该被驱动表的访问方法就是 eq_ref
mysql> explain select * from single_table inner join single_table2 on single_table.id = single_table2.id; +----+-------------+---------------+------------+--------+---------------+---------+---------+---------------------------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+---------------+------------+--------+---------------+---------+---------+---------------------------+------+----------+-------+ | 1 | SIMPLE | single_table | NULL | ALL | PRIMARY | NULL | NULL | NULL | 1 | 100.00 | NULL | | 1 | SIMPLE | single_table2 | NULL | eq_ref | PRIMARY | PRIMARY | 4 | xiaohaizi.single_table.id | 1 | 100.00 | NULL | +----+-------------+---------------+------------+--------+---------------+---------+---------+---------------------------+------+----------+-------+ 2 rows in set, 1 warning (0.00 sec)搜索条件为二级索引列与常数等值比较,采用二级索引来执行查询的访问方法称为:ref
mysql> explain select * from single_table where key1 = 'abc'; +----+-------------+--------------+------------+------+---------------+----------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------------+------------+------+---------------+----------+---------+-------+------+----------+-------+ | 1 | SIMPLE | single_table | NULL | ref | idx_key1 | idx_key1 | 303 | const | 1 | 100.00 | NULL | +----+-------------+--------------+------------+------+---------------+----------+---------+-------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec)key1 列有二级索引。先在二级索引 idx_key1 查询满足 key1 = ‘abc’ 的 id 值,然后拿着 id 值去聚簇索引上查询。
查询的某一列既有等值查询,又有 NULL 值查询。
mysql> explain select * from single_table where key1 = 'abc' or key1 is null; +----+-------------+--------------+------------+-------------+---------------+----------+---------+-------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------------+------------+-------------+---------------+----------+---------+-------+------+----------+-----------------------+ | 1 | SIMPLE | single_table | NULL | ref_or_null | idx_key1 | idx_key1 | 303 | const | 2 | 100.00 | Using index condition | +----+-------------+--------------+------------+-------------+---------------+----------+---------+-------+------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec)查询索引列的某个范围的值。
mysql> explain select * from single_table where key2 in (1368,1644) or (key2 >= 38 and key2 <=79); +----+-------------+--------------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+ | 1 | SIMPLE | single_table | NULL | range | idx_key2 | idx_key2 | 5 | NULL | 44 | 100.00 | Using index condition | +----+-------------+--------------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec)select 中所有的列正好是某个二级索引使用的列。直接遍历二级索引的叶子节点得到符合条件的数据。不用回表查询聚簇索引。
mysql> explain select key_part1, key_part2, key_part3 from single_table where key_part2 = 'power_to_go'; +----+-------------+--------------+------------+-------+---------------+--------------+---------+------+------+----------+--------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------------+------------+-------+---------------+--------------+---------+------+------+----------+--------------------------+ | 1 | SIMPLE | single_table | NULL | index | NULL | idx_key_part | 909 | NULL | 9921 | 10.00 | Using where; Using index | +----+-------------+--------------+------------+-------+---------------+--------------+---------+------+------+----------+--------------------------+ 1 row in set, 1 warning (0.00 sec)遍历整个聚簇索引。
mysql> explain select * from single_table; +----+-------------+--------------+------------+------+---------------+------+---------+------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+--------------+------------+------+---------------+------+---------+------+------+----------+-------+ | 1 | SIMPLE | single_table | NULL | ALL | NULL | NULL | NULL | NULL | 9921 | 100.00 | NULL | +----+-------------+--------------+------------+------+---------------+------+---------+------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec)