hive中使用with as优化SQL语句

前言

hive可以通过with查询来提高查询性能,因为先通过with语法将数据查询到内存(既然是加到内存中,如果数据量太大,估计会占用大量资源,可能会报内存溢出),然后后面其它查询可以直接使用,这种方法与创建临时表类似但是不需要创建临时表实体表,内存中的子查询结果在会话结束后会自动删除。使用with as可提高代码可读性。

公用表表达式(CTE)是从WITH子句中指定的简单查询派生的临时结果集(会把查询的表数据放到内存中,供其他查询随时使用),该子句紧跟在SELECT或INSERT关键字之前。CTE仅在单个语句的执行范围内定义。可以在Hive SELECT,INSERT, CREATE TABLE AS SELECT或CREATE VIEW AS SELECT语句中使用一个或多个CTE 。

使用HIVE-1180在Hive 0.13.0中添加了通用表格表达式。

语法

withClause: cteClause (, cteClause)*
cteClause: cte_name AS (select statment)

注意

SubQuery Blocks中不支持WITH子句
Views,CTAS和INSERT语句支持CTE。
不支持递归查询。

举例

在 select 中使用 CTE

with q1 as ( select key from src where key = '5')
select *
from q1;
 
-- from style
with q1 as (select * from src where key= '5')
from q1
select *;
  
-- chaining CTEs
with q1 as ( select key from q2 where key = '5'),
q2 as ( select key from src where key = '5')
select * from (select key from q1) a;

with q1 as ( select key from src where key = '5'),
q2 as ( select key from q1 where key = '5')
select * from (select key from q2) a;

-- union example
with q1 as (select * from src where key= '5'),
q2 as (select * from src s2 where key = '4')
select * from q1 union all select * from q2;

chaining CTEs 两个举例执行结果一样

CTE in Views, CTAS, and Insert Statements

-- insert example
create table s1 like src;
with q1 as ( select key, value from src where key = '5')
from q1
insert overwrite table s1
select *;
 
-- ctas example
create table s2 as
with q1 as ( select key from src where key = '4')
select * from q1;
 
-- view example
create view v1 as
with q1 as ( select key from src where key = '5')
select * from q1;
select * from v1;
  
-- view example, name collision
create view v1 as
with q1 as ( select key from src where key = '5')
select * from q1;
with q1 as ( select key from src where key = '4')
select * from v1;

在第二个View示例中,查询的CTE与创建视图时使用的CTE不同。结果将包含key ='5’的行,因为在视图的查询语句中,视图定义中定义的CTE生效。

with的相关总结

使用with子句可以让子查询重用相同的with查询块,通过select调用(with子句只能被select查询块引用),一般在with查询用到多次情况下。

with子句的返回结果存到用户的临时表空间中,只做一次查询,反复使用,提高效率。

在同级select前有多个查询定义的时候,第1个用with,后面的不用with,并且用逗号隔开。

最后一个with 子句与下面的查询之间不能有逗号,只通过右括号分割,with 子句的查询必须用括号括起来。

如果定义了with子句,而在查询中不使用,那么会报ora-32035 错误:未引用在with子句中定义的查询名。(至少一个with查询的name未被引用,解决方法是移除未被引用的with查询),注意:只要后面有引用的就可以,不一定非要在主查询中引用,比如后面的with查询也引用了,也是可以的。

前面的with子句定义的查询在后面的with子句中可以使用。但是一个with子句内部不能嵌套with子句。

当一个查询块名字和一个表名或其他的对象相同时,解析器从内向外搜索,优先使用子查询块名字。

with查询的结果列有别名,引用的时候必须使用别名或*。

官网地址:https://cwiki.apache.org/confluence/display/Hive/Common+Table+Expression#app-switcher

猜你喜欢

转载自blog.csdn.net/lz6363/article/details/88379899