-- 关于索引一点理解
1从整体理解联合索引,ORDER BY以及GROUP BY之间的过程
-- 改语句很有代表性,自己分析,理解为什么
CREATE TABLE t6(
c1 CHAR(1) NOT NULL DEFAULT ‘‘,
c2 CHAR(1) NOT NULL DEFAULT ‘‘,
c3 CHAR(1) NOT NULL DEFAULT ‘‘,
c4 CHAR(1) NOT NULL DEFAULT ‘‘,
c5 CHAR(1) NOT NULL DEFAULT ‘‘,
KEY(c1,c2,c3,c4)
)ENGINE INNODB CHARSET utf8;
INSERT INTO t6 VALUES(‘a‘,‘b‘,‘c‘,‘d‘,‘e‘);
EXPLAIN
SELECT * FROM t6 WHERE c1=‘a‘ AND c2=‘b‘ AND c4>‘a‘ AND c3=‘c‘;
EXPLAIN
SELECT * FROM t6 WHERE c1=‘a‘ AND c2=‘b‘ AND c4=‘a‘ ORDER BY c3;
EXPLAIN
SELECT * FROM t6 WHERE c1=‘a‘ AND c2=‘b‘ AND c4=‘a‘ ORDER BY c5;
EXPLAIN
SELECT * FROM t6 WHERE c1=‘a‘ AND c5=‘a‘ ORDER BY c2,c3;
EXPLAIN
SELECT * FROM t6 WHERE c1=‘a‘ AND c5=‘a‘ ORDER BY c3,c2;
EXPLAIN
SELECT * FROM t6 WHERE c1=‘a‘ AND c2=‘b‘ AND c5=‘a‘ ORDER BY c2,c3;
EXPLAIN
SELECT * FROM t6 WHERE c1=‘a‘ AND c2=‘b‘ AND c5=‘a‘ ORDER BY c3,c2;-- [改写为order by c3,‘b‘],故直接修改为order by c3,语句写的冗余
EXPLAIN
SELECT * FROM t6 WHERE c1=‘a‘ AND c4=‘d‘ GROUP BY c2,c3;
EXPLAIN
SELECT * FROM t6 WHERE c1=‘a‘ AND c4=‘d‘ GROUP BY c3,c2;
-- 有时候使用了索引但是不会计入到key_len的长度当中,但是我们发现他们区别在extra中 使用索引的using where,但是没有使用索引的using tempary;using filsort
理解图片
2关于EXPLAIN中ROWS自己的一点认识
-- 需要扫描的行数,代表必须
EXPLAIN
SELECT * FROM ta WHERE rid1=429 AND rid2=36
SELECT * FROM ta WHERE rid2=36-- 12
-- 相当于先通过索引找到12行符合条件的语句,所以扫描行数肯定为12,然后回表使用where进行过滤,得到最终的6条
-- 所以建立更好的索引,可以减少扫描行数,索引功能作用大大的
一条SQL语句的执行过程
3关于mysql是如何进行group by的分析结构
所以建立有效的索引,可以避免Using tempary;Using filesort;