1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| 查询的类型:全表扫描和索引扫描 全盘扫描查询低效,性能差,不走索引。
如果是全表扫描type字段显示:ALL(见上图) 如果是索引扫描type字段显示:INDEX、RANGE、REF、EQ_REF、CONST(SYSTEM)、NULL #从左到右性能依次变好
常见使用场景: ALL: select * from t1; select * from t1 where xxx; #where条件无索引 select * from t1 where != not in like '%xxx%'; INDEX: select id from city; RANGE: > ,< ,>= ,<= ,like ,in ,or
eq_ref:多表连接
const:主键和唯一键的等值查询
<1>.INDEX:全索引扫描
1>>. 查询需要获取整个索引树种的值时: mysql> desc student; + | Field | Type | Null | Key | Default | Extra | + | sno | int(11) | NO | PRI | NULL | auto_increment | | sname | varchar(20) | NO | | NULL | | | sage | tinyint(3) unsigned | NO | | NULL | | | ssex | enum('f','m') | NO | | m | | + 4 rows in set (0.00 sec)
mysql> desc select sno from student; + | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + | 1 | SIMPLE | student | NULL | index | NULL | PRIMARY | 4 | NULL | 10 | 100.00 | Using index | + 1 row in set, 1 warning (0.00 sec)
2>>. 联合索引中,任何一个非最左列作为查询条件时: idx_a_b_c(a,b,c)
SELECT * FROM t1 WHERE b SELECT * FROM t1 WHERE c
<2>.RANGE :索引范围扫描
辅助索引> < >= <= LIKE IN OR 主键 <> NOT IN 对于辅助索引来讲,!=和not in等语句是不走索引的 对于主键列来讲,!=和not in等语句是走RANGE
例子: 1. DESC SELECT * FROM city WHERE id<5; 2. DESC SELECT * FROM city WHERE countrycode LIKE 'CH%'; 3. DESC SELECT * FROM city WHERE countrycode IN ('CHN','USA');
注意: 1和2例子中,可以享受到B+树的优势,但是3例子中是不能享受的. 所以,我们可以将3号列子改写:
DESC SELECT * FROM city WHERE countrycode='CHN' UNION ALL SELECT * FROM city WHERE countrycode='USA';
<3>.ref: 辅助索引等值查询 非唯一性索引,等值查询 DESC SELECT * FROM city WHERE countrycode='CHN';
<4>.eq_ref:在多表连接时,子表使用主键列或唯一列作为连接条件 例子: a join b 驱动表:a 子表:b
在多表连接时,连接条件使用了唯一索引(uk pK)
DESC SELECT b.name,a.name FROM city AS a JOIN country AS b ON a.countrycode=b.code WHERE a.population <100;
<5>.system,const :主键或唯一键的等值查询 唯一索引的等值查询 DESC SELECT * FROM city WHERE id=10; <6>.null :所查数据在表内没有,索引里也没有,因为没有值所以不用遍历,性能也最好 例子: stu表内tel列没有160这个值 mysql> select * from stu; + | id | sname | sage | sgender | sfz | intime | tel | + | 1 | zs1 | 0 | n | 1 | 2021-09-26 14:07:35 | 110 | | 2 | zs1 | 0 | n | 2 | 2021-09-26 14:07:42 | 120 | | 3 | zs1 | 0 | n | 3 | 2021-09-26 14:07:48 | 130 | + 3 rows in set (0.00 sec)
mysql> desc select * from stu where tel='160'; + | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | + | 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | no matching row in const table | + 1 row in set, 1 warning (0.00 sec)
|