mysql 联合索引有什么好处? 联合索引的意义

零下一度
发布: 2017-05-08 14:58:37
原创
6942 人浏览过

mysql 里创建‘联合索引’的意义

问题?
因为什么需求,要创建‘联合索引’?最实际好处在于什么?如果是为了更快查询到数据,有单列索引不是Ok?为什么有‘联合索引’的存在?

简单的说有两个主要原因:

  • "一个顶三个"。建了一个(a,b,c)的复合索引,那么实际等于建了(a),(a,b),(a,b,c)三个索引,因为每多一个索引,都会增加写操作的开销和磁盘空间的开销。对于大量数据的表,这可是不小的开销!

  • 覆盖索引。同样的有复合索引(a,b,c),如果有如下的sql: select a,b,c from table where a=1 and b = 1。那么MySQL可以直接通过遍历索引取得数据,而无需回表,这减少了很多的随机io操作。减少io操作,特别的随机io其实是dba主要的优化策略。所以,在真正的实际应用中,覆盖索引是主要的提升性能的优化手段之一

  • 索引列越多,通过索引筛选出的数据越少。有1000W条数据的表,有如下sql:select from table where a = 1 and b =2 and c = 3,假设假设每个条件可以筛选出10%的数据,如果只有单值索引,那么通过该索引能筛选出1000W10%=100w 条数据,然后再回表从100w条数据中找到符合b=2 and c= 3的数据,然后再排序,再分页;如果是复合索引,通过索引筛选出1000w 10% 10% *10%=1w,然后再排序、分页,哪个更高效,一眼便知

如下的有a,b,c 三个key的table

create table test(
    a int,
    b int,
    c int,
);
登录后复制

如果我们
需要执行很多的类似于 select * from test where a=10, b>50, c>20
这类的组合查询 那么,我们可能需要创建 包含[a,b,c] 的联合索引,而单独的[a][b] [c]上的索引是不够的。(可以把一个索引想象成 sorted list).创建了 (a,b,c)的索引相当于 按照a,b,c 排序(排序规则是

  if(X.a>Y.a)
    return '>';
  else if(X.a<Y.a)
    return &#39;<&#39;;
   else if(X.b>Y.b)
    return &#39;>&#39;;
   else if (X.b<Y.b)
    return &#39;<&#39;;
   else if (X.c>Y.c)
    return  &#39;>&#39;
   else if (X.c<Y.c)
    return  &#39;<&#39;
   esle
    return &#39;==&#39;
)
登录后复制

和分别 按a 排序 分别按b排序 分别按照c排序是不一样的。

其中 a b c 的顺序也很重要,有时可以是a c b,或者b c a等等。
如果创建 (a,b,c)的联合索引,查询效率如下:

  优: select * from test where a=10 and b>50
  差: select * from test where a>50

  优: select * from test order by a
  差: select * from test order by b
  差: select * from test order by c

  优: select * from test where a=10 order by a
  优: select * from test where a=10 order by b
  差: select * from test where a=10 order by c

  优: select * from test where a>10 order by a
  差: select * from test where a>10 order by b
  差: select * from test where a>10 order by c

  优: select * from test where a=10 and b=10 order by a
  优: select * from test where a=10 and b=10 order by b
  优: select * from test where a=10 and b=10 order by c

  优: select * from test where a=10 and b=10 order by a
  优: select * from test where a=10 and b>10 order by b
  差: select * from test where a=10 and b>10 order by c
登录后复制

下面用图示的方式来表示


三 注意

在mysql中 若列是varchar 类型,请不要使用 int类型去访问
如下
zz_deals表中 product_id 是varchar类型

mysql> explain select * from zz_deals where  qq_shop_id = 64230 and product_id = &#39;38605906667&#39; ;+----+-------------+-------------+------+------------------------------+------------------------------+---------+-------------+------+-------------+| id | select_type | table       | type | possible_keys                | key                          | key_len | ref         | rows | Extra       |+----+-------------+-------------+------+------------------------------+------------------------------+---------+-------------+------+-------------+|  1 | SIMPLE      | zz_deals | ref  | by_product_id_and_qq_shop_id | by_product_id_and_qq_shop_id | 156     | const,const |    1 | Using where |+----+-------------+-------------+------+------------------------------+------------------------------+---------+-------------+------+-------------+1 row in set (0.00 sec)
mysql> explain select * from zz_deals where  qq_shop_id = 64230 and product_id = 38605906667 ;+----+-------------+-------------+------+------------------------------+------+---------+------+------+-------------+| id | select_type | table       | type | possible_keys                | key  | key_len | ref  | rows | Extra       |+----+-------------+-------------+------+------------------------------+------+---------+------+------+-------------+|  1 | SIMPLE      | zz_deals | ALL  | by_product_id_and_qq_shop_id | NULL | NULL    | NULL |   17 | Using where |+----+-------------+-------------+------+------------------------------+------+---------+------+------+-------------+1 row in set (0.00 sec)
登录后复制

宣传语

历经两个半月的准备,三次大改版,十七次小改版。le1024终于要和大家见面了。

le1024每天推荐1~3段,有趣、有爱、有故事的视频。

为您工作、学习、生活之余增加一点快乐的感觉。

【相关推荐】

1. 免费mysql在线视频教程

2. MySQL最新手册教程

3. 数据库设计那些事

以上是mysql 联合索引有什么好处? 联合索引的意义的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!