SQL优化经历:从30248.271s到0.001s

用的数据库是mysql5.6,下面简单的介绍下场景

课程表

create table Course(

c_id int PRIMARY KEY,

name varchar(10)

)

数据100条

学生表

create table Student(

id int PRIMARY KEY,

name varchar(10)

)

数据70000条

学生成绩表

CREATE table SC(

sc_id int PRIMARY KEY,

s_id int,

c_id int,

score int

)

数据70w条

查询目的:

查找语文考100分的考生

查询语句:

select s.* from Student s where s.s_id in (select s_id from SC sc where sc.c_id = 0 and sc.score = 100 )

执行时间:30248.271s

为什么这么慢?先来查看下查询计划:

EXPLAIN

select s.* from Student s where s.s_id in (select s_id from SC sc where sc.c_id = 0 and sc.score = 100 )

SQL优化经历:从30248.271s到0.001s

发现没有用到索引,type全是ALL,那么首先想到的就是建立一个索引,建立索引的字段当然是在where条件的字段。

先给sc表的c_id和score建个索引

CREATE index sc_c_id_index on SC(c_id);

CREATE index sc_score_index on SC(score);

再次执行上述查询语句,时间为: 1.054s

快了3w多倍,大大缩短了查询时间,看来索引能极大程度的提高查询效率,看来建索引很有必要,很多时候都忘记建索引了,数据量小的的时候压根没感觉,这优化感觉挺爽。

但是1s的时间还是太长了,还能进行优化吗,仔细看执行计划:

SQL优化经历:从30248.271s到0.001s

查看优化后的sql:

SELECT

`YSB`.`s`.`s_id` AS `s_id`,

`YSB`.`s`.`name` AS `name`

FROM

`YSB`.`Student` `s`

WHERE

< in_optimizer > (

`YSB`.`s`.`s_id` ,< EXISTS > (

SELECT

1

FROM

`YSB`.`SC` `sc`

WHERE

(

(`YSB`.`sc`.`c_id` = 0)

AND (`YSB`.`sc`.`score` = 100)

AND (

< CACHE > (`YSB`.`s`.`s_id`) = `YSB`.`sc`.`s_id`

)

)

)

)

补充:这里有网友问怎么查看优化后的语句

方法如下:

在命令窗口执行

SQL优化经历:从30248.271s到0.001s

有type=all

按照我之前的想法,该sql的执行的顺序应该是先执行子查询

select s_id from SC sc where sc.c_id = 0 and sc.score = 100

SQL优化经历:从30248.271s到0.001s

select s.* from Student s where s.s_id in(7,29,5000)

 

耗时:0.001s

这样就是相当快了啊,Mysql竟然不是先执行里层的查询,而是将sql优化成了exists子句,并出现了EPENDENT SUBQUERY,

mysql是先执行外层查询,再执行里层的查询,这样就要循环70007*11=770077次。

那么改用连接查询呢?

SELECT s.* from

Student s

INNER JOIN SC sc

on sc.s_id = s.s_id

where sc.c_id=0 and sc.score=100

SQL优化经历:从30248.271s到0.001s

CREATE index sc_s_id_index on SC(s_id);

SQL优化经历:从30248.271s到0.001s

SELECT

`YSB`.`s`.`s_id` AS `s_id`,

`YSB`.`s`.`name` AS `name`

FROM

`YSB`.`Student` `s`

JOIN `YSB`.`SC` `sc`

WHERE

(

(

`YSB`.`sc`.`s_id` = `YSB`.`s`.`s_id`

)

AND (`YSB`.`sc`.`score` = 100)

AND (`YSB`.`sc`.`c_id` = 0)

)

SQL优化经历:从30248.271s到0.001s

SELECT

s.*

FROM

(

SELECT

*

FROM

SC sc

WHERE

sc.c_id = 0

AND sc.score = 100

) t

INNER JOIN Student s ON t.s_id = s.s_id

SQL优化经历:从30248.271s到0.001s

SELECT

s.*

FROM

(

SELECT

*

FROM

SC sc

WHERE

sc.c_id = 0

AND sc.score = 100

) t

INNER JOIN Student s ON t.s_id = s.s_id

 SQL优化经历:从30248.271s到0.001s

SELECT s.* from

Student s

INNER JOIN SC sc

on sc.s_id = s.s_id

where sc.c_id=0 and sc.score=100

SQL优化经历:从30248.271s到0.001s

总结:

1.mysql嵌套子查询效率确实比较低

2.可以将其优化成连接查询

3.建立合适的索引

4.学会分析sql执行计划,mysql会对sql进行优化,所以分析执行计划很重要

由于时间问题,这篇文章先写到这里,后续再分享其他的sql优化经历。

个死循环,解不开,这里就不讨论了,每个人都有每个人的想法和评价吧。

 

来源:微信公众号:java版web项目,仅供个人学习,侵权联系我删除

原文地址:https://mp.weixin.qq.com/s?__biz=MzAxNDMwMTMwMw==&mid=2247491169&idx=1&sn=688393c488229e9de82cea408bef7ea1&chksm=9b943f79ace3b66f34d158cd8308216fe0dbb8696e00661bcbd478a3fd09b5d557565270490e&mpshare=1&scene=23&srcid=#rd