1110Nested Loop Join算法

时间:2024-01-13 11:41:14

转自 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