博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql联合索引abc
阅读量:3920 次
发布时间:2019-05-23

本文共 1663 字,大约阅读时间需要 5 分钟。

mysql 联合索引 复合索引 (abc) 命中率:在工作中经常会使用到联合索引,在百度查询多很多资料也有说法不一的、给大家实测下100w数据下查询命中率,废话不多说、上干货;

创建测试表:

SET NAMES utf8mb4;

SET FOREIGN_KEY_CHECKS = 0;
CREATE TABLE `test`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `a` int(11) NOT NULL DEFAULT 1,
  `b` int(11) NOT NULL DEFAULT 1,
  `c` int(11) NOT NULL DEFAULT 1,
  `d` int(11) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;

SET FOREIGN_KEY_CHECKS = 1;

在插入数据前添加索引,在100w数据添加还可以接受 数量越大越慢 带来的也是io占用 具体原理百度搜索 mysql innoDB索引原理(聚簇索引) 、 b+树。(或后续更新

alter table test add index sindex(a,b,c);

批量插入100w条数据做测试,漫长的等待过程,超时 set_time_limit(0);

$n = 1000000;for ($i = 0; $i <= $n; $i++) {    //a 范围 0 - 1000 随机整数    //b 范围 0 - 10000 随机整数    //c 范围 0 - 100000 随机整数    //d 范围 0 - 1000 随机整数    $data['a'] = rand(0, 1000);    $data['b'] = rand(0, 10000);    $data['c'] = rand(0, 100000);    $data['d'] = rand(0, 1000);    DB::table('test')->insert($data);}

测试索引命中、为了更清晰直观体现索引命中率、我把结果都截图到每条语句的下方导致篇幅过长;

1.explain select * from test where a = 437 and b = 7800 and c = 33561;

2.explain select * from test where a = 437 and c = 7800 and b = 33561;

3.explain select * from test where a = 437 and b = 33561;

4.explain select * from test where a = 437 

5.explain select * from test where c = 7800 and b = 33561;

6.explain select * from test where b = 33561;

7.explain select * from test where b = 33561 and a = 437;

8.explain select * from test where b = 33561 and c = 7800 and a = 437; 

结论:

1.and and 只要用到了最左侧a都会使用到索引

2.a and b or c 不会使用索引

3.最左侧a列被大于、小于、不等于比较的、不使用索引

4.b列 c列使用大于、小于、不等于比较的 会使用索引

5. order  by a desc group by a 只要保持最左侧a原则 都会使用索引;

转载地址:http://yahrn.baihongyu.com/

你可能感兴趣的文章
Linux用户身份及切换
查看>>
Linux用户权限
查看>>
Linux文档目录
查看>>
三层交换机 VS 二层交换机
查看>>
GUN,Linux,GUN/Linux
查看>>
Centos设定PATH
查看>>
Linux操作系统4.5.6.7代差别
查看>>
文件系统EXT3,EXT4和XFS的区别
查看>>
Centos7 udev
查看>>
Nmcli 网络管理命令行工具
查看>>
Linux IP地址配置
查看>>
firewalld和iptables
查看>>
SELinux
查看>>
nmcli双网卡绑定
查看>>
nmcli 网卡链路绑定team
查看>>
Linux下profile和bashrc四种的区别
查看>>
Linux文件查看指令整理
查看>>
Linux的三个时间参数
查看>>
Linux 用户ID和组ID
查看>>
Linux /etc/passwd文件
查看>>