-- 表的描述
hive>
> desc tmp_for_fix_date_format;
OK
comment_id string
hid string
dt_format string
insert overwrite local directory'/tmp/100'
row format delimited fields TERMINATED by'\t'
select
b.hid,b.comment_id,b.comment_time
from tmp_for_fix_date_format a
left join hotel_info_customer_comment_format_code_merge b
on a.hid=b.hid and a.comment_id=b.comment_id
WHERE b.dp='hotel_all' and b.dt='20171225' and b.channel_id='10'
;
先做笛卡尔积,再过滤,不可取
-- 上面的执行太慢,下面的为优化之后的sql
insert overwrite local directory'/tmp/1000'
row format delimited fields TERMINATED by'\t'
select
b.hid,b.comment_id,b.comment_time
from tmp_for_fix_date_format a
JOIN
(
select hid,comment_id,comment_time from hotel_info_customer_comment_format_code_merge
WHERE dp='hotel_all' and dt='20171225' and channel_id='10' and comment_id !='数据缺失'
) b
on a.hid=b.hid and a.comment_id=b.comment_id
;
先过滤,再做笛卡尔积,可取
join的时候没有连接条件的时候结果集是最大的 ,所以Hive中Join的关联键必须在ON()中指定,不能在Where中指定,否则就会先做笛卡尔积,再过滤 ,此外如果join的时候有非等值连接的时候可以刚在where的条件中进行过滤,减少结果集 。