MySQL 联合索引使用情况

本文详细解析MySQL中联合索引的使用情况,通过具体实例展示了不同查询条件如何利用联合索引进行快速检索和排序,深入理解索引在实际应用中的效率提升。

验证联合索引使用的情况

索引是一个排序的结构,用于快速检索和加速排序

MySQL表结构

index_test | CREATE TABLE `index_test` (
`c1` char(10) NOT NULL,
`c2` char(10) NOT NULL,
`c3` char(10) NOT NULL,
`c4` char(10) NOT NULL,
`c5` char(10) NOT NULL,
KEY `index_c1c2c3c4` (`c1`,`c2`,`c3`,`c4`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

(1).

mysql> explain select * from index_test where c1='c1' and c2='c2' and c4>'c4' and c3='c3'\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: index_test
type: range
possible_keys: index_c1c2c3c4
key: index_c1c2c3c4
key_len: 120
ref: NULL
rows: 1
Extra: Using index condition

由key_len: 120可知,四个索引都用到了(utf8,一个字符占用3个字节);

(2).

explain select * from index_test where c1='c1' and c2='c2' and c4='c4' order by c3\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: index_test
type: ref
possible_keys: index_c1c2c3c4
key: index_c1c2c3c4
key_len: 60
ref: const,const
rows: 1
Extra: Using index condition; Using where

c1,c2,c3都用到了,c1,c2索引用于排序,c3索引用于排序

(3)

select * from index_test where c1='c1' and c2='c2' order by c3,c2\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: index_test
type: ref
possible_keys: index_c1c2c3c4
key: index_c1c2c3c4
key_len: 60
ref: const,const
rows: 1
Extra: Using index condition; Using where

c1,c2,c3 c3用到索引是因为c2是常量

(4)

explain select * from index_test where c1='c1' and c5='c5' order by c2,c3\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: index_test
type: ref
possible_keys: index_c1c2c3c4
key: index_c1c2c3c4
key_len: 30
ref: const
rows: 1
Extra: Using index condition; Using where

c1,c2,c3用到索引了

(5)

explain select * from index_test where c1='c1' and c2='c2' and c5='c5' order by c3,c2\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: index_test
type: ref
possible_keys: index_c1c2c3c4
key: index_c1c2c3c4
key_len: 60
ref: const,const
rows: 1
Extra: Using index condition; Using where

c1,c2,c3使用了索引

转载于:https://www.cnblogs.com/sun5/p/11523519.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值