简体   繁体   English

左联接还是内联接?

[英]LEFT JOIN or INNER JOIN?

I have the following tables. 我有下表。 All fields are NOT NULL . 所有字段都不为NOT NULL

tb_post
    id
    account_id
    created_at
    content

tb_account
    id
    name

I want to select the latest post along with the name. 我想选择最新的帖子以及名字。 Should I use INNER JOIN or LEFT JOIN ? 我应该使用INNER JOIN还是LEFT JOIN From my understanding both produce the same results. 根据我的理解,两者都会产生相同的结果。 But which is more correct or faster? 但是哪个更正确或更快速?

SELECT p.content, a.name
FROM tb_post AS p
[INNER or LEFT] JOIN tb_account AS a
ON a.id = p.account_id
ORDER BY p.created_at DESC
LIMIT 50

A LEFT JOIN is absolutely not faster than an INNER JOIN. 左联接绝对不比内联接快。 In fact, it's slower; 实际上,它比较慢。 by definition, an outer join (LEFT JOIN or RIGHT JOIN) has to do all the work of an INNER JOIN plus the extra work of null-extending the results. 根据定义,外部联接(LEFT JOIN或RIGHT JOIN)必须完成INNER JOIN的所有工作以及对结果进行空扩展的额外工作。 It would also be expected to return more rows, further increasing the total execution time simply due to the larger size of the result set. 仅仅由于结果集的大小,也期望返回更多的行,从而进一步增加了总的执行时间。

(And even if a LEFT JOIN were faster in specific situations due to some difficult-to-imagine confluence of factors, it is not functionally equivalent to an INNER JOIN, so you cannot simply go replacing all instances of one with the other!) (即使由于某些难以想象的因素融合而使LEFT JOIN在特定情况下更快,它在功能上也不等同于INNER JOIN,因此您不能简单地将一个实例的所有实例替换为另一个实例!)

Better go for INNER JOIN. 最好去INNER JOIN。

As Per My View The Correct One Is Inner join 根据我的观点,正确的是内部联接

because it returns resultset that include only matched elements where Left Join Returns all entries from Left Table. 因为它返回只包含匹配元素的结果集,其中“左联接”返回“左表”中的所有条目。 In this case I think Inner join returns the only required amount of data to be proceed. 在这种情况下,我认为内部联接返回仅需要处理的数据量。

You have to ask yourself two questions. 您必须问自己两个问题。

1) Is there any chance that at some point in your application lifetime, there will be posts with an empty or invalid account_id ? 1)在您的应用程序生命周期中的某个时候,是否有可能使用空或无效的account_id帖子?

If not, it doesn't matter. 如果没有,那没关系。

If yes... 如是...

2) Would it be desirable to include posts without an associated account in the result of the query? 2)是否希望在查询结果中包含没有关联帐户的帖子? If yes, use LEFT JOIN, if no, use INNER JOIN. 如果是,请使用LEFT JOIN,否则请使用INNER JOIN。

I personally don't think speed is very relevant: the difference between them is what they do. 我个人认为速度不是很重要:它们之间的区别在于它们的作用。

They happen to give the same result in your case, but that does not mean they can be interchanged, because choosing the one or the other still tells the other guy that reads your code something. 在您的情况下,它们碰巧会产生相同的结果,但这并不意味着它们可以互换,因为选择一个或另一个仍会告诉另一个读取您的代码的人。

I tend to think like this: INNER JOIN - the two tables are basically ONE set, we just need to combine two sources. 我倾向于这样思考:INNER JOIN-两个表基本上是一个集合,我们只需要组合两个源即可。 LEFT JOIN - the left tables is the source, and optionally we may have additional information (in the right table). LEFT JOIN-左边的表格为来源,并且可选地,我们可能有其他信息(在右边的表格中)。

So if I would read your code and see a LEFT JOIN, that's the impression you give me about your data model. 因此,如果我阅读您的代码并看到一个LEFT JOIN,那便是您对我的数据模型的印象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM