MySQL:通过增加索引进行SQL查询优化

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 【实验】 一次非常有意思的SQL优化经历:从30248.271s到0.001s

【实验】

一次非常有意思的SQL优化经历:从30248.271s到0.001s

数据准备

1、新建3张数据表

-- 课程表 数据 100条
drop table course;
create table course(
id int primary key auto_increment,
name varchar(10)
);

-- 学生表 数据 7w条
create table student(
id int primary key auto_increment,
name varchar(10)
);

-- 学生成绩表 数据 700w条
create table student_score(
id int primary key auto_increment,
course_id int,
student_id int,
score int
);

2、使用脚本生成数据

# -- coding: utf-8 --
"""
安装依赖包
pip install requests chinesename pythink pymysql

Windows 登陆mysql: winpty mysql -uroot -p
"""
import random

from chinesename import ChineseName
from pythink import ThinkDatabase

db_url = "mysql://root:123456@localhost:3306/demo?charset=utf8"
think_db = ThinkDatabase(db_url)

course_table = think_db.table("course")
student_table = think_db.table("student")
student_score_table = think_db.table("student_score")

# 准备课程数据
course_list = [{"name": "课程{}".format(i)} for i in range(100)]
ret = course_table.insert(course_list).execute()
print(ret)

# 准备学生数据
cn = ChineseName()
student_list = [{"name": name} for name in cn.getNameGenerator(70000)]
ret = student_table.insert(student_list).execute()
print(ret)

# 准备学生成绩
score_list = []
for i in range(1, 101):
for j in range(1, 70001):
item = {
"course_id": i,
"student_id": j,
"score": random.randint(0, 100)
}

score_list.append(item)

ret = student_score_table.insert(score_list, truncate=20000).execute()
print(ret)

think_db.close()

3、检查数据情况

mysql> select * from  course limit 10;
+----+-------+
| id | name |
+----+-------+
| 1 | 课程0 |
| 2 | 课程1 |
| 3 | 课程2 |
| 4 | 课程3 |
| 5 | 课程4 |
| 6 | 课程5 |
| 7 | 课程6 |
| 8 | 课程7 |
| 9 | 课程8 |
| 10 | 课程9 |
+----+-------+
10 rows in set (0.07 sec)

mysql> select * from student limit 10;
+----+--------+
| id | name |
+----+--------+
| 1 | 司徒筑 |
| 2 | 窦侗 |
| 3 | 毕珊 |
| 4 | 余怠 |
| 5 | 喻献 |
| 6 | 庾莫 |
| 7 | 蒙煮 |
| 8 | 芮佰 |
| 9 | 鄢虹 |
| 10 | 毕纣 |
+----+--------+
10 rows in set (0.05 sec)

mysql> select * from student_score order by id desc limit 10;
+---------+-----------+------------+-------+
| id | course_id | student_id | score |
+---------+-----------+------------+-------+
| 7000000 | 100 | 70000 | 24 |
| 6999999 | 100 | 69999 | 71 |
| 6999998 | 100 | 69998 | 33 |
| 6999997 | 100 | 69997 | 14 |
| 6999996 | 100 | 69996 | 97 |
| 6999995 | 100 | 69995 | 63 |
| 6999994 | 100 | 69994 | 35 |
| 6999993 | 100 | 69993 | 66 |
| 6999992 | 100 | 69992 | 58 |
| 6999991 | 100 | 69991 | 99 |
+---------+-----------+------------+-------+
10 rows in set (0.06 sec)

4、检查数据数量

mysql> select count(*) from student;
+----------+
| count(*) |
+----------+
| 70000 |
+----------+
1 row in set (0.02 sec)

mysql> select count(*) from course;
+----------+
| count(*) |
+----------+
| 100 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from student_score;
+----------+
| count(*) |
+----------+
| 7000000 |
+----------+
1 row in set (4.08 sec)

优化测试

1、直接查询



select * from student 
where id in (
select student_id from student_score where
course_id=1 and score=100
);

不知道为什么 2.7s 就执行完了… 原文中说 执行时间:30248.271s


马上看了下版本号,难道是版本的问题:

我的 : Server version: 5.7.21
原文:mysql 5.6


用 explain 看执行计划 type=all

explain extended
select * from student
where id in (
select student_id from student_score where
course_id=1 and score=100
);


# 执行完上一句之后紧接着执行
mysql> show warnings;

SELECT
`demo`.`student`.`id` AS `id`,
`demo`.`student`.`name` AS `name`
FROM
`demo`.`student` semi
JOIN ( `demo`.`student_score` )
WHERE
(
( `<subquery2>`.`student_id` = `demo`.`student`.`id` )
AND ( `demo`.`student_score`.`score` = 100 )
AND ( `demo`.`student_score`.`course_id` = 1 )
)

2、增加索引

单条大概执行15s

alter table student_score add index INDEX_COURSE_ID(course_id);
alter table student_score add index INDEX_SCORE(score);

加完索引之后执行 0.027s ,速度快了 100倍(2.7 / 0.027)


3、使用 inner join

用了 0.26

select s.id, s.name from student as s inner JOIN student_score as ss 
on s.id=ss.student_id
where ss.course_id=1 and ss.score=100

4、再次优化

执行也是 0.26, 并没有像原文所说的那样 0.001s…难道他的机器比我好?

select s.id, s.name from 
(select * from student_score where course_id=1 and score=100 ) as t
inner join student as s
on s.id=t.student_id

虽然和原文很多不一致的地方,不过也算是一次加索引优化数据库查询的实际操作了


参考文章

一次非常有意思的SQL优化经历:从30248.271s到0.001s

            </div>
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
缓存 关系型数据库 MySQL
MySQL慢查询优化策略
MySQL慢查询优化是一个复杂的过程,需要根据具体的应用场景和数据特点进行。以上策略是提升数据库查询性能的有效途径,但最关键的是对系统进行持续的监控和分析,及时发现并解决性能瓶颈。通过实践这些策略,你可以显著提高MySQL数据库的性能,为用户提供更快的响应时间和更好的体验。
344 10
|
11月前
|
SQL 关系型数据库 MySQL
大厂面试官:聊下 MySQL 慢查询优化、索引优化?
MySQL慢查询优化、索引优化,是必知必备,大厂面试高频,本文深入详解,建议收藏。关注【mikechen的互联网架构】,10年+BAT架构经验分享。
大厂面试官:聊下 MySQL 慢查询优化、索引优化?
|
11月前
|
缓存 关系型数据库 MySQL
MySQL执行计划选择策略:揭秘查询优化的艺术
【10月更文挑战第15天】 在数据库性能优化中,选择最优的执行计划是提升查询效率的关键。MySQL作为一个强大的关系型数据库管理系统,提供了复杂的查询优化器来生成执行计划。本文将深入探讨如何选择合适的执行计划,以及为什么某些计划更优。
353 2
|
5月前
|
缓存 JSON 关系型数据库
MySQL 查询优化分析 - 常用分析方法
本文介绍了MySQL查询优化分析的常用方法EXPLAIN、Optimizer Trace、Profiling和常用监控指标。
|
9月前
|
SQL 存储 关系型数据库
MySQL秘籍之索引与查询优化实战指南
最左前缀原则。不冗余原则。最大选择性原则。所谓前缀索引,说白了就是对文本的前几个字符建立索引(具体是几个字符在建立索引时去指定),比如以产品名称的前 10 位来建索引,这样建立起来的索引更小,查询效率更快!
300 22
 MySQL秘籍之索引与查询优化实战指南
|
11月前
|
SQL 关系型数据库 MySQL
MySQL慢查询优化、索引优化、以及表等优化详解
本文详细介绍了MySQL优化方案,包括索引优化、SQL慢查询优化和数据库表优化,帮助提升数据库性能。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
MySQL慢查询优化、索引优化、以及表等优化详解
|
SQL 关系型数据库 MySQL
从理论到实践,Mysql查询优化剖析(联表查询)
从理论到实践,Mysql查询优化剖析(联表查询)
408 0
|
11月前
|
搜索推荐 关系型数据库 MySQL
mysql like查询优化
通过合理的索引设计、使用全文索引、优化查询结构以及考虑分片和分区表,可以显著提高MySQL中 `LIKE`查询的性能。针对不同的应用场景选择合适的优化策略,能够有效地提升数据库查询效率,减少查询时间。希望这些方法和技巧能帮助您优化MySQL数据库中的模糊查询。
1143 4
|
12月前
|
缓存 关系型数据库 MySQL
MySQL慢查询优化
通过上述方法综合施策,可以显著提升MySQL数据库的查询性能,降低延迟,增强应用系统的整体响应能力。实践中,优化工作是一个持续迭代的过程,需要结合具体应用场景不断调整策略。
811 1
|
存储 缓存 关系型数据库
MySQL 查询优化方法
在数据库应用中,高效的查询性能至关重要。本文探讨了常用的 MySQL 查询优化方法,包括索引优化(选择合适的索引字段、复合索引、定期维护索引)、查询语句优化(避免全表扫描、限制返回行数、避免使用不必要的函数)、表结构优化(选择合适的数据类型、分区表、定期清理无用数据)及数据库配置优化(调整缓存大小、优化存储引擎参数)。通过这些方法,可以显著提高 MySQL 的查询性能,为应用程序提供更好的用户体验。
1006 4

推荐镜像

更多