Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
mysql索引合并:一条sql可以使用多个索引_创想鸟

mysql索引合并:一条sql可以使用多个索引

         转载请注明来源:mysql索引合并:一条sql可以使用多个索引

前言

mysql的索引合并并不是什么新特性。早在mysql5.0版本就已经实现。之所以还写这篇博文,是因为好多人还一直保留着一条sql语句只能使用一个索引的错误观念。本文会通过一些示例来说明如何使用索引合并。

什么是索引合并

下面我们看下mysql文档中对索引合并的说明:

The Index Merge method is used to retrieve rows with several range scans and to merge their results into one. The merge can produce unions, intersections, or unions-of-intersections of its underlying scans. This access method merges index scans from a single table; it does not merge scans across multiple tables.

1、索引合并是把几个索引的范围扫描合并成一个索引。
2、索引合并的时候,会对索引进行并集,交集或者先交集再并集操作,以便合并成一个索引。
3、这些需要合并的索引只能是一个表的。不能对多表进行索引合并。

使用索引合并有啥收益

简单的说,索引合并,让一条sql可以使用多个索引。对这些索引取交集,并集,或者先取交集再取并集。从而减少从数据表中取数据的次数,提高查询效率。

怎么确定使用了索引合并

在使用explain对sql语句进行操作时,如果使用了索引合并,那么在输出内容的type列会显示 index_merge,key列会显示出所有使用的索引。如下:
index_merge_sql

在explain的extra字段中会以下几种:
Using union 索引取并集
Using sort_union 先对取出的数据按rowid排序,然后再取并集
Using intersect 索引取交集

你会发现并没有 sort_intersect,因为根据目前的实现,想索引取交集,必须保证通过索引取出的数据顺序和rowid顺序是一致的。所以,也就没必要sort了。

sort_union索引合并的示例

数据表结构

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@@######@@

现代化家居响应式网站模板1.0 现代化家居响应式网站模板1.0

现代化家居响应式网站模板源码是以cmseasy进行开发的家居网站模板。该软件可免费使用,模板附带测试数据!模板源码特点:整体采用浅色宽屏设计,简洁大气,电脑手机自适应布局,大方美观,功能齐全,值得推荐的一款模板,每个页面精心设计,美观大方,兼容各大浏览器;所有代码经过SEO优化,使网站更利于搜索引擎排名,是您做环保类网站的明确选择。无论是在电脑、平板、手机上都可以访问到排版合适的网站,即便是微信等

现代化家居响应式网站模板1.0 0 查看详情 现代化家居响应式网站模板1.0

数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

@@######@@

使用索引合并的案例

1

2

3

4

5

6

7

8

9

10

11

12

13

@@######@@

未使用索引合并的案例

1

2

3

4

5

6

7

8

9

10

11

12

13

@@######@@

sort_union总结

从上面的两个案例大家可以发现,相同模式的sql语句,可能有时能使用索引,有时不能使用索引。是否能使用索引,取决于mysql查询优化器对统计数据分析后,是否认为使用索引更快。
因此,单纯的讨论一条sql是否可以使用索引有点片面,还需要考虑数据。

union索引合并使用案例

数据表结构

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@@######@@

数据结构和之前有所调整。主要调整有如下两方面:
1、引擎从myisam改为了innodb。
2、组合索引中增加了id,并把id放在最后。

数据

数据和上面的数据一样。

使用索引合并的案例

1

2

3

4

5

6

7

8

9

10

11

12

13

@@######@@

union总结

相同的数据,相同的sql语句,只是数据表结构有所调整,就从sort_union变为了union。有以下几个原因:
1、只要通过索引取出的数据已经按rowid进行了排序,就可以使用union。
2、组合索引中在最后加id字段,目的就是通过索引前两个字段取出的数据是按id排序。
3、把引擎从myisam改为innodb,目的就是让id和rowid的顺序一致。

intersect使用案例

mysql索引合并:一条sql可以使用多个索引
http://www.php.cn/

以上就是mysql索引合并:一条sql可以使用多个索引的内容,更多相关内容请关注PHP中文网(www.php.cn)!

mysql> show create table testG*************************** 1. row ***************************       Table: testCreate Table: CREATE TABLE `test` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `key1_part1` int(11) NOT NULL DEFAULT '0',  `key1_part2` int(11) NOT NULL DEFAULT '0',  `key2_part1` int(11) NOT NULL DEFAULT '0',  `key2_part2` int(11) NOT NULL DEFAULT '0',  PRIMARY KEY (`id`),  KEY `key1` (`key1_part1`,`key1_part2`),  KEY `key2` (`key2_part1`,`key2_part2`)) ENGINE=MyISAM AUTO_INCREMENT=18 DEFAULT CHARSET=utf81 row in set (0.00 sec)
mysql> select * from test;+----+------------+------------+------------+------------+| id | key1_part1 | key1_part2 | key2_part1 | key2_part2 |+----+------------+------------+------------+------------+|  1 |          1 |          1 |          1 |          1 ||  2 |          1 |          1 |          2 |          1 ||  3 |          1 |          1 |          2 |          2 ||  4 |          1 |          1 |          3 |          2 ||  5 |          1 |          1 |          3 |          3 ||  6 |          1 |          1 |          4 |          3 ||  7 |          1 |          1 |          4 |          4 ||  8 |          1 |          1 |          5 |          4 ||  9 |          1 |          1 |          5 |          5 || 10 |          2 |          1 |          1 |          1 || 11 |          2 |          2 |          1 |          1 || 12 |          3 |          2 |          1 |          1 || 13 |          3 |          3 |          1 |          1 || 14 |          4 |          3 |          1 |          1 || 15 |          4 |          4 |          1 |          1 || 16 |          5 |          4 |          1 |          1 || 17 |          5 |          5 |          1 |          1 || 18 |          5 |          5 |          3 |          3 || 19 |          5 |          5 |          3 |          1 || 20 |          5 |          5 |          3 |          2 || 21 |          5 |          5 |          3 |          4 || 22 |          6 |          6 |          3 |          3 || 23 |          6 |          6 |          3 |          4 || 24 |          6 |          6 |          3 |          5 || 25 |          6 |          6 |          3 |          6 || 26 |          6 |          6 |          3 |          7 || 27 |          1 |          1 |          3 |          6 || 28 |          1 |          2 |          3 |          6 || 29 |          1 |          3 |          3 |          6 |+----+------------+------------+------------+------------+29 rows in set (0.00 sec)
mysql> explain select * from test where (key1_part1=4 and key1_part2=4) or (key2_part1=4 and key2_part2=4)G*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: test         type: index_mergepossible_keys: key1,key2          key: key1,key2      key_len: 8,4          ref: NULL         rows: 3        Extra: Using sort_union(key1,key2); Using where1 row in set (0.00 sec)
mysql> explain select * from test where (key1_part1=1 and key1_part2=1) or key2_part1=4G*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: test         type: ALLpossible_keys: key1,key2          key: NULL      key_len: NULL          ref: NULL         rows: 29        Extra: Using where1 row in set (0.00 sec)
mysql> show create table testG*************************** 1. row ***************************       Table: testCreate Table: CREATE TABLE `test` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `key1_part1` int(11) NOT NULL DEFAULT '0',  `key1_part2` int(11) NOT NULL DEFAULT '0',  `key2_part1` int(11) NOT NULL DEFAULT '0',  `key2_part2` int(11) NOT NULL DEFAULT '0',  PRIMARY KEY (`id`),  KEY `key1` (`key1_part1`,`key1_part2`,`id`),  KEY `key2` (`key2_part1`,`key2_part2`,`id`)) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf81 row in set (0.00 sec)
mysql> explain select * from test where (key1_part1=4 and key1_part2=4) or (key2_part1=4 and key2_part2=4)G*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: test         type: index_mergepossible_keys: key1,key2          key: key1,key2      key_len: 8,8          ref: NULL         rows: 2        Extra: Using union(key1,key2); Using where1 row in set (0.00 sec)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/801214.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
炉石传说茶壶骑士卡组:57% 胜率的中速上分神器
上一篇 2025年11月26日 18:07:35
java怎么调用两个数组的内容
下一篇 2025年11月26日 18:07:37

相关推荐

发表回复

登录后才能评论
关注微信