全称 common table expressions,表示临时结果,用
with as
语句,可以在其他SQL中引用,如select、insert、update和delete。
相当于用with as
语句创建临时表,SQL结构清晰,并且结果可以复用。
下面是使用样例
with c1 as (
select * from t1
)
,with c2 as (
select * from c1
)
select * from c2 where id = 100;
as materialized or not as materialized 在PG12之前,with语句都是通过将子查询先进行
物化
,这就导致了一个问题:with子查询外的条件无法内推到里面,并且会产生物化视图,会影响查询性能
。 使用PG12会自动判断是否物化(自测得出,版本12.2),但是有些场景需要我们手动控制才能保证查询效率。
只有其中一个with as
物化了
没有物化的
全部物化