转自 http://blog.csdn.net/tonyxf121/article/details/7796657

join的实现原理

join的实现是采用Nested Loop Join算法,就是通过驱动表的结果集作为循环基础数据,然后一条一条的通过该结果集中的数据作为过滤条件到下一个表中查询数据,然后合并结果。如果有多个join,则将前面的结果集作为循环数据,再一次作为循环条件到后一个表中查询数据。

接下来通过一个三表join查询来说明MySQL的Nested Loop Join的实现方式。

  1. select m.subject msg_subject, c.content msg_content
  2. from user_group g,group_message m,group_message_content c
  3. where g.user_id = 1
  4. and m.group_id = g.group_id
  5. and c.group_msg_id = m.id
 

使用explain看看执行计划:

  1. explain select m.subject msg_subject, c.content msg_content from user_group g,group_message m,
  2. group_message_content c where g.user_id = 1 and m.group_id = g.group_id and c.group_msg_id = m.id\G;

结果如下:

  1. *************************** 1. row ***************************
  2. id: 1
  3. select_type: SIMPLE
  4. table: g
  5. type: ref
  6. possible_keys: user_group_gid_ind,user_group_uid_ind,user_group_gid_uid_ind
  7. key: user_group_uid_ind
  8. key_len: 4
  9. ref: const
  10. rows: 2
  11. Extra:
  12. *************************** 2. row ***************************
  13. id: 1
  14. select_type: SIMPLE
  15. table: m
  16. type: ref
  17. possible_keys: PRIMARY,idx_group_message_gid_uid
  18. key: idx_group_message_gid_uid
  19. key_len: 4
  20. ref: g.group_id
  21. rows: 3
  22. Extra:
  23. *************************** 3. row ***************************
  24. id: 1
  25. select_type: SIMPLE
  26. table: c
  27. type: ref
  28. possible_keys: idx_group_message_content_msg_id
  29. key: idx_group_message_content_msg_id
  30. key_len: 4
  31. ref: m.id
  32. rows: 2
  33. Extra:

从结果可以看出,explain选择user_group作为驱动表,首先通过索引user_group_uid_ind来进行const条件的索引ref查找,然后用user_group表中过滤出来的结果集group_id字段作为查询条件,对group_message循环查询,然后再用过滤出来的结果集中的group_message的id作为条件与group_message_content的group_msg_id进行循环比较查询,获得最终的结果。

这个过程可以通过如下代码来表示:

for each record g_rec in table user_group that g_rec.user_id=1{
     for each record m_rec in group_message that m_rec.group_id=g_rec.group_id{
          for each record c_rec in group_message_content that c_rec.group_msg_id=m_rec.id
                pass the (g_rec.user_id, m_rec.subject, c_rec.content) row
          combination to output;
      }
}

如果去掉group_message_content表上面的group_msg_id字段的索引,执行计划会有所不一样。

  1. drop index idx_group_message_content_msg_id on group_message_content;
  2. explain select m.subject msg_subject, c.content msg_content from user_group g,group_message m,
  3. group_message_content c where g.user_id = 1 and m.group_id = g.group_id and c.group_msg_id = m.id\G;

得到的执行计划如下:

  1. *************************** 1. row ***************************
  2. id: 1
  3. select_type: SIMPLE
  4. table: g
  5. type: ref
  6. possible_keys: user_group_uid_ind
  7. key: user_group_uid_ind
  8. key_len: 4
  9. ref: const
  10. rows: 2
  11. Extra:
  12. *************************** 2. row ***************************
  13. id: 1
  14. select_type: SIMPLE
  15. table: m
  16. type: ref
  17. possible_keys: PRIMARY,idx_group_message_gid_uid
  18. key: idx_group_message_gid_uid
  19. key_len: 4
  20. ref: g.group_id
  21. rows: 3
  22. Extra:
  23. *************************** 3. row ***************************
  24. id: 1
  25. select_type: SIMPLE
  26. table: c
  27. type: ALL
  28. possible_keys: NULL
  29. key: NULL
  30. key_len: NULL
  31. ref: NULL
  32. rows: 96
  33. Extra:Using where;Using join buffer

因为删除了索引,所以group_message_content的访问从ref变成了ALL,keys相关的信息也变成了NULL,Extra信息也变成了Using Where和Using join buffer,也就是说需要获取content内容只能通过对全表的数据进行where过滤才能获取。Using join buffer是指使用到了Cache,只有当join类型为ALL,index,rang或者是index_merge的时候才会使用join buffer,它的使用过程可以用下面代码来表示:

for each record g_rec in table user_group{
      for each record m_rec in group_message that m_rec.group_id=g_rec.group_id{
           put (g_rec, m_rec) into the buffer
           if (buffer is full)
                 flush_buffer();
      }
}
flush_buffer(){
      for each record c_rec in group_message_content that c_rec.group_msg_id = c_rec.id{
            for each record in the buffer
                 pass (g_rec.user_id, m_rec.subject, c_rec.content) row combination to output;
      }
      empty the buffer;
}
在实现过程中可以看到把user_group和group_message的结果集放到join buffer中,而不用每次user_group和group_message关联后马上和group_message_content关联,这也是没有必要的;需要注意的是join buffer中只保留查询结果中出现的列值,它的大小不依赖于表的大小,我们在伪代码中看到当join buffer被填满后,mysql将会flush buffer。

join语句的优化

1. 用小结果集驱动大结果集,尽量减少join语句中的Nested Loop的循环总次数;

2. 优先优化Nested Loop的内层循环,因为内层循环是循环中执行次数最多的,每次循环提升很小的性能都能在整个循环中提升很大的性能;

3. 对被驱动表的join字段上建立索引;

4. 当被驱动表的join字段上无法建立索引的时候,设置足够的Join Buffer Size。

增加一点:

ON是最先执行, WHERE次之,HAVING最后,因为ON是先把不符合条件的记录过滤后才进行统计,它就可以减少中间运算要处理的数据,按理说应该速度是最快的,WHERE也应该比 HAVING快点的,因为它过滤数据后才进行SUM,在两个表联接时才用ON的,所以在一个表的时候,就剩下WHERE跟HAVING比较了

1考虑联接优先顺序:

2INNER JOIN

3LEFT JOIN (注:RIGHT JOIN 用 LEFT JOIN 替代)

4CROSS JOIN

打包小工具

http://www.linuxidc.com/Linux/2014-03/98553.htm

1110Nested Loop Join算法的更多相关文章

  1. 1122MySQL性能优化之 Nested Loop Join和Block Nested-Loop Join(BNL)

    转自http://blog.itpub.net/22664653/viewspace-1692317/ 一 介绍  相信许多开发/DBA在使用MySQL的过程中,对于MySQL处理多表关联的方式或者说 ...

  2. 关于join算法的四篇文章

    MySQL Join算法与调优白皮书(一) MySQL Join算法与调优白皮书(二) MySQL Join算法与调优白皮书(三) MySQL Join算法与调优白皮书(四) MariaDB Join ...

  3. 44 答疑(三)--join的写法/Simple nested loop join的性能问题/Distinct和group by的性能/备库自增主键问题

    44 答疑(三) Join的写法 35节介绍了join执行顺序,加了straight_join,两个问题: --1 如果用left join,左边的表一定是驱动表吗 --2 如果两个表的join包含多 ...

  4. MySQL Nested-Loop Join算法学习

    不知不觉的玩了两年多的MySQL,发现很多人都说MySQL对比Oracle来说,优化器做的比较差,其实某种程度上来说确实是这样,但是毕竟MySQL才到5.7版本,Oracle都已经发展到12c了,今天 ...

  5. SQL Server的三种物理连接之Loop Join(一)

    Sql Server有三种物理连接Loop Join,Merge Join,Hash Join, 当表之间连接的时候会选择其中之一,不同的连接产生的性能不同,理解这三种物理连接对性能调优有很大帮助. ...

  6. 24.join算法/锁_1

    一. JOIN算法1.1. JOIN 语法 mysql> select * from t4; +---+------+ | a | b | +---+------+ | | 11 | | | 5 ...

  7. SQL Server nested loop join 效率试验

    从很多网页上都看到,SQL Server有三种Join的算法, nested loop join, merge join, hash join. 其中最常用的就是nested loop join. 在 ...

  8. Merge join、Hash join、Nested loop join对比分析

    简介 我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge Join,Hash Join ...

  9. 022:SQL优化--JOIN算法

    目录 一. SQL优化--JOIN算法 1.1. JOIN 写法对比 2. JOIN的成本 3. JOIN算法 3.1. simple nested loop join 3.2. index nest ...

随机推荐

  1. [转]VS2010中如何创建一个WCF

    本文转自:http://www.cnblogs.com/zhangliangzlee/archive/2012/08/28/2659701.html 转载:http://www.cnblogs.com ...

  2. 感觉没睡好就..-shenmedoumeixie....

    hi 昨晚没睡好,虽然梦很香,但睡不好没精神做科研啊... 1.jQuery 十二.实现聊天室创建 12.1 基本功能 登陆: 无刷新实时交流: 支持表情. 12.2 大致效果 登陆——>验证, ...

  3. 清除浮动clear/BFC

    浮动的清除有两种方式: 一.clear clear:both/left/right; 二.创建BFC (1)什么是BFC? BFC,块级格式化上下文,是一个独立的渲染区域,只有Block-level ...

  4. 初步了解Entity Framework

    来源:http://www.cnblogs.com/Wayou/archive/2012/09/20/EF_CodeFirst.html Entity Framework的全称是ADO.NET Ent ...

  5. django csrf 处理简介

    CSRF 是什么 CSRF 即跨站请求伪造,在用户不知情的情况下向有漏洞的网站发送请求.例如有正常网站A,恶意网站B, 用户若对A B 两个网站都有访问,B 可能伪造请求到 A,比如提交表单.至于具体 ...

  6. Java开发之Servlet生命周期

    Servlet会在服务器启动或第一次请求该Servlet的时候开始生命周期,在服务器结束的时候结束生命周期.无论请求多少次Servlet,最多只有一个Servlet实例.多个客户端并发请求Servle ...

  7. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

  8. codevs 1082 线段树练习3

    1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Description 给你N个数,有两种操作: 1: ...

  9. Unity Shaders and Effets Cookbook

    Unity Shaders and Effects Cookbook 最近在读 <Unity Shaders and Effects Cookbook> 中文名称:<Unity 着色 ...

  10. luogu[1279]字串距离

    题目描述 设有字符串X,我们称在X的头尾及中间插入任意多个空格后构成的新字符串为X的扩展串,如字符串X为”abcbcd”,则字符串“abcb□cd”,“□a□bcbcd□”和“abcb□cd□”都是X ...