mysql 多列索引学习-经典实例
索引优化 ,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 多列索引学习-经典实例的更多相关文章
- mysql多列索引和最左前缀
数据库的索引可以加快查询速度,原因是索引使用特定的数据结构(B-Tree)对特定的列额外组织存放,加快存储引擎(索引是存储引擎实现)查找记录的速度.索引优化是数据库优化的最重要手段. 如果查询语句使用 ...
- Mysql多列索引经典案例
一个经典的多列索引案例,如题: 假设某个表有一个联合索引(c1,c2,c3,c4)一下--只能使用该联合索引的 c1,c2,c3 部分 Awhere c1=x and c2=x and c4>x ...
- Mysql的列索引和多列索引(联合索引)
转自:http://blog.chinaunix.net/uid-29305839-id-4257512.html 创建一个多列索引:CREATE TABLE test ( id ...
- 正确理解Mysql的列索引和多列索引
MySQL数据库提供两种类型的索引,如果没正确设置,索引的利用效率会大打折扣却完全不知问题出在这. CREATE TABLE test ( id INT NOT NULL, last_ ...
- mysql 多列索引的生效规则
mysql中 myisam,innodb默认使用的是 Btree索引,至于btree的数据结构是怎样的都不重要,只需要知道结果,既然是索引那这个数据结构最后是排好序:就像新华字典他的目录就是按照a,b ...
- mysql多列索引优化
“把Where条件里面的列都建上索引”,这种说法其实是非常错误的! 这样一个查询,假设actor_id与film_id都单独建立索引 SELECT film_id , actor_id FROM sa ...
- MySQL 多列索引优化小记
MySQL 5.6.30 问题背景 由于爬虫抓取的数据不断增多,这两天在不断对数据库以及查询语句进行优化,其中一个表结构如下: CREATE TABLE `newspaper_article` ( ` ...
- Mysql多列索引实践
在网上看到: 定义:最左前缀原则指的的是在sql where 子句中一些条件或表达式中出现的列的顺序要保持和多索引的一致或以多列索引顺序出现,只要 出现非顺序出现.断层都无法利用到多列索引. 该博文有 ...
- 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) ...
随机推荐
- 使用Git Bash上传代码到新的分支
1.进入想要提交的项目,点击鼠标右键,选择"Git Bash Here" 2.输入命令,查看当前所有分支 git branch -a 3.输入命令,新建分支 git checkou ...
- int main(int argc,char* argv[]) 的含义和用法
1.基本概念 argc,argv 用命令行编译程序时有用. 主函数main中变量(int argc,char *argv[ ])的含义,有些编译器允许将main()的返回类型声明为void,这已不再是 ...
- mysql中的int和tinyint、varchar和char、DateTime和TimeStamp区别
一.int和tinyint的区别 大小: tinyint在mysql中占用1个字节 即: 1 bytes = 8 bit ,一个字节最多可以代表的数据长度是2的8次方:11111111 = 256 在 ...
- Convert Adjacency matrix into edgelist
Convert Adjacency matrix into edgelist import numpy as np #read matrix without head. a = np.loadtxt( ...
- chown nagios:nagios -R /var/lib/php/
chown nagios:nagios -R /var/lib/php/
- 关于全局变量,static,define和const
其实按照现在主流的观点,应该尽量少用全局变量和define,尽量多用临时变量,并且用const替换值define,用短小精悍的函数替换函数define. 对这些我倒是也没有什么意见,只 ...
- 关联tomcat源代码
1.进入tomcat官网下载对应版本源代码文件 2. 3..ctrl+鼠标左键 点击Cookie对象 4. 5.
- 嵌入式linux——点亮led灯(二)
刚才在jz2440板子上写了一个点亮中间led的程序,前前后后十几分钟才好.最终代码 本节内容: 1. 汇编点灯 2. C点灯 3. 参数选择点灯 4. 按键点灯 1. 汇编点灯 .text .glo ...
- 比较C#中几种常见的复制字节数组方法的效率
在日常编程过程中,我们可能经常需要Copy各种数组,一般来说有以下几种常见的方法:Array.Copy,IList<T>.Copy,BinaryReader.ReadBytes,Buffe ...
- 啊哈算法第四章第三节 层层递进-广度优先搜索 java实现
package corejava; public class FourThree { static int [][]a=new int[50][50]; static int [][]b=new in ...