此请求使用 postgresql 抛出语句超时。
DELETE FROM my_table where my_table.id IN (
SELECT DISTINCT b.id
FROM my_table a
LEFT JOIN my_table b
on b.rowA = a.rowA and b.rowB = true
WHERE a.rowB = false
)
出于某些原因,我无法在 postgresql 上增加超时。所以,我需要改进我的要求。如何改善呢?也许不使用 IN?如何做到这一点?
预先感谢您的帮助。
编辑更多信息:
我在一个 JAVA 批处理中,我得到的错误消息如下:
Caused by: org.postgresql.util.PSQLException: ERROR: canceling statement due to statement timeout at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1525) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1309) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:188) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:452) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:354) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:308) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
最佳答案
我会删除非重复连接和左连接。
delete from my_table where my_table.id in (select b.id from tblA a, tblB b
where a.rowB = false and b.rowa = a.rowa and b.rowb = true);
问题的主要原因是左连接。为什么要在表 a 上使用左连接并将结果基于表 b 的 ID?如果您会使用表 a 的 ID,那么这是否有意义。从表 a 的角度来看,左连接将返回表 a 中的所有 id,即使表 b 中没有匹配项。那不是您想要的,因为它可能是大量数据。您想要表 a 和表 b 的子集。因此,您不应该在不使用(左连接)的情况下询问所有数据。 Distinct 也会造成性能问题。只有在它具有附加值时才使用它。我在这里看不到附加值。
关于java - 如何改进抛出 "statement timeout"的 postgresql 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16420918/