MySQL:内连接与Where连接
之间的性能(在 mysql 中)有区别吗
Select * from Table1 T1
Inner Join Table2 T2 On T1.ID = T2.ID
And
Select * from Table1 T1, Table2 T2
Where T1.ID = T2.ID
?
Is there a difference in performance (in mysql) between
Select * from Table1 T1
Inner Join Table2 T2 On T1.ID = T2.ID
And
Select * from Table1 T1, Table2 T2
Where T1.ID = T2.ID
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我会这么认为,因为第一个例子明确告诉mysql要加入哪些列以及如何加入它们,第二个例子mysql必须尝试找出你想要加入的位置。
I would think so because the first example explicitly tells mysql which columns to join and how to join them where the second one mysql has to try and figure out where you want to join.
第二个查询只是内部联接的另一种表示法,因此,如果性能存在差异,那只是因为一个查询可以比另一个查询更快地解析 - 并且这种差异(如果存在)将非常小,以至于您不会'没有注意到它。
有关更多信息,您可以尝试查看此问题(下次在提问之前使用搜索已经回答了)
the second query is just another notation for an inner join, so if there is a difference in porformance it's only because one query can be parsed faster than the other one - and that difference, if it exists, will be so tiny that you won't notice it.
for more information you could try to take a look at this question (and use the search on SO next time before asking a question that already is answered)
第一个查询对于 MySQL 来说更容易理解,因此执行计划可能会更好并且查询会运行得更快。
没有 where 子句的第二个查询是交叉联接。如果 MySQL 能够足够好地理解 where 子句,它将尽力避免交叉连接所有行,但没有任何保证。
在像您这样简单的情况下,性能将完全相同。
就性能而言,第一个查询总是比第二个查询更好或相同。从我的角度来看,重读时也更容易理解。
The first query is easier to understand for MySQL so it is likely that the execution plan will be better and that the query will run faster.
The second query without the where clause, is a cross join. If MySQL is able to understand the where clause good enough, it will do its best to avoid cross joining all the rows, but nothing guarantee that.
In a case as simple as yours, the performance will be strictly identical.
Performance wise, the first query will always be better or identical to the second one. And from my point of view it is also a lot easier to understand when rereading.
正如问题 44917 中已接受的答案所示:
在MySql 中结果是相同的。
我个人会坚持明确地连接表......这是“社会可接受的”这样做的方式。
As pulled from the accepted answer in question 44917:
In MySql the results are the same.
I would personally stick with joining tables explicitly... that is the "socialy acceptable" way of doing it.
他们是一样的。这可以通过运行
EXPLAIN
命令看到:They are the same. This can be seen by running the
EXPLAIN
command:我的一个迟来的答案是,因为我正在分析旧应用程序的性能,该应用程序使用基于逗号的联接而不是
INNER JOIN
子句。所以这里有两个有连接的表(两个表都有超过 1 个 lac 的记录)。执行具有基于逗号的联接的查询时,它比
INNER JOIN
情况花费的时间要长得多。当我分析解释语句时,我发现具有逗号连接的查询正在使用连接缓冲区。但是,具有 INNER JOIN 子句的查询具有“usingWhere”。
此外,这些查询也有显着不同,如解释查询中的行列所示。
这些是我的查询及其各自的解释结果。
速度/秒
Well one late answer from me, As I am analyzing performance of a older application which uses comma based join instead of
INNER JOIN
clause.So here are two tables which have a join (both have records more than 1 lac). When executing query which has a comma based join, it takes a lot longer than the
INNER JOIN
case.When I analyzed the explain statement, I found that the query having comma join was using the join buffer. However the query having
INNER JOIN
clause had 'using Where'.Also these queries are significantly different, as shown in rows column in explain query.
These are my queries and their respective explain results.
v/s
实际上它们实际上是相同的,JOIN / ON 是较新的 ANSI 语法,WHERE 是较旧的 ANSI 语法。两者都被查询引擎识别
Actually they are virtually the same, The JOIN / ON is newer ANSI syntac, the WHERE is older ANSI syntax. Both are recognized by query engines
FROM 子句中的逗号是 CROSS JOIN。我们可以想象 SQL Server 有一个选择查询执行过程,它应该看起来像这样:
1. 遍历每个表
2. 找到满足连接谓词的行并将其放入结果表中。
3. 从结果表中,仅获取那些满足where条件的行。
如果确实如此,那么在检查 where 条件之前将每行相互组合时,在具有数千行的表上使用 CROSS JOIN 可能会分配大量内存。那么你的 SQL 服务器可能会非常繁忙。
The comma in a FROM clause is a CROSS JOIN. We can imagine that SQL server has a select query execution procedure which somehow should look like that:
1. iterate through every table
2. find rows that meet join predicate and put it into result table.
3. from the result table, get only those rows that meets the where condition.
If it really looks like that, then using a CROSS JOIN on a table that has a few thousands rows could allocate a lot of memory, when every row is combined with each other before the where condition is examined. Your SQL server could be quite busy then.