建表语句 用实际SQL把所有类型显示出来 SQL需要从上往下执行 有些依赖于上面的SQL
DROP TABLE IF EXISTS teacher
;
CREATE TABLE teacher
(
id
bigint NOT NULL AUTO_INCREMENT,
Tid
varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
Tname
varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
BEGIN;
INSERT INTO teacher
VALUES (1, ‘01’, ‘张三’);
INSERT INTO teacher
VALUES (2, ‘02’, ‘李四’);
INSERT INTO teacher
VALUES (3, ‘03’, ‘王五’);
COMMIT;
DROP TABLE IF EXISTS course
;
CREATE TABLE course
(
id
bigint NOT NULL AUTO_INCREMENT,
Cid
varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
Cname
varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
Tid
varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
BEGIN;
INSERT INTO course
VALUES (1, ‘01’, ‘语文’, ‘02’);
INSERT INTO course
VALUES (2, ‘02’, ‘数学’, ‘01’);
INSERT INTO course
VALUES (3, ‘03’, ‘英语’, ‘03’);
COMMIT;
DROP TABLE IF EXISTS sc
;
CREATE TABLE sc
(
id
bigint NOT NULL AUTO_INCREMENT,
Sid
varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
Cid
varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
score
decimal(18,1) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
BEGIN;
INSERT INTO sc
VALUES (1, ‘01’, ‘01’, 80.0);
INSERT INTO sc
VALUES (2, ‘01’, ‘02’, 90.0);
INSERT INTO sc
VALUES (3, ‘01’, ‘03’, 99.0);
INSERT INTO sc
VALUES (4, ‘02’, ‘01’, 70.0);
INSERT INTO sc
VALUES (5, ‘02’, ‘02’, 60.0);
INSERT INTO sc
VALUES (6, ‘02’, ‘03’, 80.0);
INSERT INTO sc
VALUES (7, ‘03’, ‘01’, 80.0);
INSERT INTO sc
VALUES (8, ‘03’, ‘02’, 80.0);
INSERT INTO sc
VALUES (9, ‘03’, ‘03’, 80.0);
INSERT INTO sc
VALUES (10, ‘04’, ‘01’, 50.0);
INSERT INTO sc
VALUES (11, ‘04’, ‘02’, 30.0);
INSERT INTO sc
VALUES (12, ‘04’, ‘03’, 20.0);
INSERT INTO sc
VALUES (13, ‘05’, ‘01’, 76.0);
INSERT INTO sc
VALUES (14, ‘05’, ‘02’, 87.0);
INSERT INTO sc
VALUES (15, ‘06’, ‘01’, 31.0);
INSERT INTO sc
VALUES (16, ‘06’, ‘03’, 34.0);
INSERT INTO sc
VALUES (17, ‘07’, ‘02’, 89.0);
INSERT INTO sc
VALUES (18, ‘07’, ‘03’, 98.0);
COMMIT;
DROP TABLE IF EXISTS student
;
CREATE TABLE student
(
id
bigint NOT NULL AUTO_INCREMENT,
Sid
varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
Sname
varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
Sage
datetime DEFAULT NULL,
Ssex
varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
BEGIN;
INSERT INTO student
VALUES (1, ‘01’, ‘赵雷’, ‘1990-01-01 00:00:00’, ‘男’);
INSERT INTO student
VALUES (2, ‘02’, ‘钱电’, ‘1990-12-21 00:00:00’, ‘男’);
INSERT INTO student
VALUES (3, ‘02’, ‘钱电’, ‘1990-12-21 00:00:00’, ‘男’);
INSERT INTO student
VALUES (4, ‘03’, ‘孙风’, ‘1990-05-20 00:00:00’, ‘男’);
INSERT INTO student
VALUES (5, ‘04’, ‘李云’, ‘1990-08-06 00:00:00’, ‘男’);
INSERT INTO student
VALUES (6, ‘05’, ‘周梅’, ‘1991-12-01 00:00:00’, ‘女’);
INSERT INTO student
VALUES (7, ‘06’, ‘吴兰’, ‘1992-03-01 00:00:00’, ‘女’);
INSERT INTO student
VALUES (8, ‘07’, ‘郑竹’, ‘1989-07-01 00:00:00’, ‘女’);
INSERT INTO student
VALUES (9, ‘08’, ‘王菊’, ‘1990-01-20 00:00:00’, ‘女’);
COMMIT;
EXPLAIN SELECT * from student;
//获取学生id值最大的学生
EXPLAIN SELECT * from student a where Sid = (select MAX(Sid) from student b);
EXPLAIN SELECT * from student WHERE id > 8
UNION
SELECT * from student WHERE id < 2;
EXPLAIN
SELECT * from sc WHERE Sid in(
SELECT a.Sid from student a WHERE id > 8
UNION
SELECT b.Sid from student b WHERE id < 2);
EXPLAIN SELECT * from student WHERE id > 8
UNION
SELECT * from student WHERE id < 2;
EXPLAIN SELECT * from student a where Sid = (select MAX(Sid) from student b);
特别关注 DEPENDENT SUBQUERY
1 会严重消耗性能
2 不会进行子查询,会先进行外部查询,生成结果集,再在内部进行关联查询
3 子查询的执行效率受制于外层查询的记录数
4 可以尝试改成join查询
EXPLAIN
SELECT * from sc WHERE Sid in(
SELECT a.Sid from student a WHERE id > 8
UNION
SELECT b.Sid from student b WHERE id < 2);
EXPLAIN
SELECT * from student s JOIN (SELECT Sid, max(score) from sc GROUP BY Sid) b ON s.Sid = b.Sid;
EXPLAIN SELECT * from student a where Sid in (select Sid from sc b where score > 80);
效率从低到高排列 最低查询要保证在range级别之上
效率最低 无使用到任何索引,必须要优化
create index Sid on student(Sid
);
explain
SELECT * from student force index(Sid) ORDER BY Sid;
explain
SELECT * from student WHERE id > 4 and id < 8;
只发生在非唯一索引或者是唯一索引的非唯一查找
1.create index Sname on student(Sname
);
explain
SELECT * from student where Sname = ‘赵雷’;
2.create unique index Tid on teacher(Tid
);
#这里不建议在索引列使用null 只为测试用
insert into teacher(Tid,Tname) VALUES(null,‘老徐’);
insert into teacher(Tid,Tname) VALUES(null,‘栩栩’);
explain
SELECT * from teacher where Tid is null;
explain
SELECT * from teacher t right JOIN student s ON s.id = t.id
explain
SELECT * from teacher where Tid = ‘01’;
explain
SELECT day(NOW());
explain
SELECT Sid from student; (Sid在上面已经建立了索引)
表示优化器需要通过索引回表查询数据
使用group by一个没有索引的列
explain
SELECT sum(score) from sc GROUP BY Cid
filesort的意思是只要一个排序无法使用索引来排序,或者 ORDER BY 或 GROUP BY 的列不是来自JOIN语句序列的第一个表
explain
SELECT * from student ORDER BY Sage;
这个值意味着没有好用的索引,新的索引将在联接每的每一行重新估算。N是显示在possible_key 列中索引的位图,并且是冗余的
’#
‘#
’#
‘#
下一篇总结一下对SQL优化作法