更新postgresql的大量数据

时间:2017-02-13 15:31:39

标签: postgresql

使用的主表是transaction,可以存储数百万行(最多可以存储4-5百万行)。我需要尽快更新状态。

更新查询如下所示:

UPDATE transaction SET transaction.status = 'TO_EXECUTE'
WHERE transaction.id IN (SELECT transaction.id FROM transaction 
JOIN anotherTable ON transaction.id = anotherTable.id
JOIN anotherTable2 ON transaction.serviceId = ontherTable2.id
WHERE transaction.status = :filter1, transaction.filter2 = :filter2, ...)

你有更好的解决方案吗?是否可以更好地创建另一个表来存储状态ID? (我认为updating large Tables可能非常慢)。

2 个答案:

答案 0 :(得分:1)

您的查询的IN部分可能会被重写为“存在”,以便可能获得改进,具体取决于其他表布局和卷。此外,您很可能不需要在子查询中再次提到的事务表(存在或在中)

 UPDATE transaction tx SET transaction.status = 'TO_EXECUTE'
 WHERE exists (SELECT * 
  FROM anotherTable 
  JOIN anotherTable2 ON tx.serviceId = anotherTable2.id
  WHERE anothertable.id=tx.id and
  transaction.status = :filter1 and transaction.filter2 = :filter2,
  ...)

答案 1 :(得分:1)

试试这个:

UPDATE transaction
SET transaction.status = 'TO_EXECUTE'
From anotherTable
JOIN anotherTable2 ON transaction.serviceId = anotherTable2.id
WHERE transaction.id = anotherTable.id AND  transaction.status = :filter1, transaction.filter2 = :filter2, ...