实践一次有趣的sql优化
#课程表
create table Course(
c_id int PRIMARY KEY,
name varchar(10)
)
#增加课程表100条数据
DROP PROCEDURE IF EXISTS insert_Course;
DELIMITER $
CREATE PROCEDURE insert_Course()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i<=100 DO
INSERT INTO Course(`c_id`,`name`) VALUES(i, CONCAT('语文',i+''));
SET i = i+1;
END WHILE;
END $
CALL insert_Course();
运行耗时

课程数据

#学生表
create table Student(
s_id int PRIMARY KEY,
name varchar(10)
)
#学生表增加70000条数据
DROP PROCEDURE IF EXISTS insert_Student;
DELIMITER $
CREATE PROCEDURE insert_Student()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i<=70000 DO
INSERT INTO Student(`s_id`,`name`) VALUES(i, CONCAT('张三',i+''));
SET i = i+1;
END WHILE;
END $
CALL insert_Student();
#成绩表
CREATE table Result(
r_id int PRIMARY KEY,
s_id int,
c_id int,
score int
)
#成绩表增加70W条数据
DROP PROCEDURE IF EXISTS insert_Result;
DELIMITER $
CREATE PROCEDURE insert_Result()
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE sNum INT DEFAULT 1;
DECLARE cNum INT DEFAULT 1;
WHILE i<=700000 DO
if (sNum%70000 = 0) THEN
set sNum = 1;
elseif (cNum%100 = 0) THEN
set cNum = 1;
end if;
INSERT INTO Result(`r_id`,`s_id`,`c_id`,`score`) VALUES(i,sNum ,cNum , (RAND()*99)+1);
SET i = i+1;
SET sNum = sNum+1;
SET cNum = cNum+1;
END WHILE;
END $
CALL insert_Result();
#查询语文1考100分的考生
select s.* from Student s where s.s_id in
(select s_id from Result r where r.c_id = 1 and r.score = 100)
EXPLAIN
select s.* from Student s where s.s_id in
(select s_id from Result r where r.c_id = 1 and r.score = 100)
CREATE index result_c_id_index on Result(c_id); CREATE index result_score_index on Result(score);
SELECT
`example`.`s`.`s_id` AS `s_id`,
`example`.`s`.`name` AS `name`
FROM
`example`.`Student` `s` semi
JOIN ( `example`.`Result` `r` )
WHERE
(
( `example`.`s`.`s_id` = `<subquery2>`.`s_id` )
AND ( `example`.`r`.`score` = 100 )
AND ( `example`.`r`.`c_id` = 1 )
)
#先执行
EXPLAIN
select s.* from Student s where s.s_id in
(select s_id from Result r where r.c_id = 1 and r.score = 100);
#在执行
show warnings;
select s_id from Result r where r.c_id = 1 and r.score = 100
select s.* from Student s where s.s_id in
(12871,40987,46729,61381,3955,10687,14047,26917,28897,31174,38896,56518,10774,25030,9778,12544,24721,27295,60361,
38479,46990,66988,6790,35995,46192,47578,58171,63220,6685,67372,46279,64693)
DROP index result_c_id_index on Result;
DROP index result_score_index on Result;
select s.* from
Student s
INNER JOIN Result r
on r.s_id = s.s_id
where r.c_id = 1 and r.score = 100;
CREATE index result_s_id_index on Result(s_id);
show index from Result;
SELECT
`example`.`s`.`s_id` AS `s_id`,
`example`.`s`.`name` AS `name`
FROM
`example`.`Student` `s`
JOIN `example`.`Result` `r`
WHERE
(
( `example`.`s`.`s_id` = `example`.`r`.`s_id` )
AND ( `example`.`r`.`score` = 100 )
AND ( `example`.`r`.`c_id` = 1 )
)
DROP index result_s_id_index on Result;
SELECT
s.*
FROM
(
SELECT * FROM Result r WHERE r.c_id = 1 AND r.score = 100
) t
INNER JOIN Student s ON t.s_id = s.s_id
CREATE index result_c_id_index on Result(c_id);
CREATE index result_score_index on Result(score);
SELECT
s.*
FROM
(
SELECT * FROM Result r WHERE r.c_id = 1 AND r.score = 100
) t
INNER JOIN Student s ON t.s_id = s.s_id
EXPLAIN
select s.* from
Student s
INNER JOIN Result r
on r.s_id = s.s_id
where r.c_id = 1 and r.score = 100;
DROP PROCEDURE IF EXISTS insert_Result_TO300W;
DELIMITER $
CREATE PROCEDURE insert_Result_TO300W()
BEGIN
DECLARE i INT DEFAULT 700001;
DECLARE sNum INT DEFAULT 1;
DECLARE cNum INT DEFAULT 1;
WHILE i<=3000000 DO
INSERT INTO Result(`r_id`,`s_id`,`c_id`,`score`)
VALUES(i,(RAND()*69999)+1 ,(RAND()*99)+1 , (RAND()*99)+1);
SET i = i+1;
END WHILE;
END $
CALL insert_Result_TO300W();
show index from Result;
select s.* from
Student s
INNER JOIN Result r
on r.s_id = s.s_id
where r.c_id = 81 and r.score = 84;
DROP index result_c_id_index on Result;
DROP index result_score_index on Result;
CREATE index result_c_id_score_index on Result(c_id,score);
- MySQL 嵌套子查询效率确实比较低
- 可以将其优化成连接查询
- 连接表时,可以先用 where 条件对表进行过滤,然后做表连接(虽然 MySQL 会对连表语句做优化)
- 建立合适的索引,必要时建立多列联合索引
- 学会分析 sql 执行计划,mysql 会对 sql 进行优化,所有分析计划很重要
索引优化
单列索引
select * from user_test_copy where sex = 2 and type = 2 and age = 10
CREATE index user_test_index_sex on user_test_copy(sex);
CREATE index user_test_index_type on user_test_copy(type);
CREATE index user_test_index_age on user_test_copy(age);
多列索引
create index user_test_index_sex_type_age on user_test(sex,type,age);
select * from user_test where sex = 2 and type = 2 and age = 10
最左前缀
select * from user_test where sex = 2
select * from user_test where sex = 2 and type = 2
select * from user_test where sex = 2 and age = 10
索引覆盖
select sex,type,age from user_test where sex = 2 and type = 2 and age = 10
排序
select * from user_test where sex = 2 and type = 2 ORDER BY user_name
select * from user_test where sex = 2 and type = 2 ORDER BY user_name
- 列类型尽量定义成数值类型,且长度尽可能短,如主键和外键,类型字段等等
- 建立单列索引
- 根据需要建立多列联合索引
- 当单个列过滤之后还有很多数据,那么索引的效率将会比较低,即列的区分度较低,那么如果在多个列上建立索引,那么多个列的区分度就大多了,将会有显著的效率提高。
- 根据业务场景建立覆盖索引
- 只查询业务需要的字段,如果这些字段被索引覆盖,将极大的提高查询效率
- 多表连接的字段上需要建立索引
- 这样可以极大的提高表连接的效率
- where条件字段上需要建立索引
- 排序字段上需要建立索引
- 分组字段上需要建立索引
- Where条件上不要使用运算函数,以免索引失效
·END·
实践一次有趣的sql优化的更多相关文章
- 一次非常有趣的 SQL 优化经历
阅读本文大概需要 6 分钟. 前言 在网上刷到一篇数据库优化的文章,自己也来研究一波. 场景 数据库版本:5.7.25 ,运行在虚拟机中. 课程表 #课程表 create table Course( ...
- SQL 优化经历
一次非常有趣的 SQL 优化经历 阅读本文大概需要 6 分钟. 前言 在网上刷到一篇数据库优化的文章,自己也来研究一波. 场景 数据库版本:5.7.25 ,运行在虚拟机中. 课程表 #课程表 cr ...
- 史上最全存储引擎、索引使用及SQL优化的实践
史上最全存储引擎.索引使用及SQL优化的实践 1 MySQL的体系结构概述 2. 存储引擎 2.1 存储引擎概述 2.2 各种存储引擎特性 2.2.1 InnoDB 2.2.2 MyISAM 3. 优 ...
- 京东云TiDB SQL优化的最佳实践
京东云TiDB SQL层的背景介绍 从总体上概括 TiDB 和 MySQL 兼容策略,如下表: SQL层的架构 用户的 SQL 请求会直接或者通过 Load Balancer 发送到 京东云TiDB ...
- MySQL最全存储引擎、索引使用及SQL优化的实践
1 MySQL的体系结构概述 整个MySQL Server由以下组成 :Connection Pool :连接池组件Management Services & Utilities :管理服务和 ...
- sql优化最佳实践
1.选择最有效率的表连接顺序 首先要明白一点就是SQL 的语法顺序和执行顺序是不一致的 SQL的语法顺序: select [distinct] ....from ....[xxx join][o ...
- SQL优化案例—— RowNumber分页
将业务语句翻译成SQL语句不仅是一门技术,还是一门艺术. 下面拿我们程序开发工程师最常用的ROW_NUMBER()分页作为一个典型案例来说明. 先来看看我们最常见的分页的样子: WITH CTE AS ...
- SQL 优化总结
SQL 优化总结 (一)SQL Server 关键的内置表.视图 1. sysobjects SELECT name as '函数名称',xtype as XType FROM s ...
- 基于Oracle的SQL优化(社区万众期待 数据库优化扛鼎巨著)
基于Oracle的SQL优化(社区万众期待数据库优化扛鼎巨著) 崔华 编 ISBN 978-7-121-21758-6 2014年1月出版 定价:128.00元 856页 16开 编辑推荐 本土O ...
随机推荐
- Inheritance and the prototype chain 继承和 原型 链
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain Inherita ...
- office(CVE-2012-0158)漏洞分析报告
2019/9/12 1.漏洞复现 ①发现崩溃 ②找到漏洞所在的函数,下断点,重新跑起来,单步调试,找到栈被改写的地方 ③分析该函数 把MSCOMCTL拖入IDA,查看该函数代码 ④查看调用栈,回溯. ...
- Redis的安装并配置快捷启动
Redis 安装 1.下载 wget http://download.redis.io/releases/redis-5.0.5.tar.gz 2.解压 tar -zxvf redis-5.0.5.t ...
- jquery 版本冲突解决办法
<!-- 引入1.6.4版的jq --> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jq ...
- Power Tower
题目大意:给出一段长为 \(n\) 的序列 \(a_1,a_2,\cdots,a_n\) ,一个模数 \(m\) .每次询问给定 \(l,r\) 求 \(a_l^{{a_{l+1}^\cdots}^{ ...
- 面向对象-接口(interface)实战案例
面向对象-接口(interface)实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.接口(interface)概述 1>.接口的语法格式 接口时抽象类的一种特殊体 ...
- 洛谷 P1504 积木城堡
题目传送门 解题思路: 01背包. AC代码: #include<iostream> #include<cstdio> #include<vector> using ...
- 调用servlet报The requested resource is not available.
调用servlet的时候经常有这种报错,一般来说我直到现在遇到的情况大致有以下几类: 1.参数写错了 在新创建的servlet文件中有这么一行代码,“/LoginCheck”这个一定要和form表单中 ...
- HTML条件注释判断<!--[if IE] ![endif]-->
很多网页中会见到这样的代码: <!--[if IE 7]> <![endif]--> /*或者*/ <!--[if lt IE 9]> <![endif]-- ...
- C++命名规范——谷歌规范
1.文件命名规则 文件名全部小写,可以含下划线或连字符,按项目约定命名,且尽量保证文件名明确.比如: cmd_save_player_info_class.cc ,my_use_full_class. ...