MySQL解释中eq_ref和ref类型是什么意思
当我们在 SQL 查询中添加关键字“explain”作为前缀时,我们会得到一个包含一些列的表。请告诉我“类型”栏是什么。在这种情况下,eq_ref
和 ref
意味着什么。
When we prefix an SQL query with the keyword "explain" we get a table with some columns. Please tell me what is the "type" column. What does eq_ref
and ref
mean in that context.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将尝试解释...
eq_ref – 假设您有两个表。表 A 包含列(id、text),其中 id 是主键。表 B 具有相同的列(id、text),其中 id 是主键。表 A 具有以下数据:
表 B 具有以下数据:
想象 eq_ref 作为 A 和 B 之间的 JOIN:
此 JOIN 非常快,因为对于表 A 中扫描的每一行,表 A 中只能有 ONE 行表 B 满足 JOIN 条件。一且不超过一。那是因为 B.id 是唯一的。
这里是:伪代码,它说明了服务器端的处理:
ref - 现在想象另一个带有列(id,text)的表 C,其中 id 是索引,但不是独一无二的。表 C 具有以下数据:
将 ref 想象为 A 和 C 之间的 JOIN:
这里是:说明服务器端处理的伪代码:
此 JOIN 不如前一个快,因为对于表 A 中扫描的每一行,都有SEVERAL 行可能满足 JOIN 条件(嵌套循环)。这是因为 C.ID 不是唯一的。
I'll try an explanation...
eq_ref – imagine that you have two tables. Table A with columns (id, text) where id is a primary key. Table B with the same columns (id, text) where id is a primary key. Table A has the following data:
Table B has the following data:
Imagine eq_ref as JOIN between A and B:
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:
ref - Now imagine another table C with columns (id, text) in which id an index but a non UNIQUE one. Table C has the following data:
Imagine ref as JOIN between A and C:
Here you are: pseudo code illustrating the server side processing:
This JOIN is NOT as fast as the former one because for each row scanned in table A there are SEVERAL possible rows in table C which may satisfy the JOIN condition (nested loops). That is because C.ID is NOT UNIQUE.
“类型”是指您请求中创建的连接类型。从最好到最差,这里是列表:
您可以在 MySQL 文档中找到更详细的解释: http://dev.mysql.com/doc/refman/5.0/en/explain-output.html
The "type" refers to the join type made in your request. From best to worst, here is the list :
You will find a more detailed explanation at the MySQL documentation : http://dev.mysql.com/doc/refman/5.0/en/explain-output.html