Hive的join documentation鼓励使用隐式连接,即
SELECT *
FROM table1 t1, table2 t2, table3 t3
WHERE t1.id = t2.id AND t2.id = t3.id AND t1.zipcode = '02535';
这相当于
SELECT t1.*, t2.*, t3.*
FROM table1 t1
INNER JOIN table2 t2 ON
t1.id = t2.id
INNER JOIN table3 t3 ON
t2.id = t3.id
WHERE t1.zipcode = '02535'
,或者上面会返回额外的记录吗?
答案 0 :(得分:2)
并非总是如此。您的查询是等效的。但如果没有WHERE t1.id = t2.id AND t2.id = t3.id
,它将是CROSS JOIN
。
更新
这是一个有趣的问题,我决定添加一些演示。让我们创建两个表:
A(c1 int, c2 string)
和B(c1 int, c2 string)
。
加载数据:
insert into table A
select 1, 'row one' union all
select 2, 'row two';
insert into table B
select 1, 'row one' union all
select 3, 'row three';
检查数据:
hive> select * from A;
OK
1 row one
2 row two
Time taken: 1.29 seconds, Fetched: 2 row(s)
hive> select * from B;
OK
1 row one
3 row three
Time taken: 0.091 seconds, Fetched: 2 row(s)
检查交叉联接(不将where
转换为交叉的隐式联接):
hive> select a.c1, a.c2, b.c1, b.c2 from a,b;
Warning: Map Join MAPJOIN[14][bigTable=a] in task 'Stage-3:MAPRED' is a cross product
Warning: Map Join MAPJOIN[22][bigTable=b] in task 'Stage-4:MAPRED' is a cross product
Warning: Shuffle Join JOIN[4][tables = [a, b]] in Stage 'Stage-1:MAPRED' is a cross product
OK
1 row one 1 row one
2 row two 1 row one
1 row one 3 row three
2 row two 3 row three
Time taken: 54.804 seconds, Fetched: 4 row(s)
检查内部联接(where
的隐式联接作为INNER):
hive> select a.c1, a.c2, b.c1, b.c2 from a,b where a.c1=b.c1;
OK
1 row one 1 row one
Time taken: 38.413 seconds, Fetched: 1 row(s)
尝试通过将OR b.c1 is null
添加到where:
hive> select a.c1, a.c2, b.c1, b.c2 from a,b where (a.c1=b.c1) OR (b.c1 is null);
OK
1 row one 1 row one
Time taken: 57.317 seconds, Fetched: 1 row(s)
正如您所看到的,我们再次获得内部联接。 or b.c1 is null
被忽略
现在left join
没有where
和ON
子句(转换为CROSS):
select a.c1, a.c2, b.c1, b.c2 from a left join b;
OK
1 row one 1 row one
1 row one 3 row three
2 row two 1 row one
2 row two 3 row three
Time taken: 37.104 seconds, Fetched: 4 row(s)
你可以看到我们再次交叉。
使用where
子句尝试左连接,不使用ON
(作为INNER):
select a.c1, a.c2, b.c1, b.c2 from a left join b where a.c1=b.c1;
OK
1 row one 1 row one
Time taken: 40.617 seconds, Fetched: 1 row(s)
我们获得了INNER加入
使用where
子句尝试左连接,不使用ON
+尝试允许空值:
select a.c1, a.c2, b.c1, b.c2 from a left join b where a.c1=b.c1 or b.c1 is null;
OK
1 row one 1 row one
Time taken: 53.873 seconds, Fetched: 1 row(s)
再次得到INNER。或b.c1 is null
被忽略。
使用on
子句进行左连接:
hive> select a.c1, a.c2, b.c1, b.c2 from a left join b on a.c1=b.c1;
OK
1 row one 1 row one
2 row two NULL NULL
Time taken: 48.626 seconds, Fetched: 2 row(s)
是的,这是真正的左连接。
与on
+ where
进行左联接(获得INNER):
hive> select a.c1, a.c2, b.c1, b.c2 from a left join b on a.c1=b.c1 where a.c1=b.c1;
OK
1 row one 1 row one
Time taken: 49.54 seconds, Fetched: 1 row(s)
我们得到了INNER,因为WHERE不允许使用NULLS。
左边连接,其中+允许空值:
hive> select a.c1, a.c2, b.c1, b.c2 from a left join b on a.c1=b.c1 where a.c1=b.c1 or b.c1 is null;
OK
1 row one 1 row one
2 row two NULL NULL
Time taken: 55.951 seconds, Fetched: 2 row(s)
是的,它会被加入。
结论: