当我们在SQL查询前面加上关键字"EXPLAIN"时,我们会得到一个包含一些列的表.请告诉我"类型"一栏是什么.在这种情况下,eq_ref
和ref
意味着什么.
当我们在SQL查询前面加上关键字"EXPLAIN"时,我们会得到一个包含一些列的表.请告诉我"类型"一栏是什么.在这种情况下,eq_ref
和ref
意味着什么.
我试着解释一下...
eq_ref-假设您有两张表.包含列(id、text)的表A,其中id是主键.具有相同列(id、text)的表B,其中id是主键.表A有以下数据:
1, Hello
2, How are
表B有以下数据:
1, world!
2, you?
将eq_ref想象为A和B之间的连接:
select A.text, B.text where A.ID = B.ID
This JOIN is very fast because for each row scanned in table A there can be only ONE row in table B which satisfies the JOIN condition. One and no more than one. That is because B.id is UNIQUE.
Here you are: pseudo code which illustrates the processing at server side:
foreach (rowA in A)
{
if (existsInBRowWithID(rowA.id)
{
addToResult(rowA.text, getRowInBWithID(rowA.id).text);
}
}
ref-现在设想另一个具有列(id,text)的表C,其中id是索引,但不是唯一的.表C有以下数据:
1, John!
1, Jack!
将ref想象为A和C之间的连接:
select A.text, C.text where A.ID = C.ID
下面是:演示服务器端处理的伪代码:
foreach (rowA in A)
{
foreach (rowC in C)
{
if (rowA.id == rowC.id)
{
addToResult(rowA.text, rowC.text);
}
}
}
此联接不如前一个联接快,因为对于表A中扫描的每一行,表C中有SEVERAL个可能的行可能满足联接条件(嵌套循环).这是因为C.ID不是唯一的.
我希望这能帮上忙...
干杯!