本文介绍了MySQL中两表关联的连接表是如何创建索引的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:
问题介绍
创建数据库的索引,可以选择单列索引,也可以选择创建组合索引。
遇到如下这种情况,用户表(user)与部门表(dept)通过部门用户关联表(deptuser)连接起来,如下图所示:
![](//img.jbzj.com/file_images/article/201705/201751884304507.jpg?201741884335)
表间关系
问题就是,在这个关联表中该如何建立索引呢?
针对该表,有如下四种选择:
- 针对于user_uuid建立单列索引idx_user
- 针对于user_dept建立单列索引idx_dept
- 建立组合索引idx_user_dept,即(user_uuid,dept_uuid)
- 建立组合索引idx_dept_user,即(dept_uuid,user_uuid)
对关联表的查询,有如下四种情况:
-- 一、人员查所属部门用and方式
EXPLAIN SELECT d.dept_name,u.* FROM org_dept d,org_user u,org_dept_user duser WHERE u.user_uuid=duser.user_uuid AND d.dept_uuid=duser.dept_uuid AND u.user_code="dev1";
-- 二、人员查所属部门用join方式
EXPLAIN SELECT d.dept_name,u.* FROM org_user u LEFT JOIN org_dept_user du ON u.user_uuid=du.user_uuid LEFT JOIN org_dept d ON du.dept_uuid=d.dept_uuid WHERE u.user_code="dev1";
-- 三、部门查人员用and方式
EXPLAIN SELECT d.dept_name,u.* FROM org_dept d,org_user u,org_dept_user du WHERE u.user_uuid=du.user_uuid AND d.dept_uuid=du.dept_uuid AND d.dept_code="D006";
-- 四、部门查所属人员用join方式
EXPLAIN SELECT d.dept_name,u.* FROM org_dept d LEFT JOIN org_dept_user du ON d.dept_uuid=du.dept_uuid LEFT JOIN org_user u ON u.user_uuid=du.user_uuid WHERE d.dept_code="D006";
测试验证
一.人员查所属部门用and方式
1.1 关联表无索引
![](//img.jbzj.com/file_images/article/201705/201751884453464.png?20174188454)
1.2 单索引 Idx_dept
![](//img.jbzj.com/file_images/article/201705/201751884515754.png?201741884525)
1.3 单索引 Idx_user
![](//img.jbzj.com/file_images/article/201705/201751884704531.png?201741884712)
1.4 组合索引 Idx_dept_user
![](//img.jbzj.com/file_images/article/201705/201751884727380.png?201741884736)
1.5 组合索引 Idx_user_dept
![](//img.jbzj.com/file_images/article/201705/201751884746370.png?201741884755)
1.6 所有都建立上
![](//img.jbzj.com/file_images/article/201705/201751884806808.png?201741884815)
二 、人员查所属部门用join方式
2.1 关联表无索引
![](//img.jbzj.com/file_images/article/201705/201751884908232.png?201741884915)
2.2 单索引 Idx_dept
![](//img.jbzj.com/file_images/article/201705/201751884926764.png?201741884934)
2.3 单索引 Idx_user
![](/d/20211018/63490c2996dc4e79fb5aa3a55121d19e.gif)
2.4 组合索引 Idx_dept_user
![](//img.jbzj.com/file_images/article/201705/201751885001728.png?20174188508)
2.5 组合索引 Idx_user_dept
![](//img.jbzj.com/file_images/article/201705/201751885021878.png?201741885029)
2.6 所有都建立上
![](//img.jbzj.com/file_images/article/201705/201751885039018.png?201741885047)
三 、部门查人员用and方式
3.1 关联表无索引
![](//img.jbzj.com/file_images/article/201705/201751885131762.png?201741885139)
3.2 单索引 Idx_dept
![](//img.jbzj.com/file_images/article/201705/201751885205122.png?201741885213)
3.3 单索引 Idx_user
![](//img.jbzj.com/file_images/article/201705/201751885223317.png?201741885231)
3.4 组合索引 Idx_dept_user
![](//img.jbzj.com/file_images/article/201705/201751885242993.png?201741885249)
3.5 组合索引 Idx_user_dept
![](//img.jbzj.com/file_images/article/201705/201751885301950.png?201741885310)
3.6 所有都建立上
![](//img.jbzj.com/file_images/article/201705/201751885319333.png?201741885328)
四 、部门查所属人员用join方式
4.1 关联表无索引
![](//img.jbzj.com/file_images/article/201705/201751885430712.png?201741885441)
4.2 单索引 Idx_dept
![](//img.jbzj.com/file_images/article/201705/201751885454633.png?20174188551)
4.3 单索引 Idx_user
![](//img.jbzj.com/file_images/article/201705/201751885510961.png?201741885518)
4.4 组合索引 Idx_dept_user
![](//img.jbzj.com/file_images/article/201705/201751885529330.png?201741885536)
4.5 组合索引 Idx_user_dept
![](//img.jbzj.com/file_images/article/201705/201751885546044.png?201741885553)
4.6 所有都建立上
![](//img.jbzj.com/file_images/article/201705/201751885602792.png?20174188569)
结论
通过上面的实际测试结果可以得出如下结论:针对于该关联表分别针对于user_uuid与dept_uuid建立单列索引idx_user,idx_dept最优。
其中索引idx_user适用与通过人员ID查询出该人员所在的部门;索引idx_dept适用与通过部门查询出该部门下所属的人员。
其它
测试数据
Test.sql
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
您可能感兴趣的文章:- MySQL 索引的优缺点以及创建索引的准则
- MySQL字符串索引更合理的创建规则讨论
- MySQL使用命令创建、删除、查询索引的介绍
- 怎样正确创建MySQL索引的方法详解
- MySQL创建全文索引分享
- 详解mysql索引总结----mysql索引类型以及创建
- MySQL查看、创建和删除索引的方法
- mysql 添加索引 mysql 如何创建索引
- MySQL创建索引需要了解的