我正在尝试将临时表(“新数据”)与另一个表(“现有数据”)进行比较,以识别添加/更改/删除的行,并最终进行upsert。这是一项昂贵的操作 - 大型数据集上的完全差异。我真的很想使用EXCEPT
命令来理解语法,但是我遇到了严重的性能问题,并且发现LEFT JOIN
要好得多。
这两个表具有相似的行数和相同的模式(几乎 - “第二个”表有一个额外的created_date
列)。
他们共享distkey(date)
和sortkey(date, id1, id2)
;我甚至在EXCEPT
语句中以“正确”的顺序指定列以帮助优化器。
在测试大小的数据子集上,每个查询计划都在下面。
explain
select date, id1, id2, id3, value, attr1, attr2, attr3 from new_data
except select date, id1, id2, id3, value, attr1, attr2, attr3 from existing_data;
XN SetOp Except (cost=1000002817944.78..1000003266822.61 rows=1995013 width=1637)
-> XN Sort (cost=1000002817944.78..1000002867820.09 rows=19950126 width=1637)
Sort Key: date, id1, id2, id3, value, attr1, attr2, attr3
-> XN Append (cost=0.00..399002.52 rows=19950126 width=1637)
-> XN Subquery Scan "*SELECT* 1" (cost=0.00..199501.26 rows=9975063 width=1637)
-> XN Seq Scan on new_data (cost=0.00..99750.63 rows=9975063 width=1637)
-> XN Subquery Scan "*SELECT* 2" (cost=0.00..199501.26 rows=9975063 width=1636)
-> XN Seq Scan on existing_data (cost=0.00..99750.63 rows=9975063 width=1636)
与我的丑陋LEFT JOIN
explain
select t1.* from new_data t1
left outer join existing_data t2 on
t1.date = t2.date
and t1.id1 = t2.id1
and coalesce(t1.id2, -1) = coalesce(t2.id2, -1)
and coalesce(t1.id3, -1) = coalesce(t2.id3, -1)
and coalesce(t1.value, -1) = coalesce(t2.value, -1)
and coalesce(t1.attr1, '') = coalesce(t2.attr1, '')
and coalesce(t1.attr2, '') = coalesce(t2.attr2, '')
and coalesce(t1.attr3, '') = coalesce(t2.attr3, '')
where t2.id1 is null;
XN Merge Left Join DS_DIST_NONE (cost=0.00..68706795.68 rows=9975063 width=1637)
Merge Cond: (("outer".date = "inner".date) AND (("outer".id1)::bigint = "inner".id1))
Join Filter: (((COALESCE("outer".id2, -1))::bigint = COALESCE("inner".id2, -1::bigint)) AND ((COALESCE("outer".id3, -1))::bigint = COALESCE("inner".id3, -1::bigint)) AND ((COALESCE("outer".value, -1::numeric))::double precision = COALESCE("inner".value, -1::double precision)) AND ((COALESCE("outer".attr1, ''::character varying))::text = (COALESCE("inner".attr1, ''::character varying))::text) AND ((COALESCE("outer".attr2, ''::character varying))::text = (COALESCE("inner".attr2, ''::character varying))::text) AND ((COALESCE("outer".attr3, ''::character varying))::text = (COALESCE("inner".attr3, ''::character varying))::text))
Filter: ("inner".id1 IS NULL)
-> XN Seq Scan on new_data t1 (cost=0.00..99750.63 rows=9975063 width=1637)
-> XN Seq Scan on existing_data t2 (cost=0.00..99750.63 rows=9975063 width=1636)
查询费用为1000003266822.61
vs 68706795.68
。我知道我不应该在查询之间进行比较,但是在执行时间已经证明了这一点。有什么想法为什么EXCEPT
语句比这里的LEFT JOIN
慢得多?
答案 0 :(得分:2)
left join
为每个(可能是有序的)键值生成一堆交叉连接的行,然后通过on
过滤出它不想要的那些;它也可以在(可能有序的)旧密钥值超过新密钥值时停止,因为不能再有任何匹配 - 这也涉及通过一些coalesce
SARG智能进行一些推理。 except
首先排序所有内容。在这种情况下,分拣成本高于生成&丢弃行,遍历每个键的右手表行。当然,优化程序可以在其outer join
计划中包含except
成语 - 但显然不会。