【问题标题】:Postgres: How to update the column?Postgres:如何更新列?
【发布时间】:2018-11-30 07:33:58
【问题描述】:

我使用postgres 库来处理数据库。我有一个非常大的数据库。我需要更改列中的字符。我是这样做的:

  void replace(String col, String from, String to) async {
    String queryStr = "UPDATE $tab SET $col = replace($col, '$from', '$to');";
    await connection.query(queryStr);
  }

如果数据库的行数超过 1,000,000 行,我会收到错误消息:

未处理的异常:0:00:30.000000 之后的 TimeoutException:未来不会 完全的 0 _PostgreSQLConnection&Object&_PostgreSQLExecutionContextMixin._enqueue (包:postgres/src/connection.dart:402:24) 1 _PostgreSQLConnection&Object&_PostgreSQLExecutionContextMixin.query(包:postgres/src/connection.dart:318:12) 2 PG.replace (file:///home/vas/IdeaProjects/infovizion_platform/pg/bin/pg.dart:50:22) 3 主要 (file:///home/vas/IdeaProjects/infovizion_platform/pg/bin/main.dart:81:14) 4 _start隔离。 (dart:isolate/runtime/libisolate_patch.dart:287:32) 5 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)

当我手动执行相同的请求时,没有错误。

我该如何解决这个问题?

【问题讨论】:

  • 你必须增加或设置超时时间
  • @comrad 如何在dart中设置执行限制?
  • 让你的方法返回一个Future。 Future.timeout(new Duration(seconds: 100000),...);

标签: sql database postgresql dart dart-pub


【解决方案1】:

你可以试试这个版本:

UPDATE $tab
    SET $col = replace($col, '$from', '$to')
    WHERE $col LIKE '%' || '$from' || '%';

即只更新值实际出现的列,而不是所有列。

减少实际更改的列数可能会加快查询速度。

【讨论】:

    猜你喜欢
    • 2020-05-06
    • 1970-01-01
    • 2018-10-09
    • 2021-10-01
    • 2019-07-13
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多