Hive里的With关键字用法(调优),主要包括Hive里的With关键字用法(调优)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

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

语法限制
with...as...必须和其他sql一起使用(可以定义一个with但在后续语句中不使用他)
with...as...是一次性的
with...as...的完整格式是这样的 

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

​

同级的多个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;

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

更多推荐

Hive里的With关键字用法(调优)