1.优化案例1——单表

案例使用的表:

-- 31讲----------------------------------------------------------
create table if not exists `article`(`id` int(10) unsigned not null primary key auto_increment,`author_id` int(10) unsigned not null ,`category_id` int(10) unsigned not null,`views` int(10) unsigned not null,`comments` int(10) unsigned not null,`title` varbinary(255) not null,`content` text not null
);insert into `article`(`author_id`,`category_id`,`views`,`comments`,`title`,`content` ) values
(1,1,1,1,'1','1'),
(2,2,2,2,'2','2'),
(1,1,3,3,'3','3');

.

由于type是all,而且Extra是使用了using filesort,如何优化?

第一种:将category_id,comments,views设为索引;优化成range

第二种方式:将category_id,views设为索引,此时type变为ref,而using filesort也消失了

优化案例2——多表

-- 32讲----------------------------------------------------------
create table if not exists `class`(`id` int(10) unsigned not null  auto_increment,`card` int(10) unsigned not null,primary key (`id`)
);
create table if not exists `book`(`bookid` int(10) unsigned not null  auto_increment,`card` int(10) unsigned not null,primary key (`bookid`)
);insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));
insert into `class`(card ) values (floor(1+(rand()*20)));

原因分析:由于在左连接中,left join 条件用于确定如何从右表中搜索行,左边表的行一定都存在,索引右边表才是我们关键的地方。

索引优化案例3:三表优化

用到的表:

create table if not exists `phone`(`phoneid` int(10) unsigned not null  auto_increment,`card` int(10) unsigned not null,primary key (`phoneid`)
);insert into `phone`(card ) values (floor(1+(rand()*20)));
insert into `phone`(card ) values (floor(1+(rand()*20)));
insert into `phone`(card ) values (floor(1+(rand()*20)));
insert into `phone`(card ) values (floor(1+(rand()*20)));
insert into `phone`(card ) values (floor(1+(rand()*20)));
insert into `phone`(card ) values (floor(1+(rand()*20)));
insert into `phone`(card ) values (floor(1+(rand()*20)));

2.索引优化

主要讨论下面索引失效的原因:

涉及到的表结构:

CREATE TABLE `staffs`(id int primary key auto_increment,name varchar(24) not null default "" comment'姓名',age int not null default 0 comment '年龄',pos varchar(20) not null default ""  comment'职位',add_time timestamp not null default current_timestamp comment '入职时间')charset utf8 comment '员工记录表';insert into staffs(name,age,pos,add_time) values('z3',22,'manage',now());
insert into staffs(name,age,pos,add_time) values('july',23,'dev',now());
insert into staffs(name,age,pos,add_time) values('2000',23,'dev',now());alter table staffs add index idx_staffs_nameAgePos(name,age,pos);

①全值匹配    :精度越高,所使用key_len越大,使用了覆盖索引;

②最佳左前缀法则  如果索引了多列,要遵守最左前缀法则。指的是查询从索引的最左前列开始并且不跳过索引中的列。

 演示 ,没有用到索引的情况 :

③不要在索引列上做任何操作(计算、函数、(自动或者手动)类型转换),会导致索引失效而转向全表扫描。

④存储引擎不能使用索引中范围条件右边的列。

也就是说在原来表的索引是(name,age,pos),在下面的查询中age>23,是索引的第二个字段,它的右边pos列就失效了

⑤尽量使用覆盖索引(只访问索引的查询(索引列和查询列一致)),减少select *

⑥mysql使用不等于(!=或者<>)的时候无法使用索引导致全表扫描;

下面是用了,覆盖索引和非覆盖索引

⑦is null,is not null也无法使用索引

⑧like通配符开头(“%abc”)mysql索引失效会变成全表扫描的操作。

最好将like的通配符放在最右边

如果like的通配符必须放在开头,可以结合覆盖索引来优化;

涉及到的表:

CREATE TABLE `tb1_user`(id int not null auto_increment,name varchar(20)  default null,age int(11) default null ,email varchar(20)  default null ,primary key(`id`))engine=innodb auto_increment=1 default charset=utf8;insert into tb1_user(name,age,email) values ('1aa1','21','b@163.com');
insert into tb1_user(name,age,email) values ('2aa2','222','a@163.com');
insert into tb1_user(name,age,email) values ('3aa3','265','c@163.com');
insert into tb1_user(name,age,email) values ('4aa4','21','d@163.com');

建立索引:

 create index idx_nameAge on tb1_user(name,age);

⑨字符串不加单引号索引会失效

⑩少用or,用它会使索引失效。 

总结:

对于上面like案例的验证:索引是(name,age,pos)

mysql进阶(三)31-43相关推荐

  1. mysql进阶(三)游标简易教程

    mysql游标简易教程 从mysql V5.5开始,进行了一次大的改变,就是将InnoDB作为默认的存储引擎.InnoDB支持事务,而且拥有相关的RDBMS特性:ACID事务支持,数据完整性(支持外键 ...

  2. mysql进阶(三十四)Field ‘id‘ doesn‘t have a default value 错误解决方法

    在做项目开发过程中,实现新建表结构数据新增,前端点击新增操作后,发现后台报错信息如下:Field 'id' doesn't have a default value 后经过走查发现,表结构中存在id字 ...

  3. Mysql进阶学习(三)排序查询与常见函数

    Mysql进阶学习(三)排序查询与常见函数 一.进阶3:排序查询 1.语法: 2.特点: 3.排序方式 3.1.按单个字段排序 3.2.添加筛选条件再排序 案例:查询部门编号>=90的员工信息, ...

  4. mysql进阶教程pdf_Mysql基础到进阶精品视频教程附讲义文档 91课

    Mysql基础到进阶精品视频教程附讲义文档 91课 程介绍 Mysql基础 本章主要是php开发中Mysql基础知识的学习,包括MySQL的简单介绍和安装.MySQL管理工具的使用.表的建立.数据的查 ...

  5. 【MySQL进阶】MySQL事务隔离与锁机制底层原理万字总结(建议收藏!!)

    [MySQL进阶]MySQL事务隔离与锁机制底层原理万字总结(建议收藏!!) 参考资料: 美团技术团队:Innodb中事务隔离级别和锁的关系 数据库的锁,到底锁的是什么? 阿里面试:说说一致性读实现原 ...

  6. mysql建立司机表,MYSQL进阶,生手变司机

    MYSQL进阶,新手变司机 一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. 1 S ...

  7. 超详细图解!【MySQL进阶篇】存储过程,视图,索引,函数,触发器

    超详细图解![MySQL进阶篇]存储过程,视图,索引,函数,触发器 1.1 下载Linux 安装包 1.2 安装MySQL 1.3 启动 MySQL 服务 1.4 登录MySQL 2\. 索引 2.1 ...

  8. MySQL进阶篇(03):合理的使用索引结构和查询

    本文源码:GitHub·点这里 || GitEE·点这里 一.高性能索引 1.查询性能问题 在MySQL使用的过程中,所谓的性能问题,在大部分的场景下都是指查询的性能,导致查询缓慢的根本原因是数据量的 ...

  9. MySQL进阶篇(02):索引体系划分,B-Tree结构说明

    本文源码:GitHub·点这里 || GitEE·点这里 一.索引简介 1.基本概念 首先要明确索引是什么:索引是一种数据结构,数据结构是计算机存储.组织数据的方式,是指相互之间存在一种或多种特定关系 ...

  10. docker登录密码错误_Docker安装运行Mysql 5.7.31容器并修改数据库密码

    一.安装Docker 参见文章:https://blog.csdn.net/y1534414425/article/details/107872715 二.拉取Mysql 5.7.31镜像 1dock ...

最新文章

  1. Sigma Function LightOJ - 1336[约数和定理]
  2. Java实现批量修改文件名,重命名
  3. Intersection of Two Linked Lists
  4. 【直播】如何获得更加高效的深度卷积神经网络
  5. 大厂测试开发常见面试题收集(python,java,性能等)
  6. webpack:多文件、多环境、跨域处理、热加载
  7. Qt笔记-Qt获取百度下拉推荐词
  8. 精通Android自定义View(九)绘制篇Canvas分析之绘制图片
  9. 离群点、异常点检测及Python实现(正态分布3∂,Z-score 异常值检测,基于MAD的Z-score 异常值检测,杠杆值点、DFFITS值、SR学生化残差、cook距离和covratio值)
  10. 为什么mvc里面的ModelState.IsValid一只都是true
  11. Javascript语言精粹之Array常用方法分析
  12. ros melodic控制真实机械臂之获取moveit规划插补点
  13. GeoTools——JTS空间操作
  14. 根据ip地址查找本地时区
  15. 深度可分离卷积结构(depthwise separable convolution)计算复杂度分析
  16. python解析xml选用什么模块_什么是适合Python的XML流解析器?
  17. WPS Office Pro v10.8.2.6726 绿色便携专业增强版
  18. 新同事上来就优化了一遍MySQL索引和查询,老板人傻了。。。
  19. 咕咕机vs喵喵机测评
  20. CVE-2010-2729(MS10-061)

热门文章

  1. vue与原生安卓相互调用
  2. 按键精灵--函数的介绍
  3. 《棒球英豪》:青春球场·棒球1号位
  4. HTML iframe 标签
  5. 【JavaWeb】Servlet系列——响应HTML代码、Servlet连接数据库、IDEA开发Servlet程序、Servlet对象的生命周期、GenericServelet适配器模式
  6. 第五章 数据链路层与局域网
  7. Android studio创建第一个项目并运行
  8. 判断一个字符串是否为全字母句
  9. Golang 多版本管理神器 gvm
  10. iOS-键盘弹出的类型