hive中with...as...的用法

作者: 我傻笑你跑掉 | 来源:发表于2019-06-14 16:59 被阅读0次

with...as...也叫做子查询部分,语句允许hive定义一个sql片段,供整个sql使用

简介

with...as...需要定义一个sql片段,会将这个片段产生的结果集保存在内存中,
后续的sql均可以访问这个结果集和,作用与视图或临时表类似.

语法限制

  1. with...as...必须和其他sql一起使用(可以定义一个with但在后续语句中不使用他)
  2. with...as...是一次性的

with...as...的完整格式是这样的

-- with table_name as(子查询语句) 其他sql 
with temp as (
    select * from xxx
)
select * from temp;

只定义不实用

with temp as (
    select * from xxx
)
select * from othertable;

同级的多个temp之间用,分割with只需要一次,as后的子句必须用(),

with temp1 as (
    select * from xxx
),temp2 as (
    select * from xxx
)
select * from temp1,temp2;

with...as...当然是可以嵌套的,此处举一个简单例子

with temp2 as (
    with temp1 as (
        select * from xxx
    )
    select * from temp1
)
select * from temp2;

with...as...只能在一条sql中使用

with temp1 as (
    select * from xxx
)
select * from temp1;
select xxx from temp1; -- error! no table named temp1;

语句的优点

  1. 提高代码可读性(结构清晰)
  2. 简化sql,优化执行速度(with子句只需要执行一次)

栗子

现有 city 表,结构如下:

city_number city_name province
010 北京 北京
021 上海 上海
025 南京 江苏
0512 昆山 江苏
0531 济南 山东
0533 淄博 山东

然后分别有商品表good

city_number good_name
010 A
021 B

现在需要分别统计这上海商品,一般sql如下:

select * from `good`  where city_number in (select city_number from city where city_name = "上海");

除了子查询,上述的的例子还可以用join来实现,

如果用with...as...语句实现,如下

with tmp_shanghai as(
    select city_number from city where city_name = "上海"
)
select * from `good` where tmp_shanghai in (select * from tmp_shanghai) 

看起来使用 with...as... 语句反而更复杂一点,但如果tmp_shanghai要被多次使用的使用,就很有必要

来看一个实际的例子,有一张操作表event主要字段如下:

date event_key
20190530 Delete
20190530 Put
20190530 Get
20190530 Get
20190601 Set

......

现在要求一条sql统计出GetSet 操作的数量,先使用子查询实现

select (
    select count(*) from event where event_key = "Get"
) as get_num,(
    select count(*) from event where event_key = "Set"
) as set_num

如果再增加其他项的统计呢,是否每一个均需要增加一个对event表进行扫描的自查询

使用 with...as...

with temp as(
    select * from event where event_key = "Get" or event_key = "Set"
)
select 
    sum(case when event_key = "Get" then 1 else 0 end) as get_num,
    sum(case when event_key = "Set" then 1 else 0 end) as Set_num
from temp

阅读性是否比之前有所提高?此外,这条语句只对event表进行了一次扫描,将符合条件的数据存入temp中供后续计算,
在event表数据集非常大的情况下,性能将比子查询的方式优秀很多

相关文章

  • hive中with...as...的用法

    with...as...也叫做子查询部分,语句允许hive定义一个sql片段,供整个sql使用 简介 with.....

  • hive积累大全

    此篇内容:hive自定义函数UDF、UDTF,压缩存储方式,hive优化、hive实际编程SQL中的if表达式用法...

  • Python中with...as...

    先说明一个常见问题,文件打开: try: f =open('xxx')dosomethingexcept:dos...

  • hive with as用法

    hive 可以通过with查询来提高查询性能,因为先通过with语法将数据查询到内存,然后后面其它查询可以直接使用。

  • Hive 自定义函数

    系统内置函数 查看系统内置函数hive> show functions;显示内置函数用法hive> desc fu...

  • Python文件读写操作-4·With...as...用法

    一、With...as...概述: with语句 是用来简化代码的 在将打开文件的操作放在 with 语句中,代码...

  • hive表分区查看

    仅针对hive内表,查看表(分区)创建时间 用法 脚本名: hdfsls.sh在~/.bashrc 中添加,wor...

  • HIVE中regexp_extract用法

    函数描述: 字符串正则表达式解析函数。-- 这个函数有点类似于 substring(str from 'regex...

  • Lateral View和Explode用法简介

    一、Explode用法 hive wiki对于expolde的解释如下: explode() takes in a...

  • [译]Hive学习指南(三)

    加载数据 有多种方式将数据加载到Hive表中。用户可以创建外部表,指向HDFS中的指定路径。在这种特殊用法中,用户...

网友评论

    本文标题:hive中with...as...的用法

    本文链接:https://www.haomeiwen.com/subject/tmupfctx.html