Mysql多列索引经典案例
一个经典的多列索引案例,如题:
假设某个表有一个联合索引(c1,c2,c3,c4)一下——只能使用该联合索引的 c1,c2,c3 部分
Awhere 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
创建一张测试表
create table test (
c1 tinyint(1) not null default 0,
c2 tinyint(1) not null default 0,
c3 tinyint(1) not null default 0,
c4 tinyint(1) not null default 0,
c5 tinyint(1) not null default 0,
index c1234(c1,c2,c3,c4)
);
//插入两条数据
insert into test values (1,3,5,6,7),(2,3,9,8,3),(4,3,2,7,5);
insert into test values (2,4,5,8,7),(1,3,5,8,4),(3,4,2,9,6);
分析A => c1=x and c2=x and c4>x and c3=x <等价> c1=x and c2=x and c3=x and c4>x
c1,c2,c3,c4 都能用上
分析B => select * from test where c1=1 and c2=2 and c4=3 order by c3
c1 ,c2 索引用上了,在 c2 用到索引的基础上,c3 是排好序的,因此不用额外排序,而 c4 没发挥作用
分析C => select * from test where c1=1 and c4=2 group by c3,c2
只用到 c1 索引,因为 group by c3,c2 的顺序无法利用 c2,c3 索引
分析D => select * from test where c1=1 and c5=2 order by c2,c3
C1 确定的基础上,c2 是有序的,C2 之下 C3 是有序的,因此 c2,c3 发挥的排序的作用. 因此,没用到 filesort
分析E => select * from test where c1=1 and c2=3 and c5=2 order by c3;
因为 c2 的值既是固定的,参与排序时并不考虑
Mysql多列索引经典案例的更多相关文章
- mysql多列索引和最左前缀
数据库的索引可以加快查询速度,原因是索引使用特定的数据结构(B-Tree)对特定的列额外组织存放,加快存储引擎(索引是存储引擎实现)查找记录的速度.索引优化是数据库优化的最重要手段. 如果查询语句使用 ...
- 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 多列索引学习-经典实例
索引优化 ,b-tree假设某个表有一个联合索引(c1,c2,c3,c4) 以下 只能使用该联合索引的c1,c2,c3部分A. where c1 = x and c2 = x and c4>x ...
- 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) ...
随机推荐
- 原型模式 private static Map<String,Prototype> map = new HashMap<String,Prototype>();
public class PrototypeManager { /** * 用来记录原型的编号和原型实例的对应关系 */ private static Map<String,Prototype& ...
- 扩容数据盘_Linux
扩容数据盘_Linux_扩容云盘_云盘_用户指南_云服务器 ECS-阿里云 https://help.aliyun.com/document_detail/25452.html 磁盘扩容付费后: 在控 ...
- The connection between feature spaces and smoothness is not obvious, and is one of the things we’ll discuss in the course.
http://www.gatsby.ucl.ac.uk/~gretton/coursefiles/lecture4_introToRKHS.pdf
- mysql系列之9.mysql日志&存储引擎
mysqlbinlog 是什么? 数据目录下的如下文件: mysql-bin.xxxxxx 作用? 记录数据库内部增删改查对mysql数据库有更新的内容的记录 三种模式? statement leve ...
- mysql系列之8.mysql高可用 (mha4mysql)
环境: 三台机器 主服务器: 192.168.1.130 主备机器: 192.168.1.131 监控机器: 192.168.1.132 130和131, 是mysql双主架构 1.在三台机器上安装m ...
- selector + drawable 多状态图形
select_drawble.xml<?xml version="1.0" encoding="utf-8"?> <selector xmln ...
- 使用阿里云maven镜像加速jar包下载
编辑 MAVEN_HOME/conf 文件夹下的 settings.xml,找到 <mirrors> 节点,把下面内容添加在其子节点内: <mirror> <id> ...
- c# XML-Object对象 序列化-反序列化
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- 时尚设计div+css免费模板
时尚设计div+css免费网页模板,时尚设计,div+css. http://www.huiyi8.com/moban/
- 谷歌新操作系统fuchsia
开源地址: https://github.com/fuchsia-mirror