索引优化 ,b-tree
假设某个表有一个联合索引(c1,c2,c3,c4) 以下 只能使用该联合索引的c1,c2,c3部分
A. where c1 = x and c2 = x and c4>x and c3 = x
B. where c1 = x and c2 = x and c4=x order by c3
C. where c1 = x and c4 = x group by c3,c2
D. where c1 = ? and c5 = ? order by c2,c3
E. where c1 = ? and c2 = ? and c5=? order by c2,c3 实验:
#utf8 一个字符3个字节 , 注意:order by(索引能发挥都是要 按顺序查找, desc就用不上)
create table t6(
c1 char(1) not null default '',
c2 char(1) not null default '',
c3 char(1) not null default '',
c4 char(1) not null default '',
c5 char(1) not null default '',
key(c1,c2,c3,c4)
)engine myisam charset=utf8 #插入数据
insert into t6 values ('a','b','c','d','e'),('A','b','c','d','e'),('a','B','c','d','e'); A-E都否使用索引? 为什么
--------------------------------------

A 能用到 c1,c2,c3,c4 , mysql优化器会把A语句优化(不影响语意) where c1 = x and c2 = x and c3 = x and c4>x

explain select * from t6 where c1='a' and c2='b' and c4>'a' and c3='c'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t6
type: range
possible_keys: c1
key: c1
key_len: 12
ref: NULL
rows: 2
Extra: Using index condition
1 row in set (0.00 sec) key_len: 12 #代表4个索引全部用上( c1,c2,c3,c4 ) 4个索引 * 3字节 ------------------------
------------------------

B 只能用到 c1,c2, c3排序
explain select * from t6 where c1='a' and c2='b' and c4='d' order by c3\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t6
type: ref
possible_keys: c1
key: c1
key_len: 6
ref: const,const
rows: 2
Extra: Using index condition; Using where
1 row in set (0.00 sec)
key_len: 12 #2个索引用上( c1,c2) 2个索引 * 3字节 ps:这里c3索引用在了排序上
可以通过下面来比较
explain select * from t6 where c1='a' and c2='b' and c4='d' order by c5\G
注意观察Extra : Using filesort ------------------------
------------------------

C 只能用到 c1
explain select * from t6 where c1 = 'a' and c4 = 'd' group by c3,c2\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t6
type: ref
possible_keys: c1
key: c1
key_len: 3
ref: const
rows: 2
Extra: Using index condition; Using where; Using temporary; Using filesort
1 row in set (0.00 sec) key_len: 12 #用上1个索引( c1) 1个索引 * 3字节
Extra: Using temporary->使用到临时表 详解 有group by语句一般要先按分组字段顺序排列,如果此字段没排序好,mysql内部会先用临时表排序
explain select * from t6 where c1 = 'a' and c4 = 'd' group by c2,c3\G
因为查找用到c1, 正好c2是顺序,c3 不会建立临时表 ----------------------
----------------------

D 只能用到 c1,  c2,c3排序
explain select * from t6 where c1 = 'a' and c5 = 'e' order by c2,c3\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t6
type: ref
possible_keys: c1
key: c1
key_len: 3
ref: const
rows: 2
Extra: Using index condition; Using where
1 row in set (0.00 sec)
key_len: 12 #用上1个索引( c1) 1个索引 * 3字节
Extra:发现没有用文件排序 , (c2,c3顺序)正好用上 ----------------------------
----------------------------

E 只能用到 c1,c2,c3
explain select * from t6 where c1 = 'a' and c2='b' and c5 = 'e' order by c2,c3\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t6
type: ref
possible_keys: c1
key: c1
key_len: 6
ref: const,const
rows: 2
Extra: Using index condition; Using where
1 row in set (0.00 sec)
key_len: 12 #用上2个索引( c1,c2) 2个索引 * 3字节
Extra:发现没有用文件排序 , (c2,c3顺序)正好用上 倒过来:
explain select * from t6 where c1 = 'a' and c2='b' and c5 = 'e' order by c3,c2\G
Extra:没用到文件排序 , 因为查找的时候 已经找到了c2 ,是一个常量 对比 explain select * from t6 where c1 = 'a' and c5 = 'e' order by c3,c2\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t6
type: ref
possible_keys: c1
key: c1
key_len: 3
ref: const
rows: 2
Extra: Using index condition; Using where; Using filesort
1 row in set (0.00 sec)
Extra: 文件排序

mysql 多列索引学习-经典实例的更多相关文章

  1. mysql多列索引和最左前缀

    数据库的索引可以加快查询速度,原因是索引使用特定的数据结构(B-Tree)对特定的列额外组织存放,加快存储引擎(索引是存储引擎实现)查找记录的速度.索引优化是数据库优化的最重要手段. 如果查询语句使用 ...

  2. Mysql多列索引经典案例

    一个经典的多列索引案例,如题: 假设某个表有一个联合索引(c1,c2,c3,c4)一下--只能使用该联合索引的 c1,c2,c3 部分 Awhere c1=x and c2=x and c4>x ...

  3. Mysql的列索引和多列索引(联合索引)

    转自:http://blog.chinaunix.net/uid-29305839-id-4257512.html 创建一个多列索引:CREATE TABLE test (      id       ...

  4. 正确理解Mysql的列索引和多列索引

    MySQL数据库提供两种类型的索引,如果没正确设置,索引的利用效率会大打折扣却完全不知问题出在这. CREATE TABLE test ( id         INT NOT NULL, last_ ...

  5. mysql 多列索引的生效规则

    mysql中 myisam,innodb默认使用的是 Btree索引,至于btree的数据结构是怎样的都不重要,只需要知道结果,既然是索引那这个数据结构最后是排好序:就像新华字典他的目录就是按照a,b ...

  6. mysql多列索引优化

    “把Where条件里面的列都建上索引”,这种说法其实是非常错误的! 这样一个查询,假设actor_id与film_id都单独建立索引 SELECT film_id , actor_id FROM sa ...

  7. MySQL 多列索引优化小记

    MySQL 5.6.30 问题背景 由于爬虫抓取的数据不断增多,这两天在不断对数据库以及查询语句进行优化,其中一个表结构如下: CREATE TABLE `newspaper_article` ( ` ...

  8. Mysql多列索引实践

    在网上看到: 定义:最左前缀原则指的的是在sql where 子句中一些条件或表达式中出现的列的顺序要保持和多索引的一致或以多列索引顺序出现,只要 出现非顺序出现.断层都无法利用到多列索引. 该博文有 ...

  9. mysql多列索引

    1,数据库每次查询只能使用一个索引 2,假设数据 表T (a,b,c) rowid 为物理位置rowid a b c(1) 1 1 1(2) 2 1 13(3) 2 2 14(4) 1 3 3(5)  ...

随机推荐

  1. tomcat9配置https

    首先去阿里云申请一个pfx证书,可以免费获得. 其次,在tomcat的server.xml增加如下connector <Connector port="443" protoc ...

  2. SVN忽略已提交的文件(ignore,移出版本控制)

    本文适用于已安装TortoiseSVN客户端的同学. 1.右键点击要忽略的文件夹或文件,鼠标移到“TortoiseSVN”,找到“Unversion and add to ignore list”,选 ...

  3. WPF xml配置文件里面的大于小于号转义

    转义字符:<![CDATA[]]>,将要转义的字符放到中括号中,比如要转小于号:<,则换为<![CDATA[<]]> <?xml version=" ...

  4. 解决TensorFlow程序无限制占用GPU

    今天遇到一个奇怪的现象,使用tensorflow-gpu的时候,出现内存超额~~如果我训练什么大型数据也就算了,关键我就写了一个y=W*x.......显示如下图所示: 程序如下: import te ...

  5. opencv人脸检测,旋转处理

    年会签到,拍自己的大头照,有的人可能会拍成横向的,需要旋转,用人脸检测并修正它(图片). 1. 无脑检测步骤为: 1. opencv 读取图片,灰度转换 2. 使用CascadeClassifier( ...

  6. An error occurred while starting the application.

    一..net core 发布后的站点启动报错如下 An error occurred while starting the application. .NET Core 4.6.26328.01 X6 ...

  7. Flask对数据库的操作-----

    首先得做好做基本的框架 # -*- encoding: utf-8 -*- from flask import Flask,render_template #导入第三方连接库sql点金术 from f ...

  8. HiveServer2的WEB UI界面

    1.hive-site.xml配置如下: <property>    <name>hive.server2.webui.host</name>    <val ...

  9. css3 实现动画

    CSS3,我们可以创建动画,它可以取代许多网页动画图像,例如下面这个小球动画 使用css3关键帧动画可以轻松实现 请看下面代码 <!DOCTYPE html> <html lang= ...

  10. Java读取excel数据保存入库

    Java开发读取excel表格数据入库保存: List<Map<String, Object>> list = null; String filePath = filePath ...