源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

图文详解MySQL中两表关联的连接表如何创建索引

  • 时间:2022-09-20 02:43 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:图文详解MySQL中两表关联的连接表如何创建索引
本文介绍了MySQL中两表关联的连接表是如何创建索引的相关内容,分享出来供大家参考学习,下面来看看详细的介绍: [b]问题介绍[/b] 创建数据库的索引,可以选择单列索引,也可以选择创建组合索引。 遇到如下这种情况,用户表(user)与部门表(dept)通过部门用户关联表(deptuser)连接起来,如下图所示: [img]http://files.jb51.net/file_images/article/201705/201751884304507.jpg?201741884335[/img] 表间关系 问题就是,在这个关联表中该如何建立索引呢? [b]针对该表,有如下四种选择:[/b] [list] [*]针对于user_uuid建立单列索引idx_user[/*] [*]针对于user_dept建立单列索引idx_dept[/*] [*]建立组合索引idx_user_dept,即(user_uuid,dept_uuid)[/*] [*]建立组合索引idx_dept_user,即(dept_uuid,user_uuid) [/*] [/list] [b]对关联表的查询,有如下四种情况:[/b]
-- 一、人员查所属部门用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";
[b]测试验证[/b] [b]一.人员查所属部门用and方式[/b] 1.1 关联表无索引 [img]http://files.jb51.net/file_images/article/201705/201751884453464.png?20174188454[/img] 1.2 单索引 Idx_dept [img]http://files.jb51.net/file_images/article/201705/201751884515754.png?201741884525[/img] 1.3 单索引 Idx_user [img]http://files.jb51.net/file_images/article/201705/201751884704531.png?201741884712[/img] 1.4 组合索引 Idx_dept_user [img]http://files.jb51.net/file_images/article/201705/201751884727380.png?201741884736[/img] 1.5 组合索引 Idx_user_dept [img]http://files.jb51.net/file_images/article/201705/201751884746370.png?201741884755[/img] 1.6 所有都建立上 [img]http://files.jb51.net/file_images/article/201705/201751884806808.png?201741884815[/img] [b]二 、人员查所属部门用join方式[/b] 2.1 关联表无索引 [img]http://files.jb51.net/file_images/article/201705/201751884908232.png?201741884915[/img] 2.2 单索引 Idx_dept [img]http://files.jb51.net/file_images/article/201705/201751884926764.png?201741884934[/img] 2.3 单索引 Idx_user [img]http://files.jb51.net/file_images/article/201705/201751884943769.png?201741884950[/img] 2.4 组合索引 Idx_dept_user [img]http://files.jb51.net/file_images/article/201705/201751885001728.png?20174188508[/img] 2.5 组合索引 Idx_user_dept [img]http://files.jb51.net/file_images/article/201705/201751885021878.png?201741885029[/img] 2.6 所有都建立上 [img]http://files.jb51.net/file_images/article/201705/201751885039018.png?201741885047[/img] [b]三 、部门查人员用and方式[/b] 3.1 关联表无索引 [img]http://files.jb51.net/file_images/article/201705/201751885131762.png?201741885139[/img] 3.2 单索引 Idx_dept [img]http://files.jb51.net/file_images/article/201705/201751885205122.png?201741885213[/img] 3.3 单索引 Idx_user [img]http://files.jb51.net/file_images/article/201705/201751885223317.png?201741885231[/img] 3.4 组合索引 Idx_dept_user [img]http://files.jb51.net/file_images/article/201705/201751885242993.png?201741885249[/img] 3.5 组合索引 Idx_user_dept [img]http://files.jb51.net/file_images/article/201705/201751885301950.png?201741885310[/img] 3.6 所有都建立上 [img]http://files.jb51.net/file_images/article/201705/201751885319333.png?201741885328[/img] [b]四 、部门查所属人员用join方式[/b] 4.1 关联表无索引 [img]http://files.jb51.net/file_images/article/201705/201751885430712.png?201741885441[/img] 4.2 单索引 Idx_dept [img]http://files.jb51.net/file_images/article/201705/201751885454633.png?20174188551[/img] 4.3 单索引 Idx_user [img]http://files.jb51.net/file_images/article/201705/201751885510961.png?201741885518[/img] 4.4 组合索引 Idx_dept_user [img]http://files.jb51.net/file_images/article/201705/201751885529330.png?201741885536[/img] 4.5 组合索引 Idx_user_dept [img]http://files.jb51.net/file_images/article/201705/201751885546044.png?201741885553[/img] 4.6 所有都建立上 [img]http://files.jb51.net/file_images/article/201705/201751885602792.png?20174188569[/img] [b]结论[/b] 通过上面的实际测试结果可以得出如下结论:针对于该关联表分别针对于user_uuid与dept_uuid建立单列索引idx_user,idx_dept最优。 其中索引idx_user适用与通过人员ID查询出该人员所在的部门;索引idx_dept适用与通过部门查询出该部门下所属的人员。 [b]其它[/b] [b]测试数据[/b] [url=http://xiazai.jb51.net/201705/yuanma/test_sql(jb51.net).rar]Test.sql[/url] [b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部