Mysql多列索引
2015-10-04 20:02
706 查看
表结构如下:
mysql> show create table t4\G;
*************************** 1. row ***************************
Table: t4
Create Table: CREATE TABLE `t4` (
`c1` tinyint(1) NOT NULL DEFAULT '0',
`c2` tinyint(2) NOT NULL DEFAULT '0',
`c3` tinyint(3) NOT NULL DEFAULT '0',
`c4` tinyint(4) NOT NULL DEFAULT '0',
`c5` tinyint(5) NOT NULL DEFAULT '0',
KEY `c1234` (`c1`,`c2`,`c3`,`c4`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
表数据如下:
mysql> select * from t4;
+----+----+----+----+----+
| c1 | c2 | c3 | c4 | c5 |
+----+----+----+----+----+
| 1 | 3 | 5 | 6 | 7 |
| 2 | 3 | 9 | 8 | 3 |
| 4 | 3 | 2 | 7 | 5 |
+----+----+----+----+----+
3 rows in set (0.00 sec)
下面几个语句,哪几个索引起作用了?为什么?
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=x and c5=x order by c2,c3
E where c1=x and c2=x and c5=? order by c2,c3
A语句如下,我们会发现,将四列索引用上了,key_len(索引长度)为4个字节,一个数字为1个字节,可见总共用到了4个索引
mysql> explain select * from t4 where c1=1 and c2=2 and c4>3 and c3=3 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: range
possible_keys: c1234
key: c1234
key_len: 4
ref: NULL
rows: 1
Extra: Using index condition
1 row in set (0.00 sec)
B语句如下,我们会发现,c1 ,c2索引用上了,在c2用到索引的基础上,c3是排好序的,因此不用额外排序,而c4没有发挥作用
mysql> explain select * from t4 where c1=1 and c2=2 and c4=3 order by c3 \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 2
ref: const,const
rows: 1
Extra: Using index condition; Using where
1 row in set (0.00 sec)
下面的语句与上面的语句对比发现,下面的语句用到了filesort,这是因为索引c3已经帮我们排好序了,但是c5没有加索引,所以在order的时候,会使用filesort来进行排序
mysql> explain select * from t4 where c1=1 and c2=2 and c4=3 order by c5\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 2
ref: const,const
rows: 1
Extra: Using index condition; Using where; Using filesort
1 row in set (0.00 sec)
C语句如下,只用到c1索引,因为group by c3,c2的顺序无法利用c2,c3索引(索引的左前缀规则),因为没有利用到索引,所以用到了临时表(temporary),在mysql的group查询中,会先将查询的数据进行排序,存到临时表中,然后再进行查询。
mysql> explain select * from t4 where c1=1 and c4=2 group by c3,c2\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 1 #只用到c1,因为先用c3后用c2分组,导致c2,c3索引没发挥作用
ref: const
rows: 1
Extra: Using index condition; Using where; Using temporary; Using filesort
1 row in set (0.00 sec)
下面的语句与上面的相比,也是只用到了索引c1,c2和c3都用来group分组了
explain select * from t4 where c1=1 and c4=2 group by c2,c3 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 1
ref: const
rows: 1
Extra: Using index condition; Using where
1 row in set (0.00 sec)
D语句如下,C1确定的基础上,c2是有序的,C2之下C3是有序的,因此c2,c3发挥的排序的作用,因此,没用到filesort
mysql> explain select * from t4 where c1=1 and c5=2 order by c2,c3\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 1
ref: const
rows: 1
Extra: Using where
1 row in set (0.00 sec)
下面的语句与上面相比,先用c3排序,再用c2排序,无法利用索引,索引遵循左前缀法则,所以会用到filesort
mysql> explain select * from t4 where c1=1 and c5=2 order by c3,c2\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 1
ref: const
rows: 1
Extra: Using where; Using filesort
1 row in set (0.00 sec)
E语句如下,这一句等价与 elect * from t4 where c1=1 and c2=3 and c5=2 order by c3; 因为c2的值既是固定的,参与排序时并不考虑
mysql> explain select * from t4 where c1=1 and c2=3 and c5=2 order by c2,c3\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 2
ref: const,const
rows: 1
Extra: Using where
1 row in set (0.00 sec)
mysql> show create table t4\G;
*************************** 1. row ***************************
Table: t4
Create Table: CREATE TABLE `t4` (
`c1` tinyint(1) NOT NULL DEFAULT '0',
`c2` tinyint(2) NOT NULL DEFAULT '0',
`c3` tinyint(3) NOT NULL DEFAULT '0',
`c4` tinyint(4) NOT NULL DEFAULT '0',
`c5` tinyint(5) NOT NULL DEFAULT '0',
KEY `c1234` (`c1`,`c2`,`c3`,`c4`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
表数据如下:
mysql> select * from t4;
+----+----+----+----+----+
| c1 | c2 | c3 | c4 | c5 |
+----+----+----+----+----+
| 1 | 3 | 5 | 6 | 7 |
| 2 | 3 | 9 | 8 | 3 |
| 4 | 3 | 2 | 7 | 5 |
+----+----+----+----+----+
3 rows in set (0.00 sec)
下面几个语句,哪几个索引起作用了?为什么?
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=x and c5=x order by c2,c3
E where c1=x and c2=x and c5=? order by c2,c3
A语句如下,我们会发现,将四列索引用上了,key_len(索引长度)为4个字节,一个数字为1个字节,可见总共用到了4个索引
mysql> explain select * from t4 where c1=1 and c2=2 and c4>3 and c3=3 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: range
possible_keys: c1234
key: c1234
key_len: 4
ref: NULL
rows: 1
Extra: Using index condition
1 row in set (0.00 sec)
B语句如下,我们会发现,c1 ,c2索引用上了,在c2用到索引的基础上,c3是排好序的,因此不用额外排序,而c4没有发挥作用
mysql> explain select * from t4 where c1=1 and c2=2 and c4=3 order by c3 \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 2
ref: const,const
rows: 1
Extra: Using index condition; Using where
1 row in set (0.00 sec)
下面的语句与上面的语句对比发现,下面的语句用到了filesort,这是因为索引c3已经帮我们排好序了,但是c5没有加索引,所以在order的时候,会使用filesort来进行排序
mysql> explain select * from t4 where c1=1 and c2=2 and c4=3 order by c5\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 2
ref: const,const
rows: 1
Extra: Using index condition; Using where; Using filesort
1 row in set (0.00 sec)
C语句如下,只用到c1索引,因为group by c3,c2的顺序无法利用c2,c3索引(索引的左前缀规则),因为没有利用到索引,所以用到了临时表(temporary),在mysql的group查询中,会先将查询的数据进行排序,存到临时表中,然后再进行查询。
mysql> explain select * from t4 where c1=1 and c4=2 group by c3,c2\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 1 #只用到c1,因为先用c3后用c2分组,导致c2,c3索引没发挥作用
ref: const
rows: 1
Extra: Using index condition; Using where; Using temporary; Using filesort
1 row in set (0.00 sec)
下面的语句与上面的相比,也是只用到了索引c1,c2和c3都用来group分组了
explain select * from t4 where c1=1 and c4=2 group by c2,c3 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 1
ref: const
rows: 1
Extra: Using index condition; Using where
1 row in set (0.00 sec)
D语句如下,C1确定的基础上,c2是有序的,C2之下C3是有序的,因此c2,c3发挥的排序的作用,因此,没用到filesort
mysql> explain select * from t4 where c1=1 and c5=2 order by c2,c3\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 1
ref: const
rows: 1
Extra: Using where
1 row in set (0.00 sec)
下面的语句与上面相比,先用c3排序,再用c2排序,无法利用索引,索引遵循左前缀法则,所以会用到filesort
mysql> explain select * from t4 where c1=1 and c5=2 order by c3,c2\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 1
ref: const
rows: 1
Extra: Using where; Using filesort
1 row in set (0.00 sec)
E语句如下,这一句等价与 elect * from t4 where c1=1 and c2=3 and c5=2 order by c3; 因为c2的值既是固定的,参与排序时并不考虑
mysql> explain select * from t4 where c1=1 and c2=3 and c5=2 order by c2,c3\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t4
type: ref
possible_keys: c1234
key: c1234
key_len: 2
ref: const,const
rows: 1
Extra: Using where
1 row in set (0.00 sec)
相关文章推荐
- mysql 学习记录(七)--表优化、合成索引、浮点数与定点数
- mysql 配置 utf8 依然乱码
- mysql主备库数据不一致的原因和解决方案(转)
- MySQL 最经常使用的一千行
- Mysql事务处理
- MySQL学习笔记(六)--选择合适的数据类型
- win7已安装Mysql 开机自启动
- 索引总结-mysql
- MySQL数据库主从同步
- java代码实现JDBC连接MySql以及引用驱动程序包
- MySQL
- MySQL的日志原理
- mysql 学习记录(六)-- 查看引擎、myisam引擎、自增长、主外键关联、memory引擎、merge引擎
- mysql-5.5.46源码编译安装
- mysql优化
- mysqlcluster集群搭建
- MySQL主从安装文档
- 详解MySQL Cluster管理结点的config.ini配置文件
- mysql引擎
- mysql高速缓冲