010--Explain实战详解:ref分析

1.ref理解

  • ref:显示索引的哪一列被使用了,有时候会是一个常量:表示哪些列或常量被用于用于查找索引列上的值

2.ref分析

  • 库.表.字段
  • 常量

3.key-len案例

mysql> explain select * from t1,t2 where t1.id = t2.id and t1.id='1';
+----+-------------+-------+-------+-----------------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys         | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+-----------------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t1    | const | PRIMARY,index_id_name | PRIMARY | 4       | const |    1 |       |
|  1 | SIMPLE      | t2    | const | PRIMARY               | PRIMARY | 4       | const |    1 |       |
+----+-------------+-------+-------+-----------------------+---------+---------+-------+------+-------+
2 rows in set (0.00 sec)
mysql> explain SELECT * from t1,t2 where t1.id = t2.id and t1.`name` = 'downeyjr_1';
+----+-------------+-------+--------+-----------------------+---------+---------+--------------+------+-------------+
| id | select_type | table | type   | possible_keys         | key     | key_len | ref          | rows | Extra       |
+----+-------------+-------+--------+-----------------------+---------+---------+--------------+------+-------------+
|  1 | SIMPLE      | t2    | ALL    | PRIMARY               | NULL    | NULL    | NULL         |    1 |             |
|  1 | SIMPLE      | t1    | eq_ref | PRIMARY,index_id_name | PRIMARY | 4       | mysql1.t2.id |    1 | Using where |
+----+-------------+-------+--------+-----------------------+---------+---------+--------------+------+-------------+
2 rows in set (0.00 sec)
mysql> explain SELECT * from t1,t3 where t1.id = t3.id and t3.age='10';
+----+-------------+-------+-------+-----------------------+---------------+---------+------+------+--------------------------------+
| id | select_type | table | type  | possible_keys         | key           | key_len | ref  | rows | Extra                          |
+----+-------------+-------+-------+-----------------------+---------------+---------+------+------+--------------------------------+
|  1 | SIMPLE      | t1    | index | PRIMARY,index_id_name | index_id_name | 37      | NULL |    3 | Using index                    |
|  1 | SIMPLE      | t3    | ALL   | PRIMARY               | NULL          | NULL    | NULL |    3 | Using where; Using join buffer |
+----+-------------+-------+-------+-----------------------+---------------+---------+------+------+--------------------------------+
2 rows in set (0.00 sec)

你可能感兴趣的:(010--Explain实战详解:ref分析)