SELECT查询的INSERT结果-LEFT JOIN /为NULL或ON CONFLICT是否不能处理重复项?

时间:2019-06-27 12:31:48

标签: postgresql

如果我们有此类查询

INSERT INTO target_table (......)
  SELECT (........)
  FROM origin_table
  WHERE <some conditions>

假设有一些重复项(例如,我们直接选择并插入ID),因此我们需要处理这些重复项。更有表现力的是-LEFT JOIN / IS NULL方法,还是冲突时什么都不做?

INSERT INTO target_table (......)
  SELECT (........)
  FROM origin_table
  LEFT JOIN target_table ON target_table.id = origin_table.id
  WHERE <some conditions> AND target_table.id IS NULL

INSERT INTO target_table (......)
  SELECT (........)
  FROM origin_table
  WHERE <some conditions>
ON CONFLICT (id) DO NOTHING

通常哪个应该更快?

0 个答案:

没有答案