简体   繁体   English

比较MySQL交叉联接和内部联接

[英]Comparing MySQL Cross and Inner Joins

What are the potential pros and cons of each of these queries given different databases, configurations, etc? 给定不同的数据库,配置等,每个查询的潜在利弊是什么? Is there ever a time when one would be more efficient than the other? 有没有时间会比另一个更有效率? Vice versa? 反之亦然? Is there an even better way to do it? 有更好的方法吗? Can you explain why? 你能解释为什么吗?

Query 1: 查询1:

SELECT 
  *
FROM
  table_a, table_b, table_c
WHERE
  table_a.id = table_b.id AND
  table_a.id = table_c.id AND
  table_a.create_date > DATE('1998-01-01');

Query 2: 查询2:

SELECT 
  *
FROM
  table_a 
INNER JOIN table_b ON
  table_a.id = table_b.id
INNER JOIN table_c ON
  table_a.id = table_c.id
WHERE
  table_a.create_date > DATE('1998-01-01');

Same query, different revision of SQL spec. 相同查询,不同版本的SQL规范。 The query optimizer should come up with the same query plan for those. 查询优化器应针对这些查询提出相同的查询计划。

Nope. 不。 I'm just sharing a large, overwhelmed database with some coworkers and am trying to come up with some ways to get more processor bang for our buck. 我只是与一些同事共享一个庞大的,不堪重负的数据库,并且正在尝试提出一些方法来为我们带来更多收益。 I've been looking around online but haven't found a good explanation for some questions like this. 我一直在网上四处寻找,但找不到诸如此类问题的良好解释。

Sorry for sounding homework-y. 很抱歉,您的作业听起来不错。 I guess I spent too many years as a TA. 我想我花了很多年担任助教。

Actually, I think query 2's more readable. 实际上,我认为查询2更具可读性。 Think about when you get to say 5,6, or 7 tables when you hit the where clause in query one. 想一想当您在查询1中打到where子句时要说5、6或7个表。 Following the joins could get messy. 加入之后可能会变得混乱。

As for performance, I have no idea. 至于性能,我不知道。 I bet if you go to the MySQL website, there would be info there - probably examples of joins. 我敢打赌,如果您访问MySQL网站,那里会有信息-可能是联接的示例。

Professionally, I've only worked on one project. 专业上,我只从事一个项目。 But it was a big one, and they always followed query 2's format. 但这是一个很大的问题,他们始终遵循查询2的格式。 This was using Microsoft SQL Server though. 虽然这是使用Microsoft SQL Server。

I agree, it's sounding a bit too much like Homework! 我同意,听起来有点像作业!

If it isn't homework then I guess the simplest answer is readability. 如果不是家庭作业,那么我想最简单的答案就是可读性。

As stated before, both queries will produce the same execution plan. 如前所述,两个查询将产生相同的执行计划。 If this is the case then the only thing you need to worry about it maintainability. 如果是这种情况,那么您只需担心它的可维护性。

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

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