mysql之多列索引
2015-09-05 14:18
701 查看
mysql的多列索引是经常会遇到的问题,怎样才能有效命中索引,是本文要探讨的重点。
多列索引使用的Btree,也就是平衡二叉树。简单来说就是排好序的快速索引方式。它的原则就是要遵循左前缀索引。
多个索引从左边往右都使用上,才能使用到整个多列索引。
下面我先建立一个简单的表做实验:
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,c5)
) engine myisam charset utf8;
再分别insert into一些数据。
分别做以下一些查询:
1.
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 where
1 row in set (0.00 sec)
这里使用到了c1,c2,c3,c4索引,特别c4是一个范围查询,所以type为range,依次c1,c2,c3被命中索引。
2.
explain select * from t6 where c1='a' and c4='a' 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 where; Using filesort
1 row in set (0.00 sec)
过程中先命中了c1,然后在order by c3,c2的时候由于先去use c3做排序,从而联合索引断了。using filesort表明没有使用到多列合索引,而是做了文件内排序。
3.
explain select * from t6 where c1='a' and c4='a' 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 where
1 row in set (0.00 sec)
反之命中c1索引后,先用c2排序,再是c3来排序,分别命中,最后是c4='a'做where查询,使用到了多列索引。
多列索引使用的Btree,也就是平衡二叉树。简单来说就是排好序的快速索引方式。它的原则就是要遵循左前缀索引。
多个索引从左边往右都使用上,才能使用到整个多列索引。
下面我先建立一个简单的表做实验:
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,c5)
) engine myisam charset utf8;
再分别insert into一些数据。
分别做以下一些查询:
1.
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 where
1 row in set (0.00 sec)
这里使用到了c1,c2,c3,c4索引,特别c4是一个范围查询,所以type为range,依次c1,c2,c3被命中索引。
2.
explain select * from t6 where c1='a' and c4='a' 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 where; Using filesort
1 row in set (0.00 sec)
过程中先命中了c1,然后在order by c3,c2的时候由于先去use c3做排序,从而联合索引断了。using filesort表明没有使用到多列合索引,而是做了文件内排序。
3.
explain select * from t6 where c1='a' and c4='a' 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 where
1 row in set (0.00 sec)
反之命中c1索引后,先用c2排序,再是c3来排序,分别命中,最后是c4='a'做where查询,使用到了多列索引。
相关文章推荐
- mysql安装后配置
- mysql + unidac 使用事务例子
- MySQL 如何修改字符集 utf8 改为 utf8mb4
- Mysql之DDL语句_create_alter
- sql server 与 mysql在自定以数据类型的区别
- 快速安装mysql tcl expect
- MySQL常用操作
- 修改MySQL的默认密码的四种小方法
- 对pymysql的简单封装
- MySQL 5.7 并行复制实现原理与调优
- Navicat访问MySQL出现1044/1045错误的解决方法
- MySQL索引原理及慢查询优化
- MySQL数据库之布尔类型、枚举类型和集合类型的应用场景详解
- mysql允许远程访问 root
- 2003 Can't connect to MySQL server on 'localhost' (10061)
- Mysql字符转义
- MySQL5.6安装步骤(windows7/8_64位)
- mysql的算术运算符和比较运算符
- MySQL学习笔记(二)--存储类型
- mac MySQL5.6中文乱码