mysql 多列索引学习-经典实例

本文通过实例演示了如何利用MySQL的联合索引进行查询优化,详细解析了不同查询条件下索引的使用情况,并针对每种情况提供了explain执行计划分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

索引优化 ,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: 文件排序

转载于:https://www.cnblogs.com/loveyouyou616/p/6369744.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值