MySql优化分析
原理
- MYSQL逻辑分层 :连接层 服务层 引擎层 存储层
- InnoDB(默认) :事务优先 (适合高并发操作;行锁)
- MyISAM :性能优先 (表锁)
SQL优化
编写过程:
sql select dinstinct ..from ..join ..on ..where ..group by ...having ..order by ..limit ..解析过程:
sql from .. on.. join ..where ..group by ....having ...select dinstinct ..order by limit ...
索引分类
- 主键索引 : 不能重复。id 不能是null
- 唯一索引 : 不能重复。id 可以是null
- 单值索引 : 单列, age ;一个表可以多个单值索引,name。
- 复合索引 : 多个列构成的索引 (相当于 二级目录 : z: zhao) (name,age) (a,b,c,d,...,n)
分析SQL
- 分析SQL的执行计划 : explain ,可以模拟SQL优化器执行SQL语句,从而让开发人员 知道自己编写的SQL状况
- MySQL查询优化其会干扰我们的优化
- explain字段
| 参数 | 说明 |
|---|---|
| id | 编号 |
| select_type | 查询类型 |
| table | 表 |
| type | 类型 |
| possible_keys | 预测用到的索引 |
| key | 实际使用的索引 |
| key_len | 实际使用索引的长度 |
| ref | 表之间的引用 |
| rows | 通过索引查询到的数据量 |
| Extra | 额外的信息 |
例子数据
create table course
(
cid int(3),
cname varchar(20),
tid int(3)
);
create table teacher
(
tid int(3),
tname varchar(20),
tcid int(3)
); create table teacherCard
(
tcid int(3),
tcdesc varchar(200)
); insert into course values(1,'java',1);
insert into course values(2,'html',1);
insert into course values(3,'sql',2);
insert into course values(4,'web',3); insert into teacher values(1,'tz',1);
insert into teacher values(2,'tw',2);
insert into teacher values(3,'tl',3); insert into teacherCard values(1,'tzdesc') ;
insert into teacherCard values(2,'twdesc') ;
insert into teacherCard values(3,'tldesc') ;
id(编号):
id值相同,从上往下 顺序执行
--查询教授SQL课程的老师的描述(desc)
explain select tc.tcdesc from teacherCard tc,course c,teacher t where c.tid = t.tid
and t.tcid = tc.tcid and c.cname = 'sql' ;
id值不同:id值越大越优先查询 (本质:在嵌套子查询时,先查内层 再查外层)
--将以上 多表查询 转为子查询形式:
explain select tc.tcdesc from teacherCard tc where tc.tcid =
(select t.tcid from teacher t where t.tid =
(select c.tid from course c where c.cname = 'sql')
);
id值有相同,又有不同: id值越大越优先;id值相同,从上往下 顺序执行
--子查询+多表:
explain select t.tname ,tc.tcdesc from teacher t,teacherCard tc where t.tcid= tc.tcid
and t.tid = (select c.tid from course c where cname = 'sql') ;
select_type(查询类型):
- PRIMARY:包含子查询SQL中的 主查询 (最外层)
- SUBQUERY:包含子查询SQL中的 子查询 (非最外层)
- simple:简单查询(不包含子查询、union)
- derived:衍生查询(使用到了临时表)
- union:
- a.在from子查询中只有一张表
explain select cr.cname from ( select * from course where tid in (1,2) ) cr ;
- b.在from子查询中, 如果有table1 union table2 ,则table1 就是derived,table2就是union
explain select cr.cname from ( select * from course where tid = 1 union select * from course where tid = 2 ) cr ;
- union result :那些表之间存在union查询
type(索引类型、类型)
- 例子数据
create table test01
(
tid int(3),
tname varchar(20)
); insert into test01 values(1,'a') ;
commit;
--增加索引
alter table test01 add constraint tid_pk primary key(tid) ;
explain select * from (select * from test01 )t where tid =1 ;
- 执行效率:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
- 常用的类型:system>const>eq_ref>ref>range>index>all
- 要对type进行优化的前提:有索引,其中:system,const只是理想情况;实际能达到 ref>range;
- system(忽略): 只有一条数据的系统表 ;或 衍生表只有一条数据的主查询
- const:仅仅能查到一条数据的SQL ,用于Primary key 或unique索引 (类型 与索引类型有关)
- eq_ref:唯一性索引:对于每个索引键的查询,返回匹配唯一行数据(有且只有1个,不能多 、不能0)
- ref:非唯一性索引,对于每个索引键的查询,返回匹配的所有行(0,多)
- range:检索指定范围的行 ,where后面是一个范围查询(between ,> < >=, 特殊:in有时候会失效 ,从而转为 无索引all)
- index:查询全部索引中数据
- all:查询全部表中的数据
possible_keys
- 可能用到的索引,是一种预测,不准
- 如果 possible_key/key是NULL,则说明没用索引
key
- 实际使用到的索引
key_len
- 作用:用于判断复合索引是否被完全使用 (a,b,c)
ref
- 注意与type中的ref值区分,作用: 指明当前表所 参照的 字段
rows
- 被索引优化查询的 数据个数 (实际通过索引而查询到的 数据个数)
Extra
- using filesort : 性能消耗大;需要“额外”的一次排序(查询) 。常见于 order by 语句中
MySql最大连接数
mysql的最大连接数默认是100, 最大可以达到16384,可以通过max_connections设置
对MySQL语句性能优化的16条经验
- 为查询缓存优化查询
- EXPLAIN 我们的SELECT查询(可以查看执行的行数)
- 当只要一行数据时使用LIMIT 1
- 为搜索字段建立索引
- 在Join表的时候使用相当类型的列,并将其索引
- 千万不要 ORDER BY RAND ()
- 避免SELECT *
- 永远为每张表设置一个ID
- 可以使用ENUM 而不要VARCHAR
- 尽可能的使用NOT NULL
- 固定长度的表会更快
- 垂直分割
- 拆分打的DELETE或INSERT语句
- 越小的列会越快
- 选择正确的存储引擎
- 小心 "永久链接"
MyISAM和InnoDB比较
| MyISAM | InnoDB | |
|---|---|---|
| 事务 | 不支持 | 支持 |
| 数据行锁定 | 不支持,只有表锁定 | 支持 |
| 外键约束 | 不支持 | 支持 |
| 表空间大小 | 相对小 | 相对大 |
| 全文索引 | 支持 | 不支持 |
| 关注点 | 性能(select) | 事务 |
MySql优化分析的更多相关文章
- mysql优化(三)–explain分析sql语句执行效率
mysql优化(三)–explain分析sql语句执行效率 mushu 发布于 11个月前 (06-04) 分类:Mysql 阅读(651) 评论(0) Explain命令在解决数据库性能上是第一推荐 ...
- Mysql 索引优化分析
MySQL索引优化分析 为什么你写的sql查询慢?为什么你建的索引常失效?通过本章内容,你将学会MySQL性能下降的原因,索引的简介,索引创建的原则,explain命令的使用,以及explain输出字 ...
- SQL优化 MySQL版 -分析explain SQL执行计划与笛卡尔积
SQL优化 MySQL版 -分析explain SQL执行计划 作者 Stanley 罗昊 [转载请注明出处和署名,谢谢!] 首先我们先创建一个数据库,数据库中分别写三张表来存储数据; course: ...
- mysql性能优化分析 --- 下篇
概要回顾 之前看过<高性能mysql>对mysql数据库有了系统化的理解,虽然没能达到精通,但有了概念,遇到问题时会有逻辑条理的分析; 这回继上次sql分析结果的一个继续延伸分析,我拿了; ...
- mysql优化:explain分析sql语句执行效率
Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看SQL语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好的优 ...
- mysql优化:慢查询分析、索引配置优化
一.优化概述二.查询与索引优化分析a.性能瓶颈定位show命令慢查询日志explain分析查询profiling分析查询b.索引及查询优化三.配置优化 max_connections back_log ...
- Mysql优化_慢查询开启说明及Mysql慢查询分析工具mysqldumpslow用法讲解
Mysql优化_慢查询开启说明及Mysql慢查询分析工具mysqldumpslow用法讲解 Mysql慢查询开启 Mysql的查询讯日志是Mysql提供的一种日志记录,它用来记录在Mysql中响应 ...
- mySql索引优化分析
MySQL索引优化分析 为什么你写的sql查询慢?为什么你建的索引常失效?通过本章内容,你将学会MySQL性能下降的原因,索引的简介,索引创建的原则,explain命令的使用,以及explain输出字 ...
- MySQL · 特性分析 · 优化器 MRR & BKA【转】
MySQL · 特性分析 · 优化器 MRR & BKA 上一篇文章咱们对 ICP 进行了一次全面的分析,本篇文章小编继续为大家分析优化器的另外两个选项: MRR & batched_ ...
随机推荐
- Kriging插值计算
参考论文: http://people.ku.edu/~gbohling/cpe940 # -*- coding: utf-8 -*- # ----------------------- ...
- linux 软件包安装方式选择、安装位置、源码包安装
对外提供服务,比如apache,应使用源码包安装对内提供服务,比如gcc,只是我自己使用,使用rpm包安装 rpm包不需要指定安装位置,源码包的安装需要手动指定安装位置 rpm包默认安装位置/etc/ ...
- mysql 日期时间类型 自动转型 及 运算
日期时间类型自动转型 -- now().字符串.数字转datetime类型 create table t(dt datetime);insert into t values(now());insert ...
- Pig filter用法举例
filter:过滤数据,只有符合特定条件的数据才会被保留下来,然后进入下一个数据流. 1)等值比较 filter data by $0 == 1 filter data by $0 != 1 ...
- 2.Spring——maven依赖
1.spring-core 2.spring-context 3.spring-orm 4.spring-web spring-webmvc others pmo demo1 pmo demo2 1. ...
- ansible 一键部署
start install: rpm -Uvh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm yum in ...
- Jmeter入门--脚本录制
一.Badboy脚本录制(推荐) 下载地址:http://www.badboy.com.au/download/add,下载完成后直接安装即可. Badboy是一个强大的工具,旨在帮助测试和开发复杂的 ...
- [翻译] popping
https://github.com/schneiderandre/popping Popping is a collection of animation examples for iOS apps ...
- Swift-EasingAnimation
Swift-EasingAnimation 效果 http://gizma.com/easing/ 源码 https://github.com/YouXianMing/UI-Component-Col ...
- linux下jira搭建&破解(转自:https://www.cnblogs.com/zpw-1/p/9553358.html)
写在前面 网络类似文章不少,但是同样的路,别人走可能一马平川,自己走可能磕磕绊绊.记录一下自己搭建过程的一路踩坑历程[目前还记得的]. 一.环境准备 1,jira7.3的运行是依赖java环境的,也就 ...