我想知道是否有人可以解释为什么使用CTE而不是临时表使用更长的时间......我基本上删除客户表中的重复信息(为什么存在重复信息超出了本文的范围)。删除使用CTE比在Postgres中使用临时表慢
这是Postgres 9.5。
CTE的版本是这样的:
with targets as
(
select
id,
row_number() over(partition by uuid order by created_date desc) as rn
from
customer
)
delete from
customer
where
id in
(
select
id
from
targets
where
rn > 1
);
今天早上我杀了那个版本了一个多小时后运行。
的临时表的版本是这样的:
create temp table
targets
as select
id,
row_number() over(partition by uuid order by created_date desc) as rn
from
customer;
delete from
customer
where
id in
(
select
id
from
targets
where
rn > 1
);
这个版本完成约7秒。
任何想法可能会导致此?
。一定是。在你的情况下,它会产生所有的元组,即使那些rn = 1。作为临时表的替代方法,您可以将CTE重写为子查询。 (这可能会是最快的) – wildplasser