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) ...
随机推荐
- gmer ver2.1.19357
gmer是一款来自 波兰 的多功能安全监控分析程序.它可以结束除了System和System Idle Process以外几乎所有的进程,还可以查看被隐藏的进程,服务以及驱动(以红色的方式显示此进程. ...
- C#实现json压缩和格式化
json作为常用数据文件,为了传输的效率,在传输前要进行压缩,而在传输后要进行格式化,以便阅读.下面是使用C#完成的格式化和压缩代码. public static string Compress(st ...
- POJ1083 Moving Tables
POJ1083 Moving Tables Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35297 Accepted: ...
- WPF DEV gridcontrol 自定义计算列(TotalSummary)
/// <summary> /// 自定义计算列 /// </summary> /// <param name="sender"></pa ...
- 观察者模式的python实现
什么会观察者模式?观察者模式就是订阅-推送模式.是为了解耦合才会被利用起来的设计模式. 经典的就是boss 前台和员工之间的故事.一天A员工在看电影,B员工在看动漫,但是两人担心boss来了,自己没及 ...
- 强大的oracle分析函数
转载:https://www.cnblogs.com/benio/archive/2011/06/01/2066106.html 学习步骤:1. 拥有Oracle EBS demo 环境 或者 PRO ...
- expdp/impdp数据泵分区表导入太慢了。添加不检查元数据参数提高效率:ACCESS_METHOD=DIRECT_PATH
分区表数据泵导入太慢,达不到客户的迁移要求导出语句如下:(10G单节点)userid='/ as sysdba'directory=milk_dirdumpfile=mon_%U.dmplogfile ...
- leetcode每日刷题计划-简单篇day5
刷题成习惯以后感觉挺好的 Num 27 移除元素 Remove Element 跟那个排序去掉相同的一样,len标记然后新的不重复的直接放到len class Solution { public: i ...
- js点击图片放大
废话不说直接放代码了: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- mysql安装,以及从csv插入数据
1.mysql安装 用管理员身份打开cmd命令行工具,cd到解压文件的bin目录: ・MySql的Windows服务安装: mysqld install 回车 ・生成无密码的root用户: my ...