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

索引优化 ,b-tree假设某个表有一个联合索引(c1,c2,c3,c4) 以下 只能使用该联合索引的c1,c2,c3部分A. where c1 = x and c2 = x and c4>x and c3 = xB. where c1 = x and c2 = x and c4=x order by c3C. where c1 = x and c4 = x group by c3,c2D. where c1 = ? and c5 = ? order by c2,c3E. 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: 文件排序
时间: 2024-08-27 21:59:02

mysql 多列索引学习-经典实例的相关文章

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

mysql 多列索引优化

Mysql所有的列都可以使用索引,.对相关列使用索引是提高SELECT操作性能的最佳途径.根据存储引擎定义每个表的最大索引数和最大索引长度.所有存储引擎支持每个表至少16个索引,总索引长度至少256字节.在索引中使用col_name(length)语法,可以创建一个只使用char和archar列的第一个length个字符的索引,按这种方式只索引列的前缀可以索引文件小的多.MyISAm和INNODb存储引擎还支持对blob和text列的索引,但是必须指定索引长度.fulltext索引用于全文搜索不

Mysql的列索引和多列索引(联合索引)

转自:http://blog.chinaunix.net/uid-29305839-id-4257512.html 创建一个多列索引:CREATE TABLE test (      id         INT NOT NULL,      last_name  CHAR(30) NOT NULL,      first_name CHAR(30) NOT NULL,      PRIMARY KEY (id),      INDEX name (last_name,first_name)  

正确理解Mysql的列索引和多列索引

MySQL数据库提供两种类型的索引,如果没正确设置,索引的利用效率会大打折扣却完全不知问题出在这. [c-sharp] view plain copy CREATE TABLE test ( id         INT NOT NULL, last_name  CHAR(30) NOT NULL, first_name CHAR(30) NOT NULL, PRIMARY KEY (id), INDEX name (last_name,first_name) ); 以上创建的其实是一个多列索引

mysql 多列索引的生效规则

mysql中 myisam,innodb默认使用的是 Btree索引,至于btree的数据结构是怎样的都不重要,只需要知道结果,既然是索引那这个数据结构最后是排好序:就像新华字典他的目录就是按照a,b,c..这样排好序的:所以你在找东西的时候才快,比如你找 "中" 这个字的解释,你肯定就会定位到目录的 z 开头部分: 组合索引可以这样理解,比如(a,b,c),abc都是排好序的,在任意一段a的下面b都是排好序的,任何一段b下面c都是排好序的: 组合索引的生效原则是  从前往后依次使用生

Mysql的列索引和多列索引

Mysql数据库提供两种类型的索引,如果没正确设置,索引的利用效率会大打折扣. CREATE TABLE test ( id INT NOT NULL, last_name CHAR(30) NOT NULL, first_name CHAR(30) NOT NULL, PRIMARY KEY (id), INDEX name (last_name,first_name) ); 以上创建的其实是一个多列索引,创建列索引的代码如下: CREATE TABLE test ( id INT NOT N

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) 2 3 12(6) 1 2 5(7) 2 3 9(8) 1 2 2(9) 1 3 6(10) 2 2 11(11) 2 2 8(12) 1 1 7(13) 2 3 15(14) 1 1 4(15) 2 1 10 当你创建一个索引 create index xxx on t(a,b), 则索引文件逻辑

【从零学习经典算法系列】分治策略实例——快速排序(QuickSort)

在前面的博文(http://blog.csdn.net/jasonding1354/article/details/37736555)中介绍了作为分治策略的经典实例,即归并排序,并给出了递归形式和循环形式的c代码实例.但是归并排序有两个特点,一是在归并(即分治策略中的合并步骤)上花费的功夫较多,二是排序过程中需要使用额外的存储空间(异地排序算法<out of place sort>). 为了节省存储空间,出现了快速排序算法(原地排序in-place sort).快速排序是由东尼·霍尔所发展的一

【从零学习经典算法系列】分治策略实例——二分查找

1.二分查找算法简介 二分查找算法是一种在有序数组中查找某一特定元素的搜索算法.搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束:如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较.如果在某一步骤数组 为空,则代表找不到.这种搜索算法每一次比较都使搜索范围缩小一半.折半搜索每次把搜索区域减少一半,时间复杂度为Ο(logn). 二分查找的优点是比较次数少,查找速度快,平均性能好:其缺点是要求待查表为有序表,且