简体   繁体   English

表上的(SQL)联接总是创建这些表的笛卡尔积吗?

[英]does (SQL) JOINING on TABLES ALWAYS create Cartesian products of those tables?

does JOINING on TABLES ALWAYS create Cartesian products of those tables ? 联接表是否总是创建这些表的笛卡尔积?
if a problem can be solved by sub-query and Joining which should i prefer ? 如果可以通过子查询和Join来解决问题,我应该选择哪一个?
which one is fast and memory saving ? 哪一个速度快且可节省内存?

All the questions are tightly coupled, so please dont tell to open another thread :( 所有问题都是紧密耦合的,所以请不要告诉打开另一个线程 :(

MySQL 5 MySQL 5

  1. Yes, joining always creates a Cartesian product. 是的,加入始终会创建笛卡尔积。 That's why you usually add a condition to be met: JOIN a, b ON key_a = key_b . 这就是为什么您通常要添加一个要满足的条件: JOIN a, b ON key_a = key_b If the key_b is a UNIQUE KEY in table b, you will have at most one row from b for each row of a. 如果key_b是表b中的UNIQUE KEY,则对于a的每一行,b最多具有一行。

  2. In most cases, joins are faster, but there are exceptions ( WHERE x IN ANY (subquery) ) 在大多数情况下,联接会更快,但是会有例外情况( WHERE x IN ANY (subquery)

  3. See 2. 见2。

does JOINING on TABLES ALWAYS create Cartesian products of those tables ? 联接表是否总是创建这些表的笛卡尔积?

Yes, but normall that is then filtered down and the query optimizer is smart enough to take a more efficient approach. 是的,但是过滤掉法线,查询优化器足够聪明,可以采用更有效的方法。 Anyhow, all joins without conditions ARE cartesian products ni the start. 无论如何,所有无条件的连接都是笛卡尔产品。

if a problem can be solved by sub-query and Joining which should i prefer ? 如果可以通过子查询和Join来解决问题,我应该选择哪一个?

Depends a lot on the query optimizer and the data in question. 在很大程度上取决于查询优化器和相关数据。 As in: Try it out. 如:尝试一下。 Prettym uch the only way. Prettym这是唯一的方法。

which one is fast and memory saving ? 哪一个速度快且可节省内存?

Again - try it out. 再次-试试看。

This stuff really depends no the table sizes involved AND THE FILTER CONDITIONS. 这些东西实际上不取决于所涉及的表大小和过滤条件。 Basically a materialized cartesian product in most cases is bad programming (ups, forgot the fitler conditions) or a very bad query plan. 基本上,在大多数情况下,物化笛卡尔积是不好的编程(向上,忘记了拟合条件)或非常糟糕的查询计划。 In most cases the cartesian product iwll never materialize and the query optimizer will use a different approach. 在大多数情况下,笛卡尔积永远不会实现,查询优化器将使用其他方法。

Yes, JOINING on tables always create Cartesian products of those tables. 是的,在表上进行联接总是会创建这些表的笛卡尔积。
you can't prefer join or sub-query because it depends on your data size 您不喜欢联接或子查询,因为这取决于您的数据大小
so, you have to try the two ways then you can decide which one is more appropriate to your query 因此,您必须尝试两种方式,然后才能确定哪种方式更适合您的查询

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

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