我生成两个新旧表,它们包含相同的行集。有什么方法可以加快他们的加入速度?
WITH new as (
SELECT uid, segid, true AS b
FROM all_segments
),
old as (
select uid, segid, bool_or(status) as b
from users_segment_history_current
group by uid, segid
)
select uid, segid, now(), coalesce(new.b, false), false , now() - ('-420 min')::interval, old.b as old_b
from new full outer join old using (uid, segid)
where coalesce(new.b,false) is distinct from old.b;
解释分析:
QUERY PLAN
Merge Full Join (cost=52202.14..55745.83 rows=68964 width=28) (actual time=1565.272..1918.952 rows=219185 loops=1)
Merge Cond: ((new.uid = old.uid) AND (new.segid = old.segid))
Filter: (COALESCE(new.b, false) IS DISTINCT FROM old.b)
Rows Removed by Filter: 271427