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

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

【实验】

一次非常有意思的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)
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
SQL Oracle 关系型数据库
MySQL的sql_mode模式说明及设置
MySQL的sql_mode模式说明及设置
1665 112
|
SQL 存储 关系型数据库
MySQL体系结构详解:一条SQL查询的旅程
本文深入解析MySQL内部架构,从SQL查询的执行流程到性能优化技巧,涵盖连接建立、查询处理、执行阶段及存储引擎工作机制,帮助开发者理解MySQL运行原理并提升数据库性能。
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1926 152
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1329 156
|
SQL 监控 关系型数据库
SQL优化技巧:让MySQL查询快人一步
本文深入解析了MySQL查询优化的核心技巧,涵盖索引设计、查询重写、分页优化、批量操作、数据类型优化及性能监控等方面,帮助开发者显著提升数据库性能,解决慢查询问题,适用于高并发与大数据场景。
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
SQL 监控 关系型数据库
查寻MySQL或SQL Server的连接数,并配置超时时间和最大连接量
以上步骤提供了直观、实用且易于理解且执行的指导方针来监管和优化数据库服务器配置。务必记得,在做任何重要变更前备份相关配置文件,并确保理解每个参数对系统性能可能产生影响后再做出调节。
1055 11
|
缓存 关系型数据库 MySQL
降低MySQL高CPU使用率的优化策略。
通过上述方法不断地迭代改进,在实际操作中需要根据具体场景做出相对合理判断。每一步改进都需谨慎评估其变动可能导致其他方面问题,在做任何变动前建议先在测试环境验证其效果后再部署到生产环境中去。
477 6
|
存储 SQL 关系型数据库
MySQL 动态分区管理:自动化与优化实践
本文介绍了如何利用 MySQL 的存储过程与事件调度器实现动态分区管理,自动化应对数据增长,提升查询性能与数据管理效率,并详细解析了分区创建、冲突避免及实际应用中的关键注意事项。
633 0

推荐镜像

更多