mysql 一次扫描多次链接 机制是什么意思

昆仑山的昆 2013-01-09 03:44:38
最近在搞mysql的优化 ,看了看官方的手册。那中文翻译实在生涩。看到了“一次扫描多次链接”
上网找了找,有了自己的一些理解。
表tblA,tblB,tblC
SELECT
*
FROM
tblA,
tblB,
tblC
WHERE
tblA.col1 = tblB.col1
AND tblA.col2 = tblC.col1;
1 首先扫描tblA,然后在tblB中找到相应的值
2 扫描tblB,在tblC中找到对应的值
3 OK扫描完成了。开始链接.在tblA中的值与tblB中的值进行比较,如果相同再拿着这个值到tblC中比较。如果相同则返回。然后下一行继续再在tblA中取值与tblB比较,如果相同再到tblC中比较。

请问高手我理解的对不对。如果不对,正确的理解应该是什么样。
...全文
388 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
珠海-天堂 2013-01-17
  • 打赏
  • 举报
回复
引用 7 楼 ACMAIN_CHM 的回复:
MySQL resolves all joins using a single-sweep multi-join method. This means that MySQL reads a row from the first table, and then finds a matching row in the second table, the third table……
版主,你这个意思,不是“左深度树”
昆仑山的昆 2013-01-16
  • 打赏
  • 举报
回复
引用 11 楼 ACMAIN_CHM 的回复:
如下代码即不是想法1:,也不是想法2。 C# code?12345678910foreach (rowA in tableA){ foreach (rowB in tableB where col1=rowA.col1) { foreach (rowC in tableC where col1=rowA.col2) { ……
感谢指点,这俩天弄了本mysql的书看,正在钻研中. 结贴了~ 也感谢大家.~
昆仑山的昆 2013-01-10
  • 打赏
  • 举报
回复
引用 7 楼 ACMAIN_CHM 的回复:
MySQL resolves all joins using a single-sweep multi-join method. This means that MySQL reads a row from the first table, and then finds a matching row in the second table, the third table……
现在我有点乱是因为在我产生了俩种想法 想法1: A表与B表比较获得结果集合1, B表与C表比较获得结果集合2, 集合1和集合2再比较,获得最终结果。 想法2:(也就是嵌套循环) A表与B表比较获得结果集合1, 结果集合1与C表比较,获得最终结果。 不知道哪个对。
昆仑山的昆 2013-01-10
  • 打赏
  • 举报
回复
引用 7 楼 ACMAIN_CHM 的回复:
MySQL resolves all joins using a single-sweep multi-join method. This means that MySQL reads a row from the first table, and then finds a matching row in the second table, the third table……
------------------------------------------------------------------------------------ 虽然我还是没怎么理解这句话的意思,但是根据你给的代码,我感觉是嵌套循环的意思。 1 A所有行扫描B表所有行,然后找到符合条件的集合 2 获得符合条件的集合后,拿出第一条到C中查是否有符合条件的行。如果有,则输出。 3 然后返回到符合条件的集合,拿出第二条到C中查是否有符合第二条的行。如果有,则输出。 ----------------------------------------------------
ACMAIN_CHM 2013-01-10
  • 打赏
  • 举报
回复
如下代码即不是想法1:,也不是想法2。
foreach (rowA in tableA)
{
  foreach (rowB in tableB where col1=rowA.col1)
  {
		foreach (rowC in tableC where col1=rowA.col2)
        {
           out<< ...
        }
  }
}
小小小小周 2013-01-10
  • 打赏
  • 举报
回复
支持 想法2
ACMAIN_CHM 2013-01-09
  • 打赏
  • 举报
回复
MySQL resolves all joins using a single-sweep multi-join method. This means that MySQL reads a row from the first table, and then finds a matching row in the second table, the third table, and so on. When all tables are processed, MySQL outputs the selected columns and backtracks through the table list until a table is found for which there are more matching rows. The next row is read from this table and the process continues with the next table. foreach (rowA in tableA) { foreach (rowB in tableB where col1=rowA.col1) { foreach (rowB in tableC where col1=rowA.col2) { out<< ... } } }
昆仑山的昆 2013-01-09
  • 打赏
  • 举报
回复
引用 2 楼 rucypli 的回复:
你的理解是正确的 就是一行一行扫描
MySQL用一遍扫描多次联接(single-sweep multi-join)的方式解决所有联接。这意味着MySQL从第一个表中读一行,然后找到在第二个表中的一个匹配行,然后在第3个表中等等。当所有的表处理完后,它输出选中的列并且返回表清单直到找到一个有更多的匹配行的表从该表读入下一行并继续处理下一个表。 请问高手有颜色的句子怎么理解~~
珠海-天堂 2013-01-09
  • 打赏
  • 举报
回复
select ... from a,b,c where a.id=b.id and a.id=c.id = select ... from a inner join b on a.id=b.id inner join c on a.id=c.id;
昆仑山的昆 2013-01-09
  • 打赏
  • 举报
回复
大家都进来讨论,讨论嘛。自己UP一下。
昆仑山的昆 2013-01-09
  • 打赏
  • 举报
回复
引用 1 楼 zuoxingyu2012 的回复:
你的理解不对 a join b join c的执行过程是这样的 1:a join b -->得到临时表TMP。 2:TMP JOIN C--->得到最终结果集。
我写的句子貌似不是join链接哦 你的意思是我写的句子和join的效果是一样的么。
rucypli 2013-01-09
  • 打赏
  • 举报
回复
你的理解是正确的 就是一行一行扫描
珠海-天堂 2013-01-09
  • 打赏
  • 举报
回复
你的理解不对 a join b join c的执行过程是这样的 1:a join b -->得到临时表TMP。 2:TMP JOIN C--->得到最终结果集。

56,913

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧