【建表语句】

create table test03(id int primary key not null auto_increment,c1 char(10),c2 char(10),c3 char(10),c4 char(10),c5 char(10)
);insert into test03(c1,c2,c3,c4,c5) values('a1','a2','a3','a4','a5');
insert into test03(c1,c2,c3,c4,c5) values('b1','b2','b3','b4','b5');
insert into test03(c1,c2,c3,c4,c5) values('c1','c2','c3','c4','c5');
insert into test03(c1,c2,c3,c4,c5) values('d1','d2','d3','d4','d5');
insert into test03(c1,c2,c3,c4,c5) values('e1','e2','e3','e4','e5');select * from test03;

【建索引】

create index idx_test03_c1234 on test03(c1,c2,c3,c4);
show index from test03;

问题:我们创建了复合索引idx_test03_c1234 ,根据以下SQL分析下索引使用情况?

1 explain select * from test03 where c1='a1';
2 explain select * from test03 where c1='a1' and c2='a2';
3 explain select * from test03 where c1='a1' and c2='a2' and c3='a3';
4 explain select * from test03 where c1='a1' and c2='a2' and c3='a3' and c4='a4';

1)
explain select * from test03 where c1='a1' and c2='a2' and c3='a3' and c4='a4';

2)
explain select * from test03 where c1='a1' and c2='a2' and c4='a4' and c3='a3';

explain select * from test03 where c4='a4' and c3='a3' and c2='a2' and c1='a1';

3) 
explain select * from test03 where c1='a1' and c2='a2' and c3>'a3' and c4='a4';

4)
explain select * from test03 where c1='a1' and c2='a2' and c4>'a4' and c3='a3';

说明:4个索引全部使用,虽然c3在最后,但是mysql可以自动调优。

5)
explain select * from test03 where c1='a1' and c2='a2' and c4='a4' order by c3;

c3作用在排序而不是查找

【索引的两大功能:查找和排序】
6)
explain select * from test03 where c1='a1' and c2='a2' order by c3;

7)
explain select * from test03 where c1='a1' and c2='a2' order by c4;
出现了filesort

8)
8.1
explain select * from test03 where c1='a1' and c5='a5' order by c2,c3;

只用c1一个字段索引,但是c2、c3用于排序,无filesort
8.2
explain select * from test03 where c1='a1' and c5='a5' order by c3,c2;

出现了filesort,我们建的索引是1234,它没有按照顺序来,3 2 颠倒了

9)
explain select * from test03 where c1='a1' and c2='a2' order by c2,c3;

10)
explain select * from test03 where c1='a1' and c2='a2' and c5='a5' order by c2,c3;

用c1、c2两个字段索引,但是c2、c3用于排序,无filesort

explain select * from test03 where c1='a1' and c2='a2' and c5='a5' order by c3,c2;

本例有常量c2的情况,和8.2对比(c2='c2'已经有具体值,为常量时,无需排序)

explain select * from test03 where c1='a1' and c5='a5' order by c3,c2;

filesort出现

11)
explain select * from test03 where c1='a1' and c4='a4' group by c2,c3;

12)
explain select * from test03 where c1='a1' and c4='a4' group by c3,c2;

Using where; Using temporary; Using filesort

【group by表面理解为分组,但是要注意的是,分组之前必排序】

【结论】

【一般性建议】

1、对于单键索引,尽量选择针对当前query过滤性更好的索引

2、在选择组合索引的时候,当前Query中过滤性最好的字段在索引字段顺序中,位置越靠前(左)越好。(避免索引过滤性好的索引失效)

3、在选择组合索引的时候,尽量选择可以能够包含当前query中的where字句中更多字段的索引

4、尽可能通过分析统计信息和调整query的写法来达到选择合适索引的目的

转载于:https://www.cnblogs.com/116970u/p/10987767.html

MySQL索引面试题分析(索引分析,典型题目案例)相关推荐

  1. 干货!MySQL常见的面试题+索引原理分析!

    今天给大家分享一篇干货,面试必备之Mysql索引底层原理分析,文章末尾有福利哟!!!! Mysql索引的本质 Mysql索引的底层原理 Mysql索引的实战经验 面试 问:数据库中最常见的慢查询优化方 ...

  2. MySQL高级 之 索引面试题分析

    索引优化简单案例 单表 需求:查询category_id为1 且 comments大于1 的情况下,views最多的id 1.无索引的情况下: 很显然,type是ALL,即最坏的情况,Extra还出现 ...

  3. 干货+福利!MySQL常见的面试题+索引原理分析!

    文章开头先分享一波福利给大家!!! 扫码关注免费赠书一百本<Spring Cloud微服务实战>书籍 活动真实有效,仅限DD粉丝 MySQL索引的本质 MySQL索引的底层原理 MySQL ...

  4. 干货—MySQL常见的面试题+索引原理分析!

    目录 MySQL索引的本质 MySQL索引的底层原理 MySQL索引的实战经验 面试 问:数据库中最常见的慢查询优化方式是什么? 同学A:加索引. 问:为什么加索引能优化慢查询? 同学A:...不知道 ...

  5. Mysql 索引优化分析_如何优化MySQL的性能?从索引方面优化案例分析

    今天我们来讲讲如何优化MySQL的性能,主要从索引方面优化. 建表 //建表 CREATETABLEIFNOTEXISTSstaffs( idINTPRIMARYKEYAUTO_INCREMENT, ...

  6. JAVA中Explain注解用法,mysql之explain详解(分析索引最佳使用)

    mysql之explain详解(分析索引最佳使用) mysql explain用于分析sql 语句的执行及数据库索引的使用.本文将致力于帮助大家充分理解explain所返回的各项参数,从而使大家快速掌 ...

  7. mysql索引结构原理、性能分析与优化

    摘要: 第一部分:基础知识 第二部分:MYISAM和INNODB索引结构 1.简单介绍B-tree B+ tree树 2.MyisAM索引结构 3.Annode索引结构 4.MyisAM索引与Inno ...

  8. mysql之explain详解(分析索引的最佳使用)

    在这里对explain的各个字段进行详细的分析,来帮助大家分析自己所写的sql是否最佳的使用了索引. 首先是select_type:将select查询分为简单(simple)和复杂两种类型 复杂类型又 ...

  9. MySQL like查询后置%索引失效分析

    MySQL like查询后置%索引失效分析 表结构 CREATE TABLE `t_food_shop` (`id` bigint(20) unsigned NOT NULL AUTO_INCREME ...

最新文章

  1. ProtoBuf格式详解
  2. 使用代码配置 NHibernate
  3. python使用符号 表示单行注释-Pyhton中单行和多行注释的使用方法及规范
  4. SVN Error:请求的名称有效并且在数据库中找到,但是它没有相关的正确的数据来被解析...
  5. oracle多条sql语句常量,如何在Oracle中一次执行多条sql语句
  6. android listview ontouchlistener,Android ListView监听滑动事件的方法(详解)
  7. POJ 1151 线段树+扫描线
  8. 使用XmlPullParser解析XML
  9. php长链接要配置,PHP链接到配置文件页面。 ?id =
  10. 如何破解无线网络密码(无线网络密码破解)
  11. 《开源安全运维平台--OSSIM最佳实践》节日期间当当自营店 五折 优惠活动开始啦!...
  12. 富贵电玩 富贵旺旺 富贵精华版 富贵3 后门 格机问题研究
  13. 未来20年内,无人驾驶将颠覆这33大行业
  14. win10 自动同步时间脚本
  15. opencv中图像失焦检测
  16. android 7双排设置菜单,联想拯救者电竞手机优化横屏UI 设置菜单呈左右双排显示...
  17. 学习笔记:CentOS7学习之十六:LVM管理和ssm存储管理器使用
  18. Flyway详解以及Springboot集成Flyway
  19. Kubernetes_介绍
  20. 28335的启动步骤介绍

热门文章

  1. 世界读书日 阿里人是这样看书的?
  2. 2017.11.7 Python 制作EFM32/ AVR批量烧录工具
  3. 美国政府签署网络安全行政令 将全面加强网络安全建设
  4. SQL注入攻击的种类和防范手段
  5. Android通过for循环批量发送短信
  6. 常见宽带错误解决方法
  7. 面试官:BigDecimal 一定不会丢失精度吗?
  8. 全文搜索引擎选 ElasticSearch 还是 Solr?
  9. 阿里P7试用期被淘汰,主管给出的理由让人意想不到
  10. Python 使用正则表达式中的 /b 的时候出现了问题